如何将drawBitmap设置为不全屏

时间:2011-10-24 07:50:29

标签: android bitmap draw ontouchevent

我有一个位图,我这样画:

bit = BitmapFactory.decodeResource(getResources(),
                   R.drawable.anim_ctrl_panel);
Paint paint = new Paint();
canvas.drawBitmap(bit, 80, 440, paint);

问题是图像被设置为全屏。在onTouchEvent方法中,我实现了位图的事件,并且在我选择屏幕的任何地方实现了事件。我希望图像具有某个位置,并且只有该位置才能实现该事件。我怎么能这样做?

编辑:问题出在onTouchEvent方法上。这是解决方案:

public boolean onTouchEvent(MotionEvent event) {if(80 <= event.getX() && event.getX()<= (80+bit.getWidth()/2) && (440 <= event.getY() && (event.getY() <= 440+bit.getHeight()))){   
            //...
        }else if((80+bit.getWidth()/2) <= event.getX() && event.getX()<= (80+bit.getWidth()) && (440 <= event.getY() && (event.getY() <= 440+bit.getHeight()))){
            //...
            }

这有助于我:How can I check the Image is Touched using OnTouch()

2 个答案:

答案 0 :(得分:1)

试试这个:

canvas.save();
canvas.translate(posX, posY);
canvas.drawBitmap(bit, 0f, 0f, paint);
canvas.restore();

答案 1 :(得分:1)

对于特定区域中的图像集,我认为Rectangle对此很有用,

像这样的东西,(在我的情况下,它为我工作)

Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();         
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());         
paint.setAntiAlias(true);         
canvas.drawARGB(0, 0, 0, 0);         
canvas.drawBitmap(bitmap, rect, rect, paint);

试试吧。

有关详细信息,请查看Android - Drawing (Core)

感谢。