大家好!
  当我在屏幕上写一些字的时候,怎么才能准确的得到这些路径?
  我现在用的方法是 Activity 的 onTouchEvent(MotionEvent event) 。然后通过 (x,y)坐标计算 Path。    但是我发现这样获得的路径不准确,我明明画了曲线,结果由于画的速度太快,成了直线。原因是 onTouchEvent 有一定的时间间隔,也许我在这段时间里画了很多,但它只记录了少数几个点。  请教如果我想获得精确的路径(不管是触摸还是鼠标事件只有其中之一能实现就行),比如我快速画展了一条曲线,我就能得到曲线上大多数的点,或这条曲线的路径。

解决方案 »

  1.   

    api        demo中有例子,自己去看吧
      

  2.   

    通过onScroll方法可以获得拖拽手势时各个点得坐标,把这些坐标点记录下来就是一个拖拽手势的运行轨迹了。
      

  3.   


    你这学习态度,无语了以后自己多动手,不要指望别人喂
    /*
     * Copyright (C) 2007 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */package com.example.android.apis.graphics;import android.app.Activity;
    import android.content.Context;
    import android.graphics.*;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;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;
            }
        }
        
        private static final int COLOR_MENU_ID = Menu.FIRST;
        private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
        private static final int BLUR_MENU_ID = Menu.FIRST + 2;
        private static final int ERASE_MENU_ID = Menu.FIRST + 3;
        private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            
            menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
            menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
            menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
            menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
            menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');        /****   Is this the mechanism to extend with filter effects?
            Intent intent = new Intent(null, getIntent().getData());
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(
                                  Menu.ALTERNATIVE, 0,
                                  new ComponentName(this, NotesList.class),
                                  null, intent, 0, null);
            *****/
            return true;
        }
        
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            super.onPrepareOptionsMenu(menu);
            return true;
        }
        
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            mPaint.setXfermode(null);
            mPaint.setAlpha(0xFF);        switch (item.getItemId()) {
                case COLOR_MENU_ID:
                    new ColorPickerDialog(this, this, mPaint.getColor()).show();
                    return true;
                case EMBOSS_MENU_ID:
                    if (mPaint.getMaskFilter() != mEmboss) {
                        mPaint.setMaskFilter(mEmboss);
                    } else {
                        mPaint.setMaskFilter(null);
                    }
                    return true;
                case BLUR_MENU_ID:
                    if (mPaint.getMaskFilter() != mBlur) {
                        mPaint.setMaskFilter(mBlur);
                    } else {
                        mPaint.setMaskFilter(null);
                    }
                    return true;
                case ERASE_MENU_ID:
                    mPaint.setXfermode(new PorterDuffXfermode(
                                                            PorterDuff.Mode.CLEAR));
                    return true;
                case SRCATOP_MENU_ID:
                    mPaint.setXfermode(new PorterDuffXfermode(
                                                        PorterDuff.Mode.SRC_ATOP));
                    mPaint.setAlpha(0x80);
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
      

  4.   

    有必要这么说楼主吗,适当的问下又没什么关系,我指一个方向给你,手势(Gesture)!
      

  5.   

    谢谢楼上各位,我昨天找到方法了,不过CSDN上不来。。
    就是在用PATH画贝赛尔曲线的时候把所有点连起来而不是得到一段画一段。
    之前我对这个方法理解有误。其实我用的就是4楼方法的,不过感谢4楼!
      

  6.   

    我觉得还是说明一下我不对的方法:
    每次move事件中都PATH.moveTo() ,其实只需要在down的时候移动就行了,否则就成了画线段。