10

如何使创建的位图可点击?下面是我用来使用画布创建位图的代码。

 public class DrawView extends View implements OnClickListener
{
    public DrawView(Context context)
    {
        super(context);
        paint = new Paint();
        image = BitmapFactory.decodeResource(getResources(), R.drawable.andmrktsmall);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        int canWidth = canvas.getWidth();
        int canHeight = canvas.getHeight();
        int width = (canWidth - 200) / 2;
        int height = (canHeight - 100) / 2;
        Bitmap indexcanvas = Bitmap.createScaledBitmap(image, 200, 100, true);
        canvas.drawBitmap(indexcanvas, width, height, paint);
        this.setBackgroundColor(Color.YELLOW);

    }

    @Override
    public void onClick(View v)
    {
        Toast.makeText(context, "View is clicked", 1).show();
    }

}
4

2 回答 2

25

通过在此视图上设置 OnClickListener,所有这些都将是可点击的(但不限于您的位图)。要检查用户是否仅单击位图本身,您必须覆盖 onTouchEvent(MotionEvent event) 并检查触摸坐标是否与位图相同。

@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        //Check if the x and y position of the touch is inside the bitmap
        if( x > bitmapXPosition && x < bitmapXPosition + bitmapWidth && y > bitmapYPosition && y < bitmapYPosition + bitmapHeight )
        {
            //Bitmap touched
        }
        return true;
    }
    return false;
}

只需将 bitmapXPosition 和 bitmapYPosition 替换为用于绘制位图的坐标,将 bitmapWidth 和 bitmapHeight 替换为用于绘制它的宽度和高度。

此外,尽量不要在任何视图的 onDraw() 方法中分配内存(创建对象)。这对性能不利。

编辑

private Rect r;
private Paint paint;
Bitmap bitmap;

public TestRect(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    paint = new Paint();
    paint.setColor(Color.BLUE);
    r = new Rect();
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}

public TestRect(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
    paint.setColor(Color.BLUE);
    r = new Rect();
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}

public TestRect(Context context) {
    super(context);
    paint = new Paint();
    paint.setColor(Color.BLUE);
    r = new Rect();
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}

@Override
public void onDraw(Canvas c)
{

    r.set(getWidth()/2, getHeight()/2, getWidth()/2 + 200, getHeight()/2 + 200);
    //c.drawRect(r, paint);
    c.drawBitmap(bitmap, null, r, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        //Check if the x and y position of the touch is inside the bitmap
        if( x > getWidth()/2 && x < getWidth()/2 + 200 && y > getHeight()/2 && y < getHeight()/2 + 200 )
        {
            Log.e("TOUCHED", "X: " + x + " Y: " + y);
            //Bitmap touched
        }
        return true;
    }
    return false;
}

通过使用 Rect 作为坐标绘制位图,您可以检查触摸是否在位图中。此外,您可以使用

if(r.contains(x, y))
{
     //BITMAP TOUCHED
}
于 2013-09-16T11:38:10.577 回答
6
@Override
 public boolean onTouchEvent(MotionEvent event) 
 {
    int x=(int)event.getX();
    int y=(int)event.getY();
    if (drawable.getBounds().contains(x,y)  &&
           event.getAction()==MotionEvent.ACTION_DOWN)
        {
            Log.e(TAG, "onTouchEvent: drawable touched ");
            return true;
        }
        return false;
    }
于 2016-11-17T06:46:08.553 回答