Android手势无法正常工作?

时间:2014-02-28 07:26:03

标签: android gesture

我第一次拥有此图片。enter image description here

在我选择第二张图像enter image description here

中显示的身体疼痛后

我的问题是它在整个布局上工作我想只在不在图像外面的图像上绘制或选择怎么做?我想要像here

一样

这是我的.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="180dp"
    android:layout_height="260dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:background="@drawable/front_male"
    android:orientation="vertical" >
</LinearLayout>

</RelativeLayout>

/ *****************************这里的java文件************** *****

 class DrawingView extends View
  {
  Paint       mPaint;
   //MaskFilter  mEmboss;
   //MaskFilter  mBlur;
   Bitmap  mBitmap;
   Canvas  mCanvas;
   Path    mPath;
   Paint   mBitmapPaint;

public DrawingView(Context context) {
  super(context);
 // TODO Auto-generated constructor stub
   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(20);

   mPath = new Path();
   mBitmapPaint = new Paint();
   mBitmapPaint.setColor(Color.RED);
  }
  @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     mCanvas = new Canvas(mBitmap);
   }
  @Override
  public void draw(Canvas canvas) {
   // TODO Auto-generated method stub
   super.draw(canvas);
   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);
   //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
   // kill this so we don't double draw
   mPath.reset();
  // mPath= new Path();
    }

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


    }
  /*****************Activity here*******************

                  public class GesturesActivity extends Activity {
                  private GestureLibrary mLibrary;

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

    DrawingView mDrawingView = new DrawingView(this);

    setContentView(R.layout.main);

    LinearLayout mDrawingPad = (LinearLayout) findViewById(R.id.linearLayout1);
    // ImageView mDrawingPad=(ImageView)findViewById(R.id.view_drawing_pad);

    mDrawingPad.addView(mDrawingView);

  }

     }

0 个答案:

没有答案