2011-02-07 119 views
23

我是新来的android编程,我试图找出的是这个;Android:绘制画布到ImageView

在我的布局中,我有一个TextView,ImageView和Button,都是在一个垂直方向的LinearLayout上。

我希望能够在ImageView中动态绘制圆,而不会影响我的布局(textview/button)的其余部分。我正在尝试创建一个画布,并在画布中使用drawcircle函数来设置该圆的位置。然后以某种方式将该画布绘制到我的图像视图中。我无法得到这个工作,这有什么窍门吗?或者我的方法根本错误?我将如何去绘图圈到ImageView而不重新创建我的整个布局?

谢谢!

回答

0

如果你有一个xml布局,所有的元素都是垂直排列的。

您可以通过在扩展Class View并重写其onDraw方法的软件包中创建一个类来实现所需的功能。在你想要的地方画圆圈。

然后,而不是在布局中添加的ImageView在XML中添加这个您自己的看法layout.like以下

<的LinearLayout>

< TextView> < /TextView> 

    < Button> < /Button> 

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

< /的LinearLayout>

检查以下链接2D图形。它是一个伟大的教程阅读。
http://organicandroid.blogspot.com/2010/08/starting-to-play-with-graphics.html
希望这有助于:)

+0

请更新的链接。博客被删除 – 2013-08-20 08:56:09

0

有几个方法可以做到你想要什么,但使用的ImageView您所描述的方法是不是其中之一。一种可能是ImageView显示动画Drawable。然后,您可以专注于让Drawable实现绘制圆圈。另一种方法是在每次要更改图像并将ImageView设置为显示新的位图时创建一个新的位图。但是,通常的做法是创建一个自定义的View子类。示例项目API Demos有一个自定义视图的示例,您可以在Google中找到大量教程。

26

我有同样的挑战,并得出结论认为覆写onDraw将至少在一般情况下不起作用。 My blog解释原因。对我而言非常有效的是:

  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)); 
+0

谢谢,工作正常 – Lilo 2013-05-24 16:40:49

+5

只是一个建议,因为我遇到了同样的问题,而不是创建一个新的BitmapDrawable,我们也可以利用myImageView.setImageBitmap(tempBitmap)函数! – balachandarkm 2014-12-05 12:24:07

20
 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); 
2

我认为,更好的方法是创建一个自定义的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"/> 

,然后访问是在你的代码像任何其他视图通过使用:

customView = (CustomView) findViewById(R.id.customView);