如何在绘画时将旧位图显示到DrawingView中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在绘画时将旧位图显示到DrawingView中相关的知识,希望对你有一定的参考价值。

我想编辑我的绘画图像。我得到一个位图图像并将其设置为画布位图,但旧图像没有出现。我尝试过使用drawingView.refresh()recycle()方法,但无法使其正常工作。

    private DrawingView mDrawingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing);

        if(datadtlimgsItem !=null && datadtlimgsItem.getImageStr() 
        !=null) 
            decodedString = Base64.decode(datadtlimgsItem.getImageStr(), 
            Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, 
            decodedString.length);
            decodedByte = Util.resize(decodedByte, 400, 100);
            mDrawingView.canvasBitmap = decodedByte;
       
    

public class DrawingView extends View

    // To hold the path that will be drawn.
    private Path drawPath;
    // Paint object to draw drawPath and drawCanvas.
    private Paint drawPaint, canvasPaint;
    // initial color
    private int paintColor = 0xff000000;
    private int previousColor = paintColor;
    // canvas on which drawing takes place.
    private Canvas drawCanvas;
    // canvas bitmap
    public Bitmap canvasBitmap;
    // Brush stroke width
    private float brushSize, lastBrushSize;
    // To enable and disable erasing mode.
    private boolean erase = false;

    public DrawingView(Context context, AttributeSet attrs)
        super(context, attrs);
        setUpDrawing();
    

    /**
     * Initialize all objects required for drawing here.
     * One time initialization reduces resource consumption.
     */
    private void setUpDrawing()
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        // Making drawing smooth.
        drawPaint.setAntiAlias(true);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

        // Initial brush size is medium.
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPaint.setStrokeWidth(brushSize);
    

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) 
        super.onSizeChanged(w, h, oldw, oldh);

    canvasBitmap = Bitmap.createBitmap(w, h, 
    Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    

    @Override
    protected void onDraw(Canvas canvas) 
        super.onDraw(canvas);

            canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    

    @Override
    public boolean onTouchEvent(MotionEvent event) 
        // X and Y position of user touch.
        float touchX = event.getX();
        float touchY = event.getY();
        // Draw the path according to the touch event taking place.
        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:
                if (erase)
                    drawPaint.setXfermode(new 
    PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                drawPaint.setXfermode(null);
                break;
            default:
                return false;
        

        // invalidate the view so that canvas is redrawn.
        invalidate();
        return true;
    

    public void setColor(String newColor)
        // invalidate the view
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
        previousColor = paintColor;
    

    public void setBrushSize(float newSize)
        float pixelAmount = 
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                newSize, getResources().getDisplayMetrics());
        brushSize=pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    

    public void setLastBrushSize(float lastSize)
        lastBrushSize=lastSize;
    
    public float getLastBrushSize()
        return lastBrushSize;
    

    public void setErase(boolean isErase)
        //set erase true or false
        erase = isErase;
        if(erase) 
            drawPaint.setColor(Color.WHITE);
            //drawPaint.setXfermode(new 
    PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        
        else 
            drawPaint.setColor(previousColor);
            drawPaint.setXfermode(null);
        
    

    public void startNew()
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    

我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。

image is blank but i need to show previous image here

here is saved image want to edit this image

我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。我有一个像Paint这样的视图我希望在当前视图中显示从活动中获取的旧图像,然后编辑我的图像。

答案
 @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) 
        super.onSizeChanged(w, h, oldw, oldh);

        if (canvasBitmap == null)

            canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        
        drawCanvas = new Canvas(canvasBitmap);
    

以上是关于如何在绘画时将旧位图显示到DrawingView中的主要内容,如果未能解决你的问题,请参考以下文章

在运行时将图像加载到对话框图片控件中

使用 Laravel JWT 重新认证时将旧令牌列入黑名单

什么是,为什么我必须在清理时将位图 hdc 或内存 dc 恢复到默认状态? [复制]

Android - 在显示进度时将图像从 URL 加载到 ImageView(不保存图像)

MySQL 通过“on duplicate key update”插入新记录时将旧值增加新值

如何在两个片段之间传递位图? (我正在使用 Android 导航组件)