我的要求就是程序类似一个画板,利用触屏的方式可以在上面画线什么的,先说最简单的画条线和画一个点吧~~~~
我知道要创建自己的View,还有会用到两个方法:onDraw方法和onTouchEvent方法,但是我不知道怎么把这两个方法联系起来,实现手触画图的效果。各位大虾帮帮小弟了,谢谢!!!!!如果有谁开发过类似的程序,麻烦给我讲讲思路。或者加我的QQ:379065019(麻烦加的时候注明一下)
很愿意和各路android爱好者交朋友。

解决方案 »

  1.   

    apidemo中的fingerprint例子就是你想要的
      

  2.   

    路径apidemo/com.example.android.apis.graphics/FingerPaint.java
      

  3.   

    这个你更应该看源代码了,总共才200行的代码,其中用来画图的才只有100行。
    是通过canvas.drawPath()函数画的路径,当按下时,开始画,一直到抬起。
    核心代码如下:
    public class FingerPaint extends GraphicsActivity
            implements ColorPickerDialog.OnColorChangedListener {        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView(this));        mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFFFF0000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(12);
            
            mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                           0.4f, 6, 3.5f);        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        }
        
        private Paint       mPaint;
        private MaskFilter  mEmboss;
        private MaskFilter  mBlur;
        
        public void colorChanged(int color) {
            mPaint.setColor(color);
        }    public class MyView extends View {
            
            private static final float MINP = 0.25f;
            private static final float MAXP = 0.75f;
            
            private Bitmap  mBitmap;
            private Canvas  mCanvas;
            private Path    mPath;
            private Paint   mBitmapPaint;
            
            public MyView(Context c) {
                super(c);
                
                mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(mBitmap);
                mPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            }        @Override
            protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
            }
            
            @Override
            protected void onDraw(Canvas canvas) {
                canvas.drawColor(0xFFAAAAAA);
                
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                
                canvas.drawPath(mPath, mPaint);
            }
            
            private float mX, mY;
            private static final float TOUCH_TOLERANCE = 4;
            
            private void touch_start(float x, float y) {
                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
                mCanvas.drawPath(mPath, mPaint);
                // kill this so we don't double draw
                mPath.reset();
            }
            
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        invalidate();
                        break;
                }
                return true;
            }
        }