Android Open Source - Sketcher-Tab Surface






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  a  va 2 s. c o  m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import org.sketchertab.style.StylesFactory;

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

public final class Surface extends SurfaceView implements Callback {
    private static final String TAG = "Surface";

    private DrawThread drawThread;
    private final Canvas drawCanvas = new Canvas();
    private final DrawController drawController = new DrawController(drawCanvas);
    private Bitmap initialBitmap;
    private Bitmap bitmap;

    public Surface(Context context, AttributeSet attributes) {
        super(context, attributes);

        getHolder().addCallback(this);
        setFocusable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return drawController.onTouch(this, event);
    }

    public void setStyle(Style style) {
        drawController.setStyle(style);
    }

    public DrawThread getDrawThread() {
        if (drawThread == null) {
            drawThread = new DrawThread();
        }
        return drawThread;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(Color.WHITE);
        drawCanvas.setBitmap(bitmap);

        if (initialBitmap != null) {
            drawCanvas.drawBitmap(initialBitmap, 0, 0, null);
            initialBitmap = null;
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        getDrawThread().start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        getDrawThread().stopDrawing();
        while (true) {
            try {
                getDrawThread().join();
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        drawThread = null;
    }

    public void clearBitmap() {
        drawController.setUndoSurfaceBuffer(copyPixelsToBuffer(drawController.getUndoSurfaceBuffer()));

        bitmap.eraseColor(drawController.getBackgroundColor());
        drawController.clear();
    }

    public void setPaintColor(int color) {
        drawController.setPaintColor(color);
    }

    public int getPaintColor() {
        return drawController.getPaintColor();
    }

    public void setOpacity(int opacity) {
        drawController.setOpacity(opacity);
    }

    public int getOpacity() {
        return drawController.getOpacity();
    }

    public void setStrokeWidth(float width) {
        drawController.setStrokeWidth(width);
    }

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

    public void setBackgroundColor(int color) {
        drawController.setBackgroundColor(color);
    }

    public int getBackgroundColor() {
        return drawController.getBackgroundColor();
    }

    public void setInitialBitmap(Bitmap initialBitmap) {
        this.initialBitmap = initialBitmap;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public IntBuffer copyPixelsToBuffer(IntBuffer buffer) {
        if (null == buffer)
            buffer = IntBuffer.allocate(bitmap.getHeight() * bitmap.getWidth());
        else
            buffer.clear();
        bitmap.copyPixelsToBuffer(buffer);
        return buffer;
    }

    public final class DrawThread extends Thread {
        private boolean mRun = true;
        private boolean mPause = false;

        @Override
        public void run() {
            waitForBitmap();

            final SurfaceHolder surfaceHolder = getHolder();
            Canvas canvas = null;

            while (mRun) {
                try {
                    while (mRun && mPause) {
                        Thread.sleep(100);
                    }

                    canvas = surfaceHolder.lockCanvas();
                    if (canvas == null) {
                        break;
                    }

                    synchronized (surfaceHolder) {
                        drawController.draw();
                        canvas.drawBitmap(bitmap, 0, 0, null);
                    }

                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (canvas != null) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }

        private void waitForBitmap() {
            while (bitmap == null) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void stopDrawing() {
            mRun = false;
        }

        public void pauseDrawing() {
            mPause = true;
        }

        public void resumeDrawing() {
            mPause = false;
        }
    }
}




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