如何使用画布可点击制作位图? [英] How to make a bitmap using canvas clickable?

查看:75
本文介绍了如何使用画布可点击制作位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do i make the bitmap created clickable? Below is the code which i have used to create a bitmap using canvas.

 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();
    }

}

推荐答案

通过在此视图上设置OnClickListener,所有视图都可以单击(尽管不限于位图).要检查用户是否仅单击位图本身,必须重写onTouchEvent(MotionEvent事件),并检查触摸坐标是否与位图相同.

By setting an OnClickListener on this view, all of it will be clickable (though not limited to your bitmap). To check whether or not the user clicked only the bitmap itself you have to override onTouchEvent(MotionEvent event) and check if the touch coordinates are the same as the bitmap.

@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替换为用于绘制位图的宽度和高度.

Just replace bitmapXPosition and bitmapYPosition with the coordinates you use to draw the bitmap, and bitmapWidth and bitmapHeight with the width and height you use to draw it.

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

Also, try not to allocate memory (create objects) inside the onDraw() method of any view. It is bad for perfomance.

编辑

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作为坐标绘制位图,可以检查触摸是否在位图内部. 另外,您可以使用

By drawing the bitmap using a Rect as your coordinates you can check if the touch is inside the bitmap or not. Also, instead of that big and ugly "if" statement, you can use

if(r.contains(x, y))
{
     //BITMAP TOUCHED
}

这篇关于如何使用画布可点击制作位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆