2

そこから拡張されたビューがEditTextあり、ユーザーはテキストを書き、同じビューで指で描画できます。

私が欲しいのは、将来の使用のためにそのアイテムを保存することです。アプリが破棄されてから、ドローが削除されたときに考えています。私が欲しいのは、ユーザーが次にアプリを開いたときにこの図面を保存する方法です。

sharedPreferencesにテキストを保存するために使用したテキストについてはonPause、描画については保存方法がわかりません。

ドローを画像ファイルとして保存するのではなく、保存する方法だったと思いますが、わかりません。

編集:

ビットマップを jpeg として保存してから、キャンバスに描画してみました。しかし、それは機能しません。最後に、オブジェクトが黒色で表示され、描画できません。

私のコードは次のとおりです。最後に、私が作成した2つのメソッドが書かれていますが、うまく機能しません。それらは次saveBitmap()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