ImageView在视图类中不可触摸当试图在其上绘制时

时间:2015-06-04 09:54:26

标签: android canvas view ontouchlistener ondraw

ImageView在视图类中是不可触摸的当试图绘制它时...我的问题是,当我尝试绘制imageview我无法绘制它时...我试了很多..不能这样做..... 声明iv.setOnTouchListener(this);仍然不可触摸......你的帮助将不胜感激,谢谢!

public class BrushView extends View implements OnTouchListener {
        private Paint brush = new Paint();
        private Path path = new Path();
        public LayoutParams params;
        ImageView iv;

        public BrushView(Context context) {
            super(context);
            brush.setAntiAlias(true);
            brush.setColor(Color.BLUE);
            brush.setStyle(Paint.Style.STROKE);
            brush.setStrokeJoin(Paint.Join.ROUND);
            brush.setStrokeWidth(15f);
            iv = new ImageView(context);
            iv.setOnTouchListener(this);
            iv.setBackgroundResource(R.drawable.ic_launcher);
            params = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(params);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawPath(path, brush);
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float pointX = event.getX();
            float pointY = event.getY();

            // Checks for the event that occurs
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(pointX, pointY);

                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(pointX, pointY);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
            }
            // Force a view to draw.
            postInvalidate();
            return false;

        }
    }

0 个答案:

没有答案