0

我可以在 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;
    }
};
4

1 回答 1

0

你的 onDraw 说

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

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

于 2013-06-12T15:03:21.123 回答