Android-如何在画布上拖动图片 [英] Android- How to drag a picture on a canvas

查看:107
本文介绍了Android-如何在画布上拖动图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像在视频游戏中一样拖动图像.我画了几条线,我需要将其拖动并查看在离开"窗口之前绘制的线.我给你两张图片,试图帮助理解它.

I need to drag an image like in a videogame. I have a few lines drawed and I need to drag it and see the lines drawed before "out" of the window. I give you two images trying to help to understand it.

拖动前的图像:

然后我用手指触摸屏幕,然后移动到屏幕顶部,因此这三行必须位于初始图像上方,如下所示:

Then I touch the screen with my finger and I make a move to the top of the screen, so this three lines have to be upper than the initial image, like this:

我不知道该如何执行拖曳效果",有人可以帮助我吗?这不是作为.jpg文件的图像,而是将在画布上用"drawLine"绘制的三行绘制到imageView中.

I don't know how to do this "drag efect", can someone help me? This is not an image as a .jpg file, is three lines drawed with "drawLine" on a canvas into an imageView.

我的onTouchEvent代码和我的drawRectangle代码:

My onTouchEvent Code and my drawRectangle code:

public boolean onTouch(View view, MotionEvent event) 
{
     switch (event.getAction()) 
     {      
        case MotionEvent.ACTION_UP: 
        {
            x = event.getX();
            y = event.getY();

            getFirstFoot();
            return true;
        }
        case MotionEvent.ACTION_MOVE:
        {
            ammountX = event.getX() - startX;
            ammountY = event.getY() - startY;

            canvas.save();
            canvas.translate(ammountX, ammountY);
            //canvas.restore();

            imageView.invalidate();

            return true;    
        }           
        case MotionEvent.ACTION_DOWN:
        {
            startX = event.getX();
            startY = event.getY();
        }
     }      
     return true;   
}

drawRectagle代码:

drawRectagle code:

private void drawRectangle(boolean erase)
{
    buttonRed.setVisibility(View.VISIBLE);
    buttonGree.setVisibility(View.VISIBLE);
    buttonYellow.setVisibility(View.VISIBLE);


    Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ascensorsininfo);
    canvas.drawBitmap(bitmap2, x - (bitmap2.getWidth() / 2), y - bitmap2.getHeight(), new Paint());

    drawedPoints.add(String.valueOf(x - (bitmap2.getWidth() / 2)));
    drawedPoints.add(String.valueOf(y - bitmap2.getHeight()));

    imageView.invalidate();

    y -= bitmap2.getHeight();              
}

任何解决方案都会被忽略.谢谢!

Any solution would be apreciated. Thanks!

推荐答案

查看它将在之后绘制的所有内容移动"在x和y轴上说的量.

It will "move" everything you draw afterwards the ammount you say in x and y axis.

别忘了保存和还原它,例如:

Don't forget to save and restore it, like:

canvas.save();
canvas.translate(ammountX, ammountY);
// [draw your stuff here]
canvas.restore();

如果您的问题更多关于如何检测拖动效果,则必须重写视图的onTouchEvent方法.像这样:

If your question is more towards how to detect the dragging effect, you have to Override your view's onTouchEvent method. Something like:

@Override
public boolean onTouchEvent (MotionEvent event){
    if(event.getActionMasked() == MotionEvent.DOWN){
        startX = event.getX();
        startY = event.getY();
        return true;
    }else if(event.getActionMasked() == MotionEvent.MOVE){
        ammountX = event.getX() - startX;
        ammountY = event.getY() - startY;
        return true;
    }
    return super.onTouchEvent(event);
}

这篇关于Android-如何在画布上拖动图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆