2011-12-29 83 views
2

因此我有一个画布,并且我希望某个图像(比如说image.png)出现在用户触摸的位置。我会用什么? OnTouch?Android:在用户触摸的画布上绘制图像

有人可以帮我吗?下面是我的画布课程。现在我有一些随机的图像和形状画出来,但它们马上就会发生,而不是当有人碰到我想要的东西时。

在此先感谢!

公共类MyView的扩展视图

private Canvas canvas; 
private Bitmap bitmap; 

public MyView(Context context) { 
    super(context); 



    }  

protected void onSizeChanged(int curw, int curh, int oldw, int oldh) { 
    if (bitmap != null) { 
     bitmap .recycle(); 
    } 
    canvas= new Canvas(); 
    bitmap = Bitmap.createBitmap(curw, curh, Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(bitmap); 


} 
public void destroy() { 
    if (bitmap != null) { 
     bitmap.recycle(); 
    } 
} 

public void onTouch(Canvas canvas) { 
    Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
    canvas.drawColor(Color.TRANSPARENT); 
    canvas.drawBitmap(_scratch, 0, 0, null); 
} 

public void onDraw(Canvas canvas) { 
    //draw onto the canvas 


    Bitmap _scratch1 = BitmapFactory.decodeResource(getResources(), R.drawable.spacecat); 
    canvas.drawColor(Color.TRANSPARENT); 
    canvas.drawBitmap(_scratch1, 0, 0, null); 



    Paint myPaint = new Paint(); 
    myPaint.setStrokeWidth(4); 
    myPaint.setColor(0xFF097286); 
    canvas.drawCircle(240, 40, 30, myPaint); 
    myPaint.setColor(0xFFF07222); 
    Point p1 = new Point(); 
    Point p2 = new Point(); 
    p1.x = 0; 
    p1.y = 0; 
    p2.x = 40; 
    p2.y = 55; 
    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, myPaint); 
    float[] pts = new float[8]; 
    pts[0] = 100; 
    pts[1] = 5; 
    pts[2] = 97; 
    pts[3] = 9; 
    pts[4] = 90; 
    pts[5] = 15; 
    pts[6] = 84; 
    pts[7] = 20; 
    myPaint.setColor(0xFF40FF40); 
    myPaint.setStrokeWidth(9); 
    myPaint.setAntiAlias(true); 
    canvas.drawPoints(pts, myPaint); 


    myPaint.setColor(0xFFF0FF00); 
    myPaint.setStrokeWidth(4); 
    myPaint.setStyle(Paint.Style.STROKE); 
    canvas.drawCircle(110, 150, 100, myPaint); 

}} 
+0

也许通过使用onClick事件?并检查,它可能会有所帮助:http://stackoverflow.com/questions/2826576/android-custom-view-onclickevent-with-x-y-locations – 2011-12-29 17:49:46

回答

3

我写了一个例子:

public class MyView extends View { 

    boolean touching = false; 

    public MyView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public MyView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); //To change body of overridden methods use File | Settings | File Templates. 

     if (touching) { 
      //You can draw other thing here. just draw bitmap for example. 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
      Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
      Rect bitmapRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
      canvas.drawBitmap(bitmap, source, bitmapRect, new Paint()); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touching = true; 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_CANCEL: 
      case MotionEvent.ACTION_UP: 
       touching = false; 
       invalidate(); 
       break; 
     } 
     return super.onTouchEvent(event); //To change body of overridden methods use File | Settings | File Templates. 
    } 
}