在画布上绘制位图/图像,用户触摸屏幕

时间:2013-09-09 15:49:56

标签: android bitmap android-canvas surfaceview ontouchevent

我是android新手。我想绘制用户触摸的位图。这会在触摸时创建一个位图,但只创建一个位图,并在用户触摸时将其替换并移动到新位置。我想在新的触摸位置绘制一个新的位图,将旧位置留在原位,任何位图都可以移动/拖动到新位置。请帮助

public class SurfaceViewEx extends Activity implements OnTouchListener {

    OurView v;
    Bitmap cloud;
    float x, y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
        v = new OurView(this); //Passing the context of this class
        v.setOnTouchListener(this);
        x = y = 0;
        setContentView(v);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        v.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        v.resume();
    }

    //It will handle drawing
    public class OurView extends SurfaceView implements Runnable {

        Thread t = null;
        SurfaceHolder holder; //
        boolean isItOk = false;

        public OurView(Context context) {
            super(context);
            //To make sure we can draw on canvas and surface is valid
            holder = getHolder();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (isItOk == true) {
                //Perform canvas drawing
                if(!holder.getSurface().isValid()) {
                    //Call back to while ..
                    continue; 
                }
                //Lock the canvas, and unlock after drawing
                Canvas c = holder.lockCanvas();
                c.drawARGB(255, 0, 0, 0);
                c.drawBitmap(cloud, x - (cloud.getWidth()/2), y - (cloud.getHeight()/2), null);
                holder.unlockCanvasAndPost(c);
                }
        }

        public void pause() {
            isItOk = false;
            while(true) {
                try {
                    //Blocks the current thread until the receiver finishes 
                    //the execution and dies
                    t.join(); 

                } 
                catch (InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            //t = null;
        }

        public void resume() {
            isItOk = true; //Able to draw
            t = new Thread(this);
            t.start();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent me) {
        // TODO Auto-generated method stub

        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch(me.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = me.getX();
                y = me.getY();
                break;
            case MotionEvent.ACTION_UP:
                //Same code
            case MotionEvent.ACTION_MOVE:
                //Same code
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

我建议使用面向对象的解决方案:

class DragableObject {
  int x;
  int y;
  Bitmap bitmap;

  public DragableObject(Bitmap bitmap, int x, int y) {
    this.x = x;
    this.y = y;
    this.bitmap = bitmap;
  }

  public void draw(Canvas c) {
    c.drawBitmap(bitmap, x, y);
  } 

  public boolean grab(int touchX, int touchY) {
    // TODO check if object is touched (coordinates are inside objects bounds), return value accordingly
  }

  public void drag(int deltaX, int deltaY){
    this.x += deltaX;
    this.y += deltaY;
  }
}

比表面类:

DragableObject selectedObject = null;
int previousX;
int previousY;
List<DragableObject> objects = new ArrayList<DragableObject>();

public boolean onTouch(View v, MotionEvent me) {
  int x = me.getX();
  int y = me.getY();
  switch(me.getAction()) {
  case MotionEvent.ACTION_DOWN:
    previousX = x;
    previousY = y;
    for (DragableObject object : objects) {
      if (object.grab()) {
        selectedObject = object;
        break;
      }
    }          
    break;
  case MotionEvent.ACTION_UP:
    selectedObject = null;
    break;
  case MotionEvent.ACTION_MOVE:
    if (selectedObject != null) {
      selectedObject.drag(x - previousX, y - previousY);
      previousX = x;
      previousY = y;
    }
    break;
   }
  return true;
}

当你在表面上绘图时,只需绘制对象: ...

  for (DragableObject object : objects) {
    object.draw(c);
  }

...