jiuhaofangyinyue
听音乐@
2016-01-12 12:34

请问android在webview上用canvas绘制四圆点,拖动后原来的点却无法删除呢?

  • webview
  • canvas
  • 对象
  • android

需求是:
点击一个button,点击一次在webview上绘制两个圆点,点击第二次再绘制两个圆点,监听这四个圆点的触摸移动,将原来绘制的点隐藏会删除,请看代码:

button.setonclicklisterner(new View.onclicklistener{
measure_count = ++measure_count;
int result = measure_count % 3;
LogUtil.i("TAG=" + getClass().getSimpleName(), "result=" + result);
switch (result) {
case 0:

                    break;
                case 1:

                    if (DotCanvasView.mCircleInfos != null) {
                        DotCanvasView.mCircleInfos.clear();
                    }
                    dotCanvasMethod(dotOneX, dotOneY, 0);
                    dotCanvasMethod(dotTwoX, dotTwoY, 1);
                    break;
                case 2:
                    if (DotCanvasView.mCircleInfos != null) {
                        DotCanvasView.mCircleInfos.clear();
                    }
                    dotCanvasMethod(dotOneX, dotOneY, 0);
                    dotCanvasMethod(dotTwoX, dotTwoY, 1);

                    dotCanvasMethod(dotThreeX, dotThreeY,2);
                    dotCanvasMethod(dotFourX, dotFourY, 3);
                    break;
                default:
                    break;
            }
                            }
     private void dotCanvasMethod(float dotx, float doty, int tag) {
        view = new DotCanvasView(this);
        dotInfo = new DotCanvasView.CircleInfo();
        dotInfo.setX(dotx);
        dotInfo.setY(doty);
        dotInfo.setTag(tag);
        DotCanvasView.mCircleInfos.add(dotInfo);
        view.setMinimumHeight(40);
        view.setMinimumWidth(40);
        //通知view组件重绘
        view.invalidate();
        frame_layout.addView(view);
    }

//圆点绘制类
public class DotCanvasView extends View {

public DotCanvasView(Context context) {
    super(context);
}

//  保存绘制历史
public static List<CircleInfo> mCircleInfos = new ArrayList<CircleInfo>();

// 保存实心圆相关信息的类
public static class CircleInfo {
    private float x;                //  圆心横坐标
    private float y;                //  圆心纵坐标
    private int tag; //实心圆的标示

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }
}

//  当画布重绘时调用该方法,Canvas表示画布对象,可以在该对象上绘制基本的图形
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    //  设置画笔颜色
    canvas.save();
    paint.setColor(Color.GREEN);
    //  根据保存的绘制历史重绘所有的实心圆
    for (CircleInfo circleInfo : mCircleInfos) {
        //  绘制实心圆
        canvas.drawCircle(circleInfo.getX(), circleInfo.getY(), 20, paint);
        canvas.save();
    }
    LogUtil.i("TAG=" + getClass().getSimpleName(), "saveCount=" + canvas.getSaveCount());
    if (canvas.getSaveCount() >1) {
        canvas.restoreToCount(1);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    LogUtil.i("TAG=" + getClass().getSimpleName(), "currentX=" + currentX + "currentY=" + currentY);
    if (mCircleInfos != null) {

        for (CircleInfo circledot : mCircleInfos) {
            if (currentX < (circledot.getX() + 20) && currentX > (circledot.getX() - 20) && currentY < (circledot.getY() + 20) && currentY > (circledot.getY() - 20)) {
                LogUtil.i("TAG=" + getClass().getSimpleName(), "111dot=" + circledot.getTag() + "  dotX=" + circledot.getX() + "  doty=" + circledot.getY());
                CircleInfo circleInfo = new CircleInfo();
                circleInfo.setTag(circledot.getTag());
                LogUtil.i("TAG=" + getClass().getSimpleName(), "222tag=" + circledot.getTag());

                circleInfo.setX(currentX);
                circleInfo.setY(currentY);
                mCircleInfos.set(circledot.getTag(), circleInfo);
                mCircleInfos.remove(circledot);

                invalidate();
            }
            LogUtil.i("TAG=" + getClass().getSimpleName(), "333dot=" + circledot.getTag() + "  x=" + circledot.getX() + "  y=" + circledot.getY());
        }
    }
    return true;
}

}

xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/button"
     android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/regional_webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:scrollbars="none" />
</FrameLayout>

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答