2011年4月20日 星期三

FingerPaint功能

FingerPaint功能
下面為一繪圖的範例
    public class Project1 extends Activity {
        private Bitmap  mBitmap;
        private MyView mView;

        public void onCreate (Bundle bundle) {
            super.onCreate(bundle);
            // remove the title, and display "full screen"
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // get the windows resolution, for bitmap use
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            // create a new bitmap for paint
            mBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.ARGB_8888);
            // use "MyView" class, this is define by author
            mView = new MyView(this);
            setContentView(mView);
        }

        private class MyView extends View {
            private Canvas mCanvas;
            private Path mPath;
            private Paint mBitmapPaint;
            private Paint mPaint;

            private float new_x, new_y;
            private float old_x, old_y;

            public MyView(Context context) {
                super(context);
                // create and initial object
                mCanvas = new Canvas(mBitmap);
                mPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        
                mPaint = new Paint();
                // make paint more smooth
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                // (1.FILL 2.FILL_AND_STROKE 3.STROKE)
                mPaint.setStyle(Paint.Style.STROKE);
                // (1.BEVEL 2.MITER 3.ROUND) default is MITER
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                // (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                // if set "0", will be a line
                mPaint.setStrokeWidth(12);
                mPaint.setColor(Color.GREEN);
            }
    
            protected void onDraw(Canvas canvas) {
                // set background color
                canvas.drawColor(Color.WHITE);
                // draw the specified bitmap
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                // the path will be filled or framed based on the Style in the paint
                canvas.drawPath(mPath, mPaint);
            }
 
            public boolean onTouchEvent(MotionEvent event) {
                new_x = event.getX();
                new_y = event.getY();
      
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // save the previous path, then reset the path
                    mPath.reset();
                    // start position
                    mPath.moveTo(new_x, new_y);
                    old_x = new_x;
                    old_y = new_y;
                }
                else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    float dx = Math.abs(new_x - old_x);
                    float dy = Math.abs(new_y - old_y);
          
                    if (dx >= 4 || dy >= 4) {
                        // set path from (old_x, old_y) to ((new_x + old_x)/2, (new_y + old_y)/2)
                        mPath.quadTo(old_x, old_y, (new_x + old_x)/2, (new_y + old_y)/2);
                        old_x = new_x;
                        old_y = new_y;
                    }
                }
                else if (event.getAction() == MotionEvent.ACTION_UP) {
                    // unknown, this line like no used
                    mPath.lineTo(old_x, old_y);
                    // paint the path when "ACTION_UP"
                    mCanvas.drawPath(mPath, mPaint);
                    // save the paint
                    mPath.reset();
                }
      
                // update the paint, it will recall the onDraw function
                invalidate();
                return true;
            }
        }
    }

1 則留言: