安卓:绘图画布到ImageView的画布、ImageView

2023-09-04 11:11:27 作者:孤客

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

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

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

谢谢!

解决方案

我有同样的挑战,得出的结论是覆盖的OnDraw至少会在一般情况下不能正常工作。 我的博客解释了原因。什么工作对我非常好如下:

创建一个新的形象的位图,并附加了一个全新的画布来了。 绘制图像位图到画布上。 在画你想要的一切东西到画布上。 画布附加到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));
 
用AI作图后,尺寸不够大,可以再到ps里重新调节尺寸吗 会影响质量吗

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:

Create a new image bitmap and attach a brand new canvas to it. Draw the image bitmap into the canvas. Draw everything else you want into the canvas. 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));