我正在寻找在xml布局中间显示画布的任何方法,即在Imageview上显示画布。我试过但位图返回null。
Bitmap b = Bitmap.createBitmap(img.getMeasuredWidth(),img.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawRect(0, 0, 200, 200, paint);
答案 0 :(得分:0)
请将ImageView子类化,并在onDraw()中绘制您的要求,如下所示。
public class CustomImageView extends ImageView{
public CustomImageView (Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap Bitmap.createBitmap(img.getMeasuredWidth(),img.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawRect(0, 0, 200, 200, paint);
}
}
答案 1 :(得分:0)
使用此代码创建位图。
public static Bitmap createScaledBitmap(String path, float scale, boolean filtering){
Bitmap src = BitmapFactory.decodeFile(path);
int width = (int)( src.getWidth() * scale + 0.5f);
int height = (int)( src.getHeight() * scale + 0.5f);
return Bitmap.createScaledBitmap(src, width, height, filtering);
}
此处path是图像的路径,然后最终将此缩放位图放在Canvas中。