android捕获手势并在画布上绘制,不完整的绘制

时间:2015-06-12 23:08:21

标签: android android-canvas

我正在创建一个应用程序,用户在画布上绘制一个字母,如果绘图是实际字母,则会验证它。我正在使用带有画布的GestureOverlayView来捕捉手势和路径,问题是画布没有绘制我用手做的整个路径/手势。我需要帮助才能找到解决方案。我需要完全绘制路径/手势。

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <android.gesture.GestureOverlayView
         android:id="@+id/gestures"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:eventsInterceptionEnabled="true"
         android:gestureStrokeType="multiple"
         android:layout_alignParentRight="true"
         android:fadeOffset="500"
         android:background="#000000">
              <cohen.projetoTEA.com.DrawLetter
                 android:id="@+id/drawing"
                 android:layout_width="wrap_content"
                 android:layout_alignParentRight="true"
                 android:layout_height="wrap_content"
                 android:background="#FFFFFF" 
                 />
         </android.gesture.GestureOverlayView>
</RelativeLayout>

活动

public class Reconhecimento extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
private final File mStoreFile = new      File(Environment.getExternalStorageDirectory(), "gestures");
    /**
     * Called when the activity is first created.
     */
    @Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.reconhecimento);
   //mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
   mLibrary = GestureLibraries.fromFile(mStoreFile);
   if (!mLibrary.load()) {
     finish();
   }

   GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
   gestures.addOnGesturePerformedListener(this);
   gestures.setGestureVisible(false);
}

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
   ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

   if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
     String result = predictions.get(0).name;
      Toast.makeText(this, result, Toast.LENGTH_LONG).show();
     if ("open".equalsIgnoreCase(result)) {
       Toast.makeText(this, "Opening the document", Toast.LENGTH_LONG).show();
     } else if ("save".equalsIgnoreCase(result)) {
       Toast.makeText(this, "Saving the document", Toast.LENGTH_LONG).show();
     }
   }
}

}

DrawLetter app

public class DrawLetter extends View {
    //drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor = 0xFF660000;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    private Bitmap canvasBitmap;
    private boolean erase=false;
   public DrawLetter(Context context, AttributeSet attrs){
        super(context, attrs);
        setupDrawing();
    }

    private void setupDrawing(){
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(5);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }
    public void setErase(boolean isErase){
        erase=isErase;
        if(erase){ 
            drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            drawPaint.setStrokeWidth(10);
        }
        else{
            drawPaint.setXfermode(null);
            drawPaint.setStrokeWidth(5);
        }
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    //view given size
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }



    @Override
    protected void onDraw(Canvas canvas) {
    //draw view
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    //detect user touch
        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_UP:
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                break;
            default:
                return false;
        }
    invalidate();
    return true;
    }

    public boolean getErase(){ return erase; }
}

1 个答案:

答案 0 :(得分:1)

你可以用另一种方式来做。 喜欢这个

 <android.gesture.GestureOverlayView
  android:id="@+id/g1"
  android:layout_width="fill_parent"
  android:layout_height="200dp"
  android:background="#ffffff"
  android:fadeOffset="999999"
  android:gestureColor="#0000FF"
  android:gestureStrokeType="multiple"
  android:gestureStrokeWidth="5" > 

<android.gesture.GestureOverlayView
  android:id="@+id/g2"
  android:layout_width="fill_parent"
  android:layout_height="200dp"
  android:gestureStrokeType="multiple" 
  android:fadeOffset="001"
  android:gestureColor="#000"
  android:gestureStrokeAngleThreshold="0.0"
  android:gestureStrokeLengthThreshold="0.0"
  android:gestureStrokeSquarenessThreshold="0.0"

   >  
  </android.gesture.GestureOverlayView>
  </android.gesture.GestureOverlayView>

此处GestureOverlayView g2在g1内。

g2用于识别字母,一旦写入字母就会消失。

但是g1不会消退。

如果您不想要这封信,可以清除GestureOverlayView。