2

I have a view that extends from EditText that allows the user to write a text and then draw with the finger in the same view.

What I want is to save that item for a future use, I'm thinking on when the app is destroyed and then opened the draws are removed. What I want is a way to save this drawing for the next time that the user opens the app.

For the text I used sharedPreferences that save the text in the onPause, and for the draw I don't know how can be saved.

I think that saving the draw as an image file not was a way to save it, but I don't know.

EDIT:

I tried saving the Bitmap as a jpeg and then drawing to the canvas. But it don't work, at the end, it shows the object in color black and don't allow me to draw.

My code is the following, at the end are written the two methods that I created, but that they don't work well. They are saveBitmap() and openBitmap():

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.*;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

public class DrawEditText extends EditText {

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    private Paint   mPaint;
    private MaskFilter  mEmboss;
    private MaskFilter  mBlur;
    private int brush;        
    private Boolean drawing;
    private int maxLines;

    public DrawEditText(Context c) {
        super(c);


        //This initializes all the objects
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setColor(Color.TRANSPARENT);

        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        drawing = false;
        brush = -1;
        maxLines = 20;
    }
    public DrawEditText(Context c, AttributeSet attrs) {
        super(c, attrs);

        //This initializes all the objects
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        drawing = false;
        brush = -1;
        maxLines = 20;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
        super.onDraw(canvas);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        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;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        //This conditional makes that if not it's drawing no points are saved and no points are drawed
        if (drawing){
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        } else {
            return super.onTouchEvent(event);
        }
    }

    public void changePaint(int stroke, int color){
        mPaint.setColor(color);
        mPaint.setStrokeWidth(stroke);
    }
    public void clear(){
        mCanvas.drawColor(0x00AAAAAA);
        mCanvas.drawColor( 0, PorterDuff.Mode.CLEAR );
        setText("");
    }
    public void changeBrush(int id){
        //String[] ids={"emboss", "blur", "another"};
        switch (id) {
        case 0:
            mPaint.setMaskFilter(mEmboss);
            brush = 0;
            break;
        case 1:
            mPaint.setMaskFilter(mBlur);
            brush = 1;
            break;
        case 2:
            mPaint.setMaskFilter(null); 
            brush = -1;
            break;
        default:
            mPaint.setMaskFilter(null);
            brush = -1;
            break;
        }
    }
    public int getBrush(){
        //String[] ids={"emboss", "blur", "another"};
        return brush;
    }
    public void eraser(){
        mPaint.setMaskFilter(null); 
        mPaint.setColor(0x00AAAAAA);
    }

    public void setDrawing(Boolean drawing){
        this.drawing = drawing;
    }

    public Boolean isDrawing(){
        return drawing;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        TextWatcher watcher = new TextWatcher() {

            private String text;
            private int beforeCursorPosition = 0;

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                text = s.toString();
                beforeCursorPosition = start;
            }

            @Override
            public void afterTextChanged(Editable s) {

                /* turning off listener */
                removeTextChangedListener(this);

                /* handling lines limit exceed */
                if (DrawEditText.this.getLineCount() > maxLines) {
                    DrawEditText.this.setText(text);
                    DrawEditText.this.setSelection(beforeCursorPosition);
                }

                /* turning on listener */
                addTextChangedListener(this);

            }
        };

        this.addTextChangedListener(watcher);
    }

    @Override
    public int getMaxLines() {
        return maxLines;
    }

    @Override
    public void setMaxLines(int maxLines) {
        this.maxLines = maxLines;
    }

    public void saveBitmap(){
        mCanvas.setBitmap(mBitmap);
        File sd = Environment.getExternalStorageDirectory();
        File myDir = new File(sd + "/tweeet");    
        myDir.mkdirs();
        File f = new File(myDir, ".drawing.jpeg");
        try {
            if (myDir.canWrite()) {
                 f.createNewFile();
                 OutputStream os = new FileOutputStream(f);
                 mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                 os.close();
            }
       } catch (FileNotFoundException e) {
            e.printStackTrace();
       } catch (IOException e) {
            e.printStackTrace();
       }
    }
    public void openBitmap(){
        File sd = Environment.getExternalStorageDirectory();
        File myDir = new File(sd + "/tweeet");    
        myDir.mkdirs();
        File f = new File(myDir, ".drawing.jpeg");
        mBitmap = BitmapFactory.decodeFile(f.getPath());
    }
}
4

3 回答 3

2
  1. 将图像从画布保存到文件。这是如何做到的。
  2. onResume()加载图像文件并将其绘制在画布上。
于 2013-10-20T13:42:40.107 回答
1

You can save the canvas as a bitmap. Try this:

1.Create a bitmap of the correct size using Bitmap.createBitmap()
2.Set canvas to point to the bitmap
3.Draw to the canvas
4.Use the bitmap

Bitmap bitmap = Bitmap.createBitmap(width, height. Config.RGB_565));  
canvas.setBitmap(bitmap);  
...

Then save the bitmap as a file and you can load it later when you need it.

于 2013-10-20T13:44:05.220 回答
1

“然后我将位图保存到文件中,或者我可以将其保存为对象?”<br/> -- 您应该将位图保存到像 *.png 这样的图像文件中

于 2013-10-20T14:54:07.873 回答