如何设置drawBitmap不全屏

发布于 2024-12-11 23:37:43 字数 999 浏览 0 评论 0原文

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

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()))){
            //...
            }

这对我有帮助:我该怎么办使用 OnTouch() 检查图像是否被触摸

I have a bitmap and I draw it like this :

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

The problem is that the image is set as fullscreen .On onTouchEvent method I implement the event for the bitmap and anywhere I select screen the event is implement. I want the image to have a certain position and only for that position to be implement the event. How can I do this?

EDIT: the problem was at onTouchEvent method. This is the solution :

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()))){
            //...
            }

this help me : How can I check the Image is Touched using OnTouch()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-12-18 23:37:43

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

类似这样的东西(在我的情况下它对我有用)

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 - 绘图(核心)

谢谢。

For Image set in particular area, I think Rectangle is useful for this,

Something like this, (In my case Its work for me)

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

Try it.

For more info look at Android - Drawing (Core)

Thanks.

ゃ人海孤独症 2024-12-18 23:37:43

试试这个:

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

Try this:

canvas.save();
canvas.translate(posX, posY);
canvas.drawBitmap(bit, 0f, 0f, paint);
canvas.restore();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文