Ontouchevent不起作用

时间:2012-10-31 10:38:27

标签: android ontouchevent

Ontouchevent不起作用,我无法弄清楚原因,没有例外或错误,我很确定我的代码是正确的,但是当我触摸屏幕时没有响应。

基本上下面的代码应该将图像设置为画布并允许您在其上绘制/绘制,我已经完成了将图像设置为背景,但它不会做第二部分。

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

public class ImgDisplay extends Activity {
    private String a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_display);

 a =getIntent().getStringExtra(getString(R.string.app_name));
 Toast.makeText(this,a, Toast.LENGTH_LONG).show();

/**

 Uri myUri = Uri.parse(a);
 **/

 ImageView b=(ImageView) findViewById(R.id.image_view);
 b.setImageBitmap(BitmapFactory.decodeFile(a));
 SignatureView sign=new SignatureView(this,null);
sign.invalidate();
    }


    public class SignatureView extends ImageView  {
        Bitmap mBitmap = BitmapFactory.decodeFile(a);
        /** Canvas c = new Canvas(mBitmap);
**/
          private static final float STROKE_WIDTH = 5f;

          /** Need to track this so the dirty region can accommodate the stroke. **/
          private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

          private Paint paint = new Paint();
          private Path path = new Path();

          /**
           * Optimizes painting by invalidating the smallest possible area.
           */
          private float lastTouchX;
          private float lastTouchY;
          private final RectF dirtyRect = new RectF();
        private Context context;

          public SignatureView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context=context;
            setOnTouchListener(this);
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(STROKE_WIDTH);
          }

          /**
           * Erases the signature.
           */
          public void clear() {
            path.reset();

            // Repaints the entire view.
            invalidate();
          }

          @Override
          protected void onDraw(Canvas canvas) {
              canvas.drawBitmap(mBitmap,null,null);
              canvas.drawPath(path, paint);
          }

          @Override
          public boolean onTouchEvent(MotionEvent event) {
            float eventX = event.getX();
            float eventY = event.getY();

            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                lastTouchX = eventX;
                lastTouchY = eventY;
                // There is no end point yet, so don't waste cycles invalidating.
                return true;

              case MotionEvent.ACTION_MOVE:
              case MotionEvent.ACTION_UP:
                // Start tracking the dirty region.
                resetDirtyRect(eventX, eventY);

                // When the hardware tracks events faster than they are delivered, the
                // event will contain a history of those skipped points.
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++) {
                  float historicalX = event.getHistoricalX(i);
                  float historicalY = event.getHistoricalY(i);
                  expandDirtyRect(historicalX, historicalY);
                  path.lineTo(historicalX, historicalY);
                }

                // After replaying history, connect the line to the touch point.
                path.lineTo(eventX, eventY);
                break;

              default:
                debug("Ignored touch event: " + event.toString());
                return false;
            }

            // Include half the stroke width to avoid clipping.
            invalidate(
                (int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

            lastTouchX = eventX;
            lastTouchY = eventY;

            return true;
          }

          private void debug(String string) {
            // TODO Auto-generated method stub

        }

        /**
           * Called when replaying history to ensure the dirty region includes all
           * points.
           */
          private void expandDirtyRect(float historicalX, float historicalY) {
            if (historicalX < dirtyRect.left) {
              dirtyRect.left = historicalX;
            } else if (historicalX > dirtyRect.right) {
              dirtyRect.right = historicalX;
            }
            if (historicalY < dirtyRect.top) {
              dirtyRect.top = historicalY;
            } else if (historicalY > dirtyRect.bottom) {
              dirtyRect.bottom = historicalY;
            }
          }

          /**
           * Resets the dirty region when the motion event occurs.
           */
          private void resetDirtyRect(float eventX, float eventY) {

            // The lastTouchX and lastTouchY were set when the ACTION_DOWN
            // motion event occurred.
            dirtyRect.left = Math.min(lastTouchX, eventX);
            dirtyRect.right = Math.max(lastTouchX, eventX);
            dirtyRect.top = Math.min(lastTouchY, eventY);
            dirtyRect.bottom = Math.max(lastTouchY, eventY);
          }



}
}

3 个答案:

答案 0 :(得分:0)

您的代码非常完美,但您没有在任何地方显示SignatureView

SignatureView sign=new SignatureView(this,null);
sign.invalidate();

你只是初始化那个视图&amp;无效。

需要添加主布局。或setContentView(sign);

答案 1 :(得分:0)

试试这个:

 SignatureView sign=new SignatureView(this,null);
sign=(SignatureView) findViewById(R.id.image_view);
 sign.setImageBitmap(BitmapFactory.decodeFile(a));
sign.invalidate();

应该有效。基本上您没有将签名视图分配给ImageView(R.id.image_view)。改为使用默认的ImageView。

答案 2 :(得分:0)

见这个

  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mPaint.setDither(true);
  mPaint.setColor(Color.GREEN);
  mPaint.setStyle(Paint.Style.STROKE);
  mPaint.setStrokeJoin(Paint.Join.ROUND);
  mPaint.setStrokeCap(Paint.Cap.ROUND);
  mPaint.setStrokeWidth(5);

  relativelayout.addView(mView);

这里是示例绘制自由手和清除油漆http://polamreddyn.blogspot.in/2012/10/free-hand-graw.html