Android Open Source - Sketcher-Tab Draw Controller






From Project

Back to project page Sketcher-Tab.

License

The source code is released under:

Apache License

If you think the Android project Sketcher-Tab listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.sketchertab;
//w w  w  .j  av  a2s  . c o  m
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import org.sketchertab.style.StylesFactory;

import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Map;

public class DrawController implements View.OnTouchListener {
    public static final int INIT_BG_COLOR = Color.rgb(255, 250, 232);
    public static final int DEFAULT_OPACITY = 50;
    public static final float DEFAULT_WIDTH = 1;
    public static final int DEFAULT_COLOR = Color.argb(DEFAULT_OPACITY, 0, 0, 0);

    private Style style;
    private final Canvas mCanvas;
    private boolean toDraw = false;
    private Paint mColor = new Paint();
    private Paint bgColor = new Paint();
    private IntBuffer undoSurfaceBuffer;
    private Map<StylesFactory.BrushType, Object> brushData;

    public DrawController(Canvas canvas) {
        setStyle(StylesFactory.getCurrentStyle());
        mCanvas = canvas;
        bgColor.setColor(INIT_BG_COLOR);
        setOpacity(DEFAULT_OPACITY);
        setStrokeWidth(DEFAULT_WIDTH);
        setPaintColor(DEFAULT_COLOR);
    }

    public void draw() {
        if (toDraw)
            style.draw(mCanvas);
    }

    public void setStyle(Style style) {
        toDraw = false;
        style.setColor(mColor.getColor());
        style.setOpacity(mColor.getAlpha());
        style.setStrokeWidth(mColor.getStrokeWidth());
        this.style = style;
    }

    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                try {
                    undoSurfaceBuffer = Sketcher.getInstance().getSurface().copyPixelsToBuffer(undoSurfaceBuffer);
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                    DocumentHistory.getInstance().clear();
                    undoSurfaceBuffer = Sketcher.getInstance().getSurface().copyPixelsToBuffer(undoSurfaceBuffer);
                }
                brushData = new HashMap<StylesFactory.BrushType, Object>();
                StylesFactory.saveState(brushData);
                toDraw = true;
                style.strokeStart(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                style.stroke(mCanvas, event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                HistoryItem item = new HistoryItem(undoSurfaceBuffer, brushData);
                DocumentHistory.getInstance().pushNewItem(item);
                Sketcher.getInstance().invalidateOptionsMenu();
                break;
        }
        return true;
    }

    public IntBuffer getUndoSurfaceBuffer() {
        return undoSurfaceBuffer;
    }

    public void setUndoSurfaceBuffer(IntBuffer buffer) {
        undoSurfaceBuffer = buffer;
    }

    public void clear() {
        toDraw = false;
        StylesFactory.clearCache();
        setStyle(StylesFactory.getCurrentStyle());

        brushData = new HashMap<StylesFactory.BrushType, Object>();
        StylesFactory.saveState(brushData);
        HistoryItem item = new HistoryItem(undoSurfaceBuffer, brushData);
        DocumentHistory.getInstance().pushNewItem(item);
    }

    public void setPaintColor(int color) {
        mColor.setColor(color);
        style.setColor(color);
    }

    public int getPaintColor() {
        return mColor.getColor();
    }

    public void setBackgroundColor(int color) {
        bgColor.setColor(color);
    }

    public int getBackgroundColor() {
        return bgColor.getColor();
    }

    public void setOpacity(int opacity) {
        mColor.setAlpha(opacity);
        style.setOpacity(opacity);
    }

    public int getOpacity() {
        return mColor.getAlpha();
    }

    public void setStrokeWidth(float width) {
        mColor.setStrokeWidth(width);
        style.setStrokeWidth(width);
    }

    public float getStrokeWidth() {
        return mColor.getStrokeWidth();
    }
}




Java Source Code List

org.sketchertab.AboutDialog.java
org.sketchertab.BrushProperties.java
org.sketchertab.DocumentHistory.java
org.sketchertab.DrawController.java
org.sketchertab.FileHelper.java
org.sketchertab.HistoryItem.java
org.sketchertab.Sketcher.java
org.sketchertab.Style.java
org.sketchertab.SurfaceDiff.java
org.sketchertab.Surface.java
org.sketchertab.colorpicker.HuePicker.java
org.sketchertab.colorpicker.PickerDialog.java
org.sketchertab.colorpicker.Picker.java
org.sketchertab.colorpicker.PreviewView.java
org.sketchertab.colorpicker.SatValPicker.java
org.sketchertab.colorpicker.Utils.java
org.sketchertab.style.CirclesStyle.java
org.sketchertab.style.FurStyle.java
org.sketchertab.style.RibbonStyle.java
org.sketchertab.style.ShadedStyle.java
org.sketchertab.style.SimpleStyle.java
org.sketchertab.style.SketchyStyle.java
org.sketchertab.style.StyleBrush.java
org.sketchertab.style.StylesFactory.java
org.sketchertab.style.WebStyle.java