0

可能重复:
Android 如何沿着手指画一条平滑线

我使用以下代码创建了布局:

activity_hand_write_demo.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="5dp">

      <LinearLayout android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
    android:background="@drawable/custom_border">
   <TextView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="20dp"
    android:textColor="#FFFFFF"
    android:gravity="center_horizontal"
    android:background="#AAAAAA" />
    </LinearLayout>
</LinearLayout>

可绘制的custom_border.xml

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"               android:shape="rectangle">
  <corners android:radius="20dp"/>
  <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#1ABCD1"/>
 </shape>

HandWriteDemo.java

  package com.example.handwritedemo;
  import android.os.Bundle;
  import android.app.Activity;


     public class HandWriteDemo extends Activity {

 @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_hand_write_demo);

        } 
   }

现在我想在手指移动时画一条线。我被卡住了如何通过触摸监听器或其他方式来实现这一点。

如果有人知道...

4

2 回答 2

0

我就是这样做的:

public MyView(Context c, int width, int height) {
    super(c);

    WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int w = display.getWidth(); // deprecated
    int h = display.getHeight();
    // setFocusable(true);
    // setBackgroundResource(R.drawable.download);

    // setting paint
    mPath = new Path();
    mPaint = new Paint();

    mPaint.setColor(0xFF000000);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    mPaint.setAntiAlias(true);
    mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);

    this.getContext().getResources();

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.smoke);
    Bitmap bm2 = getResizedBitmap(bm, h, w);

    // converting image bitmap into mutable bitmap

    bitmap = bm2.createBitmap(w, h, Config.ARGB_8888);
    mCanvas = new Canvas();
    mCanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
    mCanvas.drawBitmap(bm2, 0, 0, null);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    //mCanvas.drawCircle(x, y, r, mPaint);

    // canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);

    canvas.drawPath(mPath, mPaint);

    super.onDraw(canvas);

}

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);
    // mPath.lineTo(x, y);
    // mCanvas.drawPoint(x, y, mPaint);
    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;
    }
    // invalidate();
}

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;
}

public Bitmap getImage() {
    return mBitmap;
}
}

如果您想在空白页上绘图,只需忽略位图,就可以了。

于 2012-09-06T07:20:28.007 回答
0

我开发了一个应用程序。我在那用过手指画。我给你代码的链接。希望这会有所帮助。

https://github.com/anupam1525/AcrylicPaint/blob/master/Acrylic_Paint/src/anupam/acrylic/EasyPaint.java

于 2012-09-06T07:09:14.700 回答