在SurfaceView上分开滑动多个位图

时间:2013-06-12 14:45:16

标签: android android-canvas surfaceview

我可以在SurfaceView上绘制多个位图。但我无法将其中一个分开滑动。当我尝试滑动时,它们就像一个位图。他们一起移动。 SurfaceView上只有一个位图时没有问题。当有多个位图时,如何控制一个位图?

感谢。

我的位图:

normalButton = BitmapFactory.decodeResource(getResources(), R.drawable.cyan);
normalButton=normalButton.copy(normalButton.getConfig(), true);
normalButton1=normalButton.copy(normalButton.getConfig(), true);
canvasNormalButton = new Canvas(normalButton);

pressedButton = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
pressedButton=pressedButton.copy(pressedButton.getConfig(), true);
canvasPressedButton = new Canvas(pressedButton);

slideButton = BitmapFactory.decodeResource(getResources(), R.drawable.green);
slideButton=slideButton.copy(slideButton.getConfig(), true);

我的SurfaceView类:

class surfaceClass extends SurfaceView
{
public surfaceClass(Context context) 
{
    super(context);
    setWillNotDraw(false);
    this.setOnTouchListener(listener);
}

@Override
protected void onDraw(Canvas canvas) 
{   
// I add two bitmaps. But I can not slide them apartly.
        canvas.drawBitmap(normalButton, newX, newY, null);
        canvas.drawBitmap(normalButton, newX+40, newY, null);
}

private OnTouchListener listener = new OnTouchListener() 
{   
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getActionMasked()) 
        {
        case MotionEvent.ACTION_MOVE:
            if(buttonPressed==true)
            {
                canvasNormalButton.drawBitmap(slideButton, 0, 0, null);
                                    newX=event.getX()
                                    newY=event.getY()
                invalidate();
            }
            break;

        case MotionEvent.ACTION_UP:     
            buttonPressed=false;
            canvasNormalButton.drawBitmap(normalButton1, 0, 0, null);
            invalidate();
            break;

        case MotionEvent.ACTION_DOWN:

                if(0<event.getX() && event.getX()<40 && event.getY()<40 && 0<event.getY())
                {
                    buttonPressed=true;
                    canvasNormalButton.drawBitmap(pressedButton, 0, 0, null);
                    invalidate();
                }

            break;
        }
        return true;
    }
};

1 个答案:

答案 0 :(得分:0)

你的onDraw说

canvas.drawBitmap(normalButton, newX, newY, null);
canvas.drawBitmap(normalButton, newX+40, newY, null);

我认为其中一个你的意思是normalButton1