@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create and attach the view that is responsible for painting. mView = new PaintView(this); setContentView(mView); mView.requestFocus(); // Restore the fading option if we are being thawed from a // previously saved state. Note that we are not currently remembering // the contents of the bitmap. if (savedInstanceState != null) { mFading = savedInstanceState.getBoolean("fading", true); mView.mColorIndex = savedInstanceState.getInt("color", 0); } else { mFading = true; mView.mColorIndex = 0; } }
private void paint(PaintMode mode, float x, float y, float pressure, float major, float minor, float orientation, float distance, float tilt) { if (mBitmap != null) { if (major <= 0 || minor <= 0) { // If size is not available, use a default value. major = minor = 16; } switch (mode) { case Draw: mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(Math.min((int)(pressure * 128), 255)); drawOval(mCanvas, x, y, major, minor, orientation, mPaint); break; case Erase: mPaint.setColor(BACKGROUND_COLOR); mPaint.setAlpha(Math.min((int)(pressure * 128), 255)); drawOval(mCanvas, x, y, major, minor, orientation, mPaint); break; case Splat: mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(64); drawSplat(mCanvas, x, y, orientation, distance, tilt, mPaint); break; } } mFadeSteps = 0; invalidate(); }
@Override public void handleMessage(Message msg) { switch (msg.what) { // Upon receiving the fade pulse, we have the view perform a // fade and then enqueue a new message to pulse at the desired // next time. case MSG_FADE: { mView.fade(); scheduleFade(); break; } default: super.handleMessage(msg); } } };
advanceColor(); for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { paint(getPaintModeForTool(event.getToolType(j), mode), event.getHistoricalX(j, i), event.getHistoricalY(j, i), paint(getPaintModeForTool(event.getToolType(j), mode), event.getX(j), event.getY(j),
advanceColor(); for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { paint(getPaintModeForTool(event.getToolType(j), mode), event.getHistoricalX(j, i), event.getHistoricalY(j, i), paint(getPaintModeForTool(event.getToolType(j), mode), event.getX(j), event.getY(j),
private void paint(PaintMode mode, float x, float y, float pressure, float major, float minor, float orientation, float distance, float tilt) { if (mBitmap != null) { if (major <= 0 || minor <= 0) { // If size is not available, use a default value. major = minor = 16; } switch (mode) { case Draw: mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(Math.min((int)(pressure * 128), 255)); drawOval(mCanvas, x, y, major, minor, orientation, mPaint); break; case Erase: mPaint.setColor(BACKGROUND_COLOR); mPaint.setAlpha(Math.min((int)(pressure * 128), 255)); drawOval(mCanvas, x, y, major, minor, orientation, mPaint); break; case Splat: mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(64); drawSplat(mCanvas, x, y, orientation, distance, tilt, mPaint); break; } } mFadeSteps = 0; invalidate(); }
public void text(String text) { if (mBitmap != null) { final int width = mBitmap.getWidth(); final int height = mBitmap.getHeight(); mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(255); int size = height; mPaint.setTextSize(size); Rect bounds = new Rect(); mPaint.getTextBounds(text, 0, text.length(), bounds); int twidth = bounds.width(); twidth += (twidth/4); if (twidth > width) { size = (size*width)/twidth; mPaint.setTextSize(size); mPaint.getTextBounds(text, 0, text.length(), bounds); } Paint.FontMetrics fm = mPaint.getFontMetrics(); mCanvas.drawText(text, (width-bounds.width())/2, ((height-size)/2) - fm.ascent, mPaint); mFadeSteps = 0; invalidate(); } }
public void text(String text) { if (mBitmap != null) { final int width = mBitmap.getWidth(); final int height = mBitmap.getHeight(); mPaint.setColor(COLORS[mColorIndex]); mPaint.setAlpha(255); int size = height; mPaint.setTextSize(size); Rect bounds = new Rect(); mPaint.getTextBounds(text, 0, text.length(), bounds); int twidth = bounds.width(); twidth += (twidth/4); if (twidth > width) { size = (size*width)/twidth; mPaint.setTextSize(size); mPaint.getTextBounds(text, 0, text.length(), bounds); } Paint.FontMetrics fm = mPaint.getFontMetrics(); mCanvas.drawText(text, (width-bounds.width())/2, ((height-size)/2) - fm.ascent, mPaint); mFadeSteps = 0; invalidate(); } }
@Override public boolean onTrackballEvent(MotionEvent event) { final int action = event.getActionMasked(); if (action == MotionEvent.ACTION_DOWN) { // Advance color when the trackball button is pressed. advanceColor(); } if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { final int N = event.getHistorySize(); final float scaleX = event.getXPrecision() * TRACKBALL_SCALE; final float scaleY = event.getYPrecision() * TRACKBALL_SCALE; for (int i = 0; i < N; i++) { moveTrackball(event.getHistoricalX(i) * scaleX, event.getHistoricalY(i) * scaleY); } moveTrackball(event.getX() * scaleX, event.getY() * scaleY); } return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case CLEAR_ID: mView.clear(); return true; case FADE_ID: mFading = !mFading; if (mFading) { startFading(); } else { stopFading(); } return true; default: return super.onOptionsItemSelected(item); } }
@Override public boolean onTrackballEvent(MotionEvent event) { final int action = event.getActionMasked(); if (action == MotionEvent.ACTION_DOWN) { // Advance color when the trackball button is pressed. advanceColor(); } if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { final int N = event.getHistorySize(); final float scaleX = event.getXPrecision() * TRACKBALL_SCALE; final float scaleY = event.getYPrecision() * TRACKBALL_SCALE; for (int i = 0; i < N; i++) { moveTrackball(event.getHistoricalX(i) * scaleX, event.getHistoricalY(i) * scaleY); } moveTrackball(event.getX() * scaleX, event.getY() * scaleY); } return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case CLEAR_ID: mView.clear(); return true; case FADE_ID: mFading = !mFading; if (mFading) { startFading(); } else { stopFading(); } return true; default: return super.onOptionsItemSelected(item); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create and attach the view that is responsible for painting. mView = new PaintView(this); setContentView(mView); mView.requestFocus(); // Restore the fading option if we are being thawed from a // previously saved state. Note that we are not currently remembering // the contents of the bitmap. if (savedInstanceState != null) { mFading = savedInstanceState.getBoolean("fading", true); mView.mColorIndex = savedInstanceState.getInt("color", 0); } else { mFading = true; mView.mColorIndex = 0; } }
@Override public void handleMessage(Message msg) { switch (msg.what) { // Upon receiving the fade pulse, we have the view perform a // fade and then enqueue a new message to pulse at the desired // next time. case MSG_FADE: { mView.fade(); scheduleFade(); break; } default: super.handleMessage(msg); } } };
public PaintView(Context c) { super(c); init(); }
public PaintView(Context c, AttributeSet attrs) { super(c, attrs); init(); }
public PaintView(Context c) { super(c); init(); }
public void fade() { if (mCanvas != null && mFadeSteps < MAX_FADE_STEPS) { mCanvas.drawPaint(mFadePaint); invalidate(); mFadeSteps++; } }
public void clear() { if (mCanvas != null) { mPaint.setColor(BACKGROUND_COLOR); mCanvas.drawPaint(mPaint); invalidate(); mFadeSteps = MAX_FADE_STEPS; } }
public PaintView(Context c, AttributeSet attrs) { super(c, attrs); init(); }