安卓:绘图画布到ImageView的 [英] Android: Drawing a canvas to an ImageView

查看:723
本文介绍了安卓:绘图画布到ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid编程和我想要搞清楚的是这一点;

在我的布局,我有一个TextView,ImageView的和按钮,所有的垂直方向的LinearLayout中。

我希望能够动态地画圈圈的ImageView的,而不会干扰我的布局(的TextView /按钮)的其余部分。我试图创建一个画布,并使用画圆函数内的画布,设置圆的位置。然后绘制的帆布我以某种方式ImageView的。我不能得到这个工作,是有一招呢?或者是我的方法从根本上错了吗?我将如何去约画圆到的ImageView无需重新创建我的整个布局?

谢谢!

解决方案

我有同样的挑战,得出的结论是覆盖的OnDraw至少会在一般情况下不能正常工作

我的博客解释了原因。什么工作对我非常好如下:

  1. 创建一个新的形象的位图,并附加了一个全新的画布来了。
  2. 绘制图像位图到画布上。
  3. 在画你想要的一切东西到画布上。
  4. 画布附加到ImageView的。

下面是一个code段为这样的:

 进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.RectF;
进口android.graphics.drawable.BitmapDrawable;

ImageView的myImageView = ...
位图MYBITMAP = ...
油漆myRectPaint = ...
INT X1 = ...
INT Y1 = ...
INT X2 = ...
INT Y2 = ...

//创建一个新位图图像,并附加了一个全新的画布给它
位图tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(),myBitmap.getHeight(),Bitmap.Config.RGB_565);
帆布tempCanvas =新的Canvas(tempBitmap);

//绘制图像的位图到卡瓦酒
tempCanvas.drawBitmap(MYBITMAP,0,0,NULL);

//绘制要到画布一切,在本实施例中具有圆边的矩形
tempCanvas.drawRoundRect(新RectF(X1,Y1,X2,Y2),2,2,myPaint);

//画布附加到的ImageView
myImageView.setImageDrawable(新BitmapDrawable(getResources(),tempBitmap));
 

I'm new to android programming and what I'm trying to figure out is this;

In my layout i have a TextView, ImageView, and Button, all on a vertically oriented LinearLayout.

I want to be able to dynamically draw circles in the ImageView, without disturbing the rest of my layout(textview/button). I'm trying to create a canvas, and use the drawcircle function within canvas to set the location of the circle. And then draw that canvas to my imageview in some way. I cannot get this to work, is there a trick to this? Or is my method fundamentally wrong? How would i go about drawing circles to the ImageView without recreating my entire layout?

Thanks!

解决方案

I had the same challenge and came to the conclusion that overwriting onDraw will at least in the general case not work. My blog explains the reasons. What worked very well for me is the following:

  1. Create a new image bitmap and attach a brand new canvas to it.
  2. Draw the image bitmap into the canvas.
  3. Draw everything else you want into the canvas.
  4. Attach the canvas to the ImageView.

Here is a code snippet for this:

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;

ImageView myImageView = ...
Bitmap myBitmap = ...
Paint myRectPaint = ...
int x1 = ...
int y1 = ...
int x2 = ...
int y2 = ...

//Create a new image bitmap and attach a brand new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);

//Draw the image bitmap into the cavas
tempCanvas.drawBitmap(myBitmap, 0, 0, null);

//Draw everything else you want into the canvas, in this example a rectangle with rounded edges
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, myPaint);

//Attach the canvas to the ImageView
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));

这篇关于安卓:绘图画布到ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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