如何在图像上绘画并保存

how to paint over the image and save it

我已经构建了一个自定义视图来在位图上绘制问题是当我尝试保存或共享图像时只保存了原始位图而没有在其上绘制线条,这是我用于自定义视图的代码和用于保存图像并将视图设置为图像视图。

这是我的自定义视图:

public class DrawView extends android.support.v7.widget.AppCompatImageView {
    private ArrayList<ColouredPoint> paths ;
    private ColouredPoint mPath;
    private Paint mPaint;
    private static final float TOUCH_TOLERANCE = 4;
    boolean erase;
    // Current used colour
    private int mCurrColour;
    public void setErase(boolean era){
        erase=era;
    }
    public void setColor(int colour) {
        mCurrColour = colour;
    }

    public DrawView(Context context) {
        super(context);
        init();
    }

    // XML constructor
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paths = new ArrayList<>();
        mPaint = new Paint();
        mPath = new ColouredPoint(mCurrColour);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(15);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

    }
    private float mX, mY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Capture a reference to each touch for drawing
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath = new ColouredPoint(mCurrColour);
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                    mX = x;
                    mY = y;
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                mPath.lineTo(mX, mY);
                paths.add(mPath);
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas c) {
        // Let the image be drawn first
        super.onDraw(c);
        // Draw your custom points here
        if(!erase){

            for (ColouredPoint i:paths) {
                mPaint.setColor(i.colour);
                c.drawPath(i, mPaint);}
            mPaint.setColor(mCurrColour);
            c.drawPath(mPath,mPaint);}
        else if (paths.size()>0){
            paths.remove(paths.size()-1);} }
    /**
     * Class to store the coordinate and the colour of the point.
     */
    private class ColouredPoint extends Path{
        int colour;
        public ColouredPoint(int colour) {this.colour = colour;}}}

我在主程序中调用它 activity :

im = (DrawView) findViewById(R.id.imageView); im.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        DrawView mcustomImagview = (DrawView) v;
        mcustomImagview.invalidate();
        mcustomImagview.setColor(color);
        mcustomImagview.setErase(value);

        if (v.onTouchEvent(event)) {

            // Do something with event.getX(), event.getY()
        }
        return true;
    }
});

我用这个代码保存图像:

Bitmap b2 = ((BitmapDrawable)im.getDrawable()).getBitmap();
                MediaStore.Images.Media.insertImage(getContentResolver(), b2, "title",null);
                Toast.makeText(getBaseContext(),"Saved",Toast.LENGTH_LONG).show();

尝试以下方法 保存图像:

im .setDrawingCacheEnabled(true);
            im.buildDrawingCache();
            Bitmap bm = im.getDrawingCache();
            SaveImage(bm);

将图像保存到本地存储

 private void SaveImage(Bitmap finalBitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int randomNumber = 10000;
    randomNumber = generator.nextInt(n);
    String fname = "AppName-" + randomNumber + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        final Uri contentUri = Uri.fromFile(file);
        scanIntent.setData(contentUri);
        sendBroadcast(scanIntent);
    } else {
        final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
        sendBroadcast(intent);
    }
}

并在清单中

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />