ITKeyword,专注技术干货聚合推荐

注册 | 登录

android - Draw multiple times from a single bitmap on a canvas

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android Bitmap与Canvas

推荐安卓开发神器(里面有各种UI特效和android代码库实例) 位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。 1. 从资源中获取位图 可

up vote 2 down vote favorite 1 Basically i have the following bitmap: Bitmap cloud; cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud); What i want to do is draw the same bitmap(in this case cloud) multiple times in different(random) locations on canvas. I want to do this in the OnDraw method of my view. So the final product should look like this: However This is the code for drawing the bitmap multiple times on the same canvas: protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

// canvas.drawColor(Color.BLUE);

//drawBackground(canvas);

//objectMovement(canvas);

for (int i = 0; i < 500; i += 50) {

canvas.drawBitmap(cloud, 50 + rand.nextInt(350),

10 + rand.nextInt(350), null);

}

invalidate();

}

As you can see, i'm using random to generate the position of the bitmap, and everytime the bitmap is drawn in a different place on the canvas. The only problem is, everytime invalidate() is being called, the drawing in the for loop happens again. Because of this, it looks like the drawings(cloud) is moving very fast all over the place. After searching for a while, i found this question: Draw multiple times from a single bitmap It's pretty much the same question as my question, but onfortunately there is no right answer there. So how do i archieve this? Thanks! android view bitmap draw
  |
  this question edited May 23 at 12:19 Community ♦ 1 1 asked Oct 3 '12 at 18:41 MeesterPatat 586 2 11 29



 |  1 Answers

up vote 0 down vote The first problem is that you are reading in the bitmap 10 times per frame which is completely unnecessary. You should read the bitmap in once when you instantiate your class. I've also added a Paint. I don't know if it

推荐:android draw text at canvas

最近在定制一个画文字的控件,想把文字画到指定的点。开始始终会有几个像素的偏差。 经过研究找到了几个在调用画布来画文字的时候需要注意的地方: 1.文字大小的

's completely necessary but just incase you decide to do more in your draw method it's there. public class MyView extends View

{

private Bitmap _cloud;

private Paint _paint;

public MyView(Context context)

{

super(context);

_cloud = BitmapFactory.decodeResource(getResources(),R.drawable.cloud);

_paint = new Paint();

}

protected void onDraw(Canvas canvas)

{

for(int i = 0; i < 500; i += 50)

{

canvas.drawBitmap(_cloud, 50 + rand.nextInt(350rand.nextInt(350)), 10 + i, _paint);

}

}

}


  |
  this answer answered Oct 3 '12 at 18:50 CaseyB 20.6k 8 55 99      Thank you for your answer, your right about reading the Bitmap once. My mistake. However i asked my question the wrong way. You can see the updated question in the opening post. –  MeesterPatat Oct 3 '12 at 19:14      First, don't call invalidate. If you're looking for something that keeps drawing as fast as it can you should use a SurfaceView. Second the reason that they look like they move very fast all over the place is because you are getting a new, random location each frame. This means that they are moving very fast all over the place. If you want them to stay still then you need to store the location of each circle and use that location to draw them every time. –  CaseyB Oct 3 '12 at 19:26



 | 

推荐:bitmap与canvas android

位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。 1. 从资源中获取位图 可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。

up vote 2 down vote favorite 1 Basically i have the following bitmap: Bitmap cloud; cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud); What i want to do is dra

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。