空位图:View.getDrawingCache()给出Null位图

时间:2015-06-29 12:41:47

标签: android view android-studio bitmap

我正在绘制自定义视图。然后将绘制的图像转换为位图以供进一步处理,但是当我这样做时 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);
        }

    }


}

1 个答案:

答案 0 :(得分:2)

我的理解是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

给出的评论得到了这个答案