Android:将画布绘制到ImageView

sil*_*sil 33 android draw imageview android-canvas

我是android编程的新手,我想弄清楚的是这个;

在我的布局中,我有一个TextView,ImageView和Button,所有这些都在垂直方向的LinearLayout上.

我希望能够在ImageView中动态绘制圆圈,而不会干扰我的其余布局(textview/button).我正在尝试创建一个画布,并使用画布中的drawcircle函数来设置圆的位置.然后以某种方式将该画布绘制到我的imageview.我不能让这个工作,这有什么诀窍吗?或者我的方法根本错了?如何在不重新创建整个布局的情况下将图形绘制到ImageView?

谢谢!

Nan*_*oka 32

我遇到了同样的挑战,并得出结论,覆盖onDraw至少在一般情况下不起作用.我的博客解释了原因.对我来说非常有效的是:

  1. 创建一个新的图像位图并为其附加一个全新的画布.
  2. 将图像位图绘制到画布中.
  3. 将所有其他内容绘制到画布中.
  4. 将画布附加到ImageView.

这是一个代码片段:

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));
Run Code Online (Sandbox Code Playgroud)

  • 只是一个建议,因为我遇到了同样的问题,而不是创建一个新的BitmapDrawable,我们也可以利用myImageView.setImageBitmap(tempBitmap)函数! (7认同)

Job*_*ews 27

     ImageView imageView=(ImageView) findViewById(R.id.image);
        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);    
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(50, 50, 10, paint);
        imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)


Lys*_*Lys 5

我认为更好的方法是创建自定义ImageView并覆盖onDraw方法.就像是:

public class CustomView extends ImageView {

public CustomView(Context context) {
    super(context);
}

public CustomView(Context context, AttributeSet attrst) {
    super(context, attrst);
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

MyBitmapFactory bitMapFac = null;
public void setBitmapFactory(MyBitmapFactory bitMapFac)
{
    this.bitMapFac = bitMapFac;
}

@Override
public void onDraw(Canvas canvas) {

    canvas.drawColor(Color.TRANSPARENT);
    /*instantiate a bitmap and draw stuff here, it could well be another
    class which you systematically update via a different thread so that you can get a fresh updated
    bitmap from, that you desire to be updated onto the custom ImageView. 
   That will happen everytime onDraw has received a call i.e. something like:*/
    Bitmap myBitmap = bitMapFac.update(); //where update returns the most up  to date Bitmap
    //here you set the rectangles in which you want to draw the bitmap and pass the bitmap        
    canvas.drawBitmap(myBitMap, new Rect(0,0,400,400), new Rect(0,0,240,135) , null);
    super.onDraw(canvas);
    //you need to call postInvalidate so that the system knows that it  should redraw your custom ImageView
    this.postInvalidate();
}
}
Run Code Online (Sandbox Code Playgroud)

最好实现一些逻辑,检查是否有通过update()方法获取的新位图,以便onDraw中的代码不会每次都执行并将开销放到系统中.

然后在您需要的任何地方使用自定义视图.最简单的方法是直接在activity_layout.xml中声明它:

   <com.mycustomviews.CustomView
        android:id="@+id/customView"
        android:layout_centerInParent="true"
        android:layout_height="135dp"
        android:layout_width="240dp"
       android:background="@android:color/transparent"/>
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令访问代码,就像任何其他视图一样:

   customView = (CustomView) findViewById(R.id.customView);
Run Code Online (Sandbox Code Playgroud)


Jav*_*tor 0

如果您有一个 xml 布局,其中所有元素都以垂直方向排列。

您可以通过在包中创建一个扩展 Class View 的类并重写其 onDraw 方法来实现您想要的目的。根据需要在其中画圈。

然后不要在布局中添加 imageView,而是在 xml 布局中添加您自己的视图。如下所示

<线性布局>

 < TextView> < /TextView>

  < Button> < /Button>

  <com.prac.MyView> </ com.prac.MyView>
Run Code Online (Sandbox Code Playgroud)

</LinearLayout>

检查以下 2D 图形链接。这是一本非常值得阅读的教程。 http://organicandroid.blogspot.com/2010/08/starting-to-play-with-graphics.html 希望这有帮助:)