Android:imageview的onTouch事件无法正常工作。

时间:2016-03-02 10:38:43

标签: java android

这是我项目的oncreate方法。

        Button button = (Button) findViewById(R.id.button);
        final EditText n = (EditText) findViewById(R.id.editText);
        imageview=(CustomImage) findViewById(R.id.image);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                t = Integer.valueOf(n.getText().toString());

                paint = new Paint();
                paint.setColor(Color.BLACK);
                paint.setStyle(Paint.Style.FILL);
                paint.setStrokeWidth(70);

                bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888);
                c = new Canvas(bmp);
                c.drawBitmap(bmp, 0, 0, null);
                int h = bmp.getHeight() / (t + 1);
                int w = bmp.getWidth() / (t + 1);
                c = new Canvas(bmp);
                int i, j;
               for (i = w; i <= (bmp.getWidth() - w); i = i + w) {
                   for (j = h; j <= (bmp.getHeight() - h); j = j + h)
                       c.drawCircle(i, j, 20, paint);
                    }
                imageview.setImageBitmap(bmp);
            }

        });

这是customimage类,其中实现了imageview的ontouch事件。但它无法正常工作。这是ontouchevent方法中的代码

    p.setColor(Color.BLACK);
    p.setStyle(Paint.Style.FILL);
    p.setStrokeWidth(70);

    MainActivity12 ma=new MainActivity12();
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            downx = event.getX();
            downy = event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            upx = event.getX();
            upy = event.getY();
            invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
        default:
            break;
    }
    return true;
}

@Override
protected void onDraw(@NonNull Canvas canvas)
{
    super.onDraw(canvas);
    c.drawLine(downx, downy, upx, upy, p);
}

1 个答案:

答案 0 :(得分:0)

我知道我没有使用ondraw的canvas对象,因此我在画线时遇到了问题。