使用canvas作为GestureOverlayView

时间:2011-04-21 05:28:11

标签: android bitmap gesture

我有一个GestureOverlayView,用户可以在其上绘制手势,我想将这些笔划保存在PNG文件中。 我已经尝试了public void draw (Canvas canvas),我得到了一个PNG文件作为我以下代码的输出,但我无法得到任何笔画,任何人都可以帮助我.... 提前谢谢。

我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.create_gesture);

    bitmap= Bitmap.createBitmap(12, 12, Bitmap.Config.RGB_565);
    can=new Canvas(bitmap);
    GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
    overlay.addOnGestureListener(new GesturesProcessor());
    overlay.draw(can);

}

将笔划保存为PNG的代码

          bm=mGesture.toBitmap(313, 343, 8,3);
           can.drawBitmap(bm, 0, 0, null);

           File file = new File(Environment.getExternalStorageDirectory()
                             + File.separator+"Strokes");

                      out = new FileOutputStream(file);

                     bm.compress(Bitmap.CompressFormat.PNG,100, out);
                     Toast.makeText(this, "PNG file Created "+mGesture, Toast.LENGTH_LONG).show();

Dr.J

指定的修改
 Paint paint=new Paint();
 Bitmap bitmap=Bitmap.createBitmap(412, 412, Bitmap.Config.ARGB_4444);
 Canvas can=new Canvas(bitmap);
 Path path = new Path();
 paint.setColor(255);

。 。 。

 private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
 public void onGestureEnded(final GestureOverlayView overlay, MotionEvent event) {

        new Thread(new Runnable() {
            public void run() {

            mGesture = overlay.getGesture();

            try{

              path=overlay.getGesturePath();

              can.drawPath(path, paint);

              can.drawBitmap(bitmap, 0, 0, null);
              File file = new File(Environment.getExternalStorageDirectory()
                                  + File.separator+"Strokes");

                           out = new FileOutputStream(file);

                           bitmap.compress(Bitmap.CompressFormat.PNG,100, out);
                         Toast.makeText(getBaseContext(), "Created gesture 12:56 section active "+mGesture, Toast.LENGTH_LONG).show();

            }catch(Exception exp){exp.printStackTrace();}

              }
      }).start();

}

1 个答案:

答案 0 :(得分:0)

你需要直接从用户那里捕捉手势,从Draw(Canvas画布)给你的Canvas是你可以画的,它不包含你要捕捉的数据。

您应该使用:

   public Path getGesturePath (Path path)

然后,如果你想保存它,请使用Canvas的drawPath AndDev Docs

. It can be drawn with canvas.drawPath(path, paint), either filled or stroked (based on the paint's Style), or it can be used for clipping or to draw text on a path.