Android:如何在可移动图像上绘制一个矩形,使得绘制的矩形也随图像一起移动?

时间:2013-08-12 16:36:46

标签: android surfaceview

我有一个可以在整个屏幕上移动的图像。现在,我想在这个图像上绘制一个矩形,这样当我移动图像时,图像上绘制的矩形也会移动。目前我可以不在图像上绘制矩形,而是在包含图像的SurfaceView上绘制矩形。我目前的代码如下。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);       


      int w=getWindowManager().getDefaultDisplay().getWidth()-25;
      int h=getWindowManager().getDefaultDisplay().getHeight()-25;

      MySurfaceView mySurface=new MySurfaceView(this,w,h);
      setContentView(mySurface);
  }
}

我有一个名为MySurfaceView.java的课程

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private Bitmap bitmap ;
private MyThread thread;
private int x=20,y=20;int width,height;

public MySurfaceView(Context context,int w,int h) {
    super(context);

    width=w;
    height=h;
    thread=new MyThread(getHolder(),this);
    getHolder().addCallback(this);
    setFocusable(true);
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.my_pic);
    canvas.drawColor(Color.BLUE);//To make background 
    canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);


    Paint paintShape = new Paint();
    paintShape.setColor(Color.BLACK);
    paintShape.setStyle(Paint.Style.STROKE);

    Rect myRectangle = new Rect();
    myRectangle.set(0, 100, canvas.getWidth()/4, canvas.getHeight()/4);

    canvas.drawRect(myRectangle, paintShape);


}

@Override
public boolean onTouchEvent(MotionEvent event) {

    x=(int)event.getX();
    y=(int)event.getY();

    if(x<25)
            x=25;
     if(x> width)   
            x=width;
     if(y <25)
            y=25;
     if(y > 405)
            y=405;      
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    thread.startrun(true);
    thread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {


    thread.startrun(false);
    thread.stop();

}

public class MyThread extends Thread{

    private SurfaceHolder msurfaceHolder;
    private MySurfaceView mSurfaceView;
    private boolean mrun =false;

    public MyThread(SurfaceHolder holder, MySurfaceView mSurfaceView) {

        this.msurfaceHolder = holder;
        this.mSurfaceView=mSurfaceView;
    }

    public void startrun(boolean run) {

        mrun=run;
    }

    @SuppressLint("WrongCall")
    @Override
    public void run() {

        super.run();
         Canvas canvas;
         while (mrun) {
            canvas=null;
             try {
                 canvas = msurfaceHolder.lockCanvas(null);
                  synchronized (msurfaceHolder) {
                   mSurfaceView.onDraw(canvas);
                 }
             } finally {
                     if (canvas != null) {
                     msurfaceHolder.unlockCanvasAndPost(canvas);
                 }
             }
         }
      }
}

}

有人可以帮助我改进我的代码以适应上述期望的任务。谢谢!

2 个答案:

答案 0 :(得分:0)

在onCreate中,为该位图创建一个新的Bitmap图像和一个画布。使用该画布将原始位图和矩形绘制到新位图。然后在onDraw中,只需在任意位置绘制新的位图。作为奖励,你将有非常快的onDraws。

答案 1 :(得分:0)

嗯,我想出了自己的方式。这只是上述代码中的一个小变化,这是必要的。足以定义矩形相对于由于触摸事件而获得的x和y的坐标。例如,您可以执行以下操作:

myRectangle.set(x-50, y-50, x+50, y+50);

其余部分将自动处理。