Android:将画布绘制到ImageView

Android:将画布绘制到ImageView,android,imageview,android-canvas,draw,Android,Imageview,Android Canvas,Draw,我是android编程新手,我想弄明白的是: 在我的布局中,我有一个文本视图、图像视图和按钮,它们都位于垂直方向的线性布局上 我希望能够在ImageView中动态绘制圆圈,而不会干扰布局的其余部分(textview/按钮)。我正在尝试创建一个画布,并在画布中使用drawcircle函数来设置圆的位置。然后以某种方式将画布绘制到我的imageview。我不能让它工作,有什么诀窍吗?还是我的方法根本错误?在不重新创建整个布局的情况下,如何在ImageView中绘制圆 谢谢 如果您有一个布局为所有元素

我是android编程新手,我想弄明白的是:

在我的布局中,我有一个文本视图、图像视图和按钮,它们都位于垂直方向的线性布局上

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


谢谢

如果您有一个布局为所有元素垂直排列的xml。

您可以通过在包中创建一个类来实现所需的功能,该类扩展类视图并覆盖其onDraw方法。根据需要在其中绘制圆圈。

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

 < TextView> < /TextView>

  < Button> < /Button>

  <com.prac.MyView> </ com.prac.MyView>


检查以下链接中的二维图形。这是一本很好的教程。

希望对您有所帮助:)

有几种方法可以满足您的需求,但您所描述的使用ImageView的方式并不是其中之一。一种可能是让ImageView显示一个动画的可绘制视图。然后,您可以专注于让您的可绘制实现绘制圆圈。另一种方法是在每次需要更改图像时创建新位图,并将ImageView设置为显示新位图。不过,通常的方法是创建一个自定义视图子类。示例项目有一个自定义视图的示例,您可以在Google上找到很多教程。

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

  • 创建一个新的图像位图并向其附加一个全新的画布
  • 将图像位图绘制到画布中
  • 在画布上画出所有你想要的东西
  • 将画布附加到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));
    

    我认为更好的方法是创建自定义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();
    }
    }
    
    最好实现一些逻辑,通过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"/>
    

    只是一个建议,因为我遇到了同样的问题,我们不需要创建一个新的BitmapDrawable,我们还可以利用myImageView.setImageBitmap(tempBitmap)函数@PeterKä强积金商店盗窃?这是我的博客,伙计。如果我想在setImageBitmap之后删除画布呢?
       <com.mycustomviews.CustomView
            android:id="@+id/customView"
            android:layout_centerInParent="true"
            android:layout_height="135dp"
            android:layout_width="240dp"
           android:background="@android:color/transparent"/>
    
       customView = (CustomView) findViewById(R.id.customView);