梅特勒杯创新大赛(五):自定义ImageView-手控标记点

来源:互联网 发布:淘宝判定为广告的评价 编辑:程序博客网 时间:2024/05/22 07:01

1.自定义onDraw()方法

首先我们继承自ImageView并需要重写这个方法,我们的项目需求是通过对ImageView所展示的图片进行点的标记,即通过触摸屏幕进行标点动作,首先在onDraw()方法里面我们根据类的坐标参数进行O,X,Y,Z四个点的绘制,同时O和另外三个点通过线进行连接,代码如下:

  @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //绘制三个标志点        // 创建画笔        Paint p = new Paint();        p.setColor(Color.RED);// 设置红色点        p.setStrokeWidth(15);        for (int i = 0; i < 4; i++) {            if (i == 0){                //绘制O点(X Y 坐标)                canvas.drawPoint(mOLocation[0],mOLocation[1],p);            }            if (i == 1){                //绘制X点(X Y 坐标)                canvas.drawPoint(mXLocation[0],mXLocation[1],p);            }            if (i == 2){                //绘制Y点(X Y 坐标)                canvas.drawPoint(mYLocation[0],mYLocation[1],p);            }            if (i == 3){                //绘制Z点(X Y 坐标)                canvas.drawPoint(mZLocation[0],mZLocation[1],p);            }        }        //绘制总共三根线,o-x,y,z的-蓝色        p.setColor(Color.BLUE);        p.setStrokeWidth(10);        canvas.drawLine(mOLocation[0],mOLocation[1],mXLocation[0],mXLocation[1],p);        canvas.drawLine(mOLocation[0],mOLocation[1],mYLocation[0],mYLocation[1],p);        canvas.drawLine(mOLocation[0],mOLocation[1],mZLocation[0],mZLocation[1],p);    }

循环这里可以再改进一下,mLocation存储的是对应点的X和Y坐标。

2.添加到xml布局文件

<com.handsome.robot.Activity.MeasureView        android:id="@+id/iv_photo_measure"        android:layout_width="220dp"        android:layout_height="180dp"        android:layout_gravity="center"        android:layout_marginTop="3dp"/>

没有什么特别的,注意在写自定义View的类的时候,三个构造函数都要写上去。

public MeasureView(Context context) {        super(context);    }    public MeasureView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MeasureView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }

3.添加onTouch监听器

这里直接添加代码

 imagePhoto.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                switch (motionEvent.getAction()) {                    case MotionEvent.ACTION_DOWN:                        switch (addPointNumber) {                            case 0:                                pointO_xy[0] = motionEvent.getX();                                pointO_xy[1] = motionEvent.getY();                                imagePhoto.setmOLocation(pointO_xy);                                imagePhoto.invalidate();                                break;                            case 1:                                pointX_xy[0] = motionEvent.getX();                                pointX_xy[1] = motionEvent.getY();                                imagePhoto.setmXLocation(pointX_xy);                                imagePhoto.invalidate();                                break;                            case 2:                                pointY_xy[0] = motionEvent.getX();                                pointY_xy[1] = motionEvent.getY();                                imagePhoto.setmYLocation(pointY_xy);                                imagePhoto.invalidate();                                break;                            case 3:                                pointZ_xy[0] = motionEvent.getX();                                pointZ_xy[1] = motionEvent.getY();                                imagePhoto.setmZLocation(pointZ_xy);                                imagePhoto.invalidate();                                break;                        }                        break;                    case MotionEvent.ACTION_UP:                        break;                }                return true;            }        });

这里通过addPointNumber来指定移动哪个点(用户定义),之后进入对应的case里面调用自定义ImageView的set方法进行坐标修改并invalidate()进行onDraw()方法。

以上就是带有手控标记点的ImageView的使用了,通过ImageView进行图片的显示,然后用户便可以方便地进行自助标记了。