无法在绘图视图中执行撤消/重做操作

时间:2014-09-19 08:20:29

标签: android canvas

我想在我的绘图应用程序中执行撤消/重做操作,但它仍然无法正常工作。

我知道关于这个话题有很多线索,但我尝试了很多。所以请不要将此主题标记为重复。

以下是执行绘图的代码:

public class Drawing extends View {
    private Path mPath;
    private static Canvas canvas;
    public static Paint mPaint;
    private static Paint mBitmapPaint;
    public static Bitmap mBitmap;
    private BlurMaskFilter mBlur;
    private MaskFilter mEmboss;

    public static String drawKreis;
    public static int radius;
    public static String kreisFarbe;
    public static String hintergrundFarbe;

    private int paintColor = 0xFF660000;

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    public static String strichFarbeHex;

    public static int stroke = 12;
    /*
     * ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
     * 
     * ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
     * 
     * // ArrayList<Pair<Rect, Paint>> rectPaths = new ArrayList<Pair<Rect, Paint>>();
     */

    public ArrayList<Path> paths = new ArrayList<Path>();
    public ArrayList<Paint> paints = new ArrayList<Paint>();
    public ArrayList<Path> undonePaths = new ArrayList<Path>();
    public ArrayList<Paint> undonePaints = new ArrayList<Paint>();

    // private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();

    public Drawing(final Context context, AttributeSet attrs) {

        super(context, attrs);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeWidth(stroke);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(paintColor);
        mPath = new Path();

        mPaint.setXfermode(null);

        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        // paths.add(new Pair<Path, Paint>(mPath, mPaint));

        this.setDrawingCacheEnabled(true);

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

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

        this.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                Log.d("onTouch", "onTouch");
                Log.d("x", String.valueOf(x));
                Log.d("y", String.valueOf(y));

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("a", "down");
                        Log.d("hier", "hier1");
                        touch_start(x, y, context);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d("onTouch", "onTouch2");
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("onTouch", "onTouch3");
                        touch_up();
                        invalidate();
                        break;
                }
                return true;
            }
        });
    }

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

    private void touch_start(float x, float y, Context context) {
        undonePaths.clear();
        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
        canvas.drawPath(mPath, mPaint);

        paths.add(mPath);
        mPath = new Path();

    }

    public static int getStroke() {
        return stroke;

    }

    @Override
    public void onDraw(Canvas c) {

        int strichdicke = OpenProject.getStrichdicke();
        int durchsichtigkeitswert = OpenProject.getDurchsichtigkeit() * 255 / 100;

        Log.d("strichdicke", "strichdicke " + strichdicke);

        for (/* Pair<Path, Paint> p */Path p : paths) {

            if (strichdicke == 0) {
                mPaint.setStrokeWidth(stroke);
            } else {
                mPaint.setStrokeWidth(strichdicke);
            }
            mPaint.setAlpha(durchsichtigkeitswert);

            c.drawPath(p, mPaint);

        }

        c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        c.drawPath(mPath, mPaint);

    }

    @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);
        canvas = new Canvas(mBitmap);

        if (hintergrundFarbe != null) {
            mBitmap.eraseColor(Color.parseColor(hintergrundFarbe));

        } else {
            mBitmap.eraseColor(Color.WHITE);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // wenn die Zurücktaste das erste Mal gedrückt wurde
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return true;
    }

    public void onClickUndo() {
        Log.d("onClickUndo5", "onClickUndo5 " + paths.size());
        if (paths.size() > 0) {
            Log.d("onClickUndo", "onClickUndo " + paths.size());
            undonePaths.add(this.paths.remove(this.paths.size() - 1));
            clearCanvasCache();
            this.invalidate();
            Log.d("onClickUndo1", "onClickUndo1 " + paths.size());
        } else {

        }
    }

    public static String setStrichFarbe(String strichFarbe) {

        strichFarbeHex = strichFarbe;
        Log.d("strichFarbeHex", "strichFarbeHex " + strichFarbeHex);
        return strichFarbeHex;
    }

    // update color
    public void setColor(String newColor) {
        Log.d("setColor", "setColor " + newColor);
        invalidate();
        paintColor = Color.parseColor(newColor);
        Log.d("paintColor", "paintColor " + paintColor);
        mPaint.setColor(paintColor);
        int color = mPaint.getColor();
        Log.d("color", "color " + color);
    }

    public void drawKreis(Drawing drawing) {
        invalidate();
        drawing.canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        drawing.canvas.drawCircle(30, 30, 5, mPaint);
        invalidate();
        Log.d("drawKreis", "drawKreis " + canvas + "|" + mPaint.getColor());
    }

    public static void erase() {
        // mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mBitmap.eraseColor(Color.TRANSPARENT);
        canvas = new Canvas(mBitmap);

    }

    public static void startErasing() {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    public String getStrichFarbe() {
        return strichFarbeHex;
    }

    public void clearCanvasCache() {

        mBitmap = Bitmap.createBitmap((int) 400, (int) 400, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mBitmap);
    }

}

非常感谢您的帮助

0 个答案:

没有答案