我正在自定义视图上绘画。然后将绘制的图像转换为位图以进行进一步处理,但是当我这样做时Bitmap bitmap = drawView1.getDrawingCache();
位图为空。为什么它为null。以及如何解决它
public class TrainingActivity extends ActionBarActivity {
private DrawingView drawView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_training);
drawView1 = (DrawingView) findViewById(R.id.drawing1);
}
public void submitButtonClicked(View v) {
Bitmap bitmap = drawView1.getDrawingCache();
}
用于自定义视图的xml文件是
<persistent.DrawingView
android:id="@+id/drawing1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
而且我的DrawingView类是
public class DrawingView extends View {
//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
//private int paintColor = 0xFF660000;
private int paintColor = 0xFF000000;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
private Context mContext;
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setupDrawing();
}
private void setupDrawing() {
//get drawing area setup for interaction
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
/* DisplayMetrics metrics = new DisplayMetrics();
((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
drawCanvas.drawRect(100, height - 100, width - 100, 100, drawPaint);*/
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//view given size
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
int leftx = 70;
int topy = 70;
Paint rectPaint=new Paint();
rectPaint.setColor(Color.BLUE);
rectPaint.setAntiAlias(true);
rectPaint.setStrokeWidth(9);
rectPaint.setStyle(Paint.Style.STROKE);
rectPaint.setStrokeJoin(Paint.Join.ROUND);
rectPaint.setStrokeCap(Paint.Cap.ROUND);
drawCanvas.drawRect(leftx, topy, w-leftx, h-topy, rectPaint);
Rect r=new Rect(leftx, topy, w-topy, h-topy);
drawCanvas.clipRect(r);
}
@Override
protected void onDraw(Canvas canvas) {
//draw view
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
public boolean onTouchEvent(MotionEvent event) {
//detect user touch
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
invalidate();
return true;
}
//for erasing purpose this function will be called
public void setErase(boolean isErase) {
//set erase true or false
boolean erase = isErase;
if (erase) {
//drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
} else {
drawPaint.setXfermode(null);
}
}
}
最佳答案
我了解的是getDrawingCache()方法对View类的大小(宽度和高度)有限制。如果View类太大,则getDrawingCache()仅返回null。因此,解决方案是不使用该方法,而是按以下方式进行操作。
Bitmap bitmap = Bitmap.createBitmap(drawView1.getMeasuredWidth(), drawView1.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
drawView1.draw(bitmapHolder);
// bitmap now contains the data we need! Do whatever with it!.
我按照这个人getDrawingCache() returns null的评论得到了这个答案