如何在Android画布上移动位图? [英] How to move a bitmap on android canvas ?

查看:210
本文介绍了如何在Android画布上移动位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android画布上绘制了这个位图 -



  protected   void  onDraw(Canvas canvas){

super.onDraw(canvas);

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic1);

canvas.drawBitmap(myBitmap, 0 0 );

}







如何将位图移动到坐标(100 ,100)?

有没有办法在android上做位图的Translate Animation?

解决方案

和许多其他图形渲染系统一样,你的代码是需要绘图时调用的某个事件的处理程序。你硬编码了位图坐标,所以你期望什么。您必须根据声明类的某些字段进行这些坐标,例如 bitmapX bitmapY 。您修改这些字段的值,然后重新绘制视图。



如何?如果我可以假设您的代码是在类 SurfaceView 派生的类中声明的,则可以调用其中一个 invalidate 方法: http://developer.android.com/reference/android/view/SurfaceView.html [ ^ ]。



它最终会调用事件(双关语不是故意的)导致重新渲染。根据失效方法,它将重绘脏区域(矩形,对象等)。在你的情况下,它应该重新绘制至少计算为位图占用的旧和新区域的并集的区域。



- SA

I have this bitmap drawn on android canvas -

protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);

            Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

            canvas.drawBitmap(myBitmap, 0, 0, null);

        }




How do I move the Bitmap to coordinates (100, 100) ?
Is there any way to do Translate Animation for Bitmap on android ?

解决方案

As in many other graphics rendering system, your code is the handler of some event invoked when the drawing is required. You hard-coded the bitmap coordinates, so what would you expect. You have to make these coordinate depending on some field(s) of declaring class, say, bitmapX, bitmapY. You modify the values of these fields and then redraw the view.

How? If I can assume that your code is declared in the class derived from the class SurfaceView, you can call one of the invalidate methods: http://developer.android.com/reference/android/view/SurfaceView.html[^].

It will eventually invoke the event (pun not intentional) cause re-rendering. Depending on invalidation method, it will redraw "dirty" area (rectangle, object, etc.). In your case, it should redraw at least the area calculated as the union of the "old" and "new" areas occupied by the bitmap.

—SA


这篇关于如何在Android画布上移动位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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