在我的应用程序中,我想在 imageview 上画一条线。当我触摸它时,我想用手指写一条线。这是我的要求。为此,我用谷歌搜索了很多,并使用下面的链接:
如何在 Android 上的 ImageView 中画一条线?.
在我的活动中,我创建了 MyImageView 类的对象,并在我的 OnTouchListener 覆盖 onTouch() 方法和 MotionEvent.ACTION_DOWN: switch case 我编写了 update() 方法。我收到了这个错误:“方法 update() 是未定义类型 new View.OnTouchListener(){}"。所以请任何人建议我如何执行此操作。感谢您提前提供的任何帮助。
这是我的代码: imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN: // first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
og.d(TAG, "mode=DRAG"); // write to LogCat
mode1 = DRAG1;
if(checkPencil){
//update();
}
break;
case MotionEvent.ACTION_UP: // first finger lifted
case MotionEvent.ACTION_POINTER_UP: // second finger lifted
mode1 = NONE1;
Log.d(TAG, "mode1=NONE1");
break;
case MotionEvent.ACTION_MOVE:
if (mode1 == DRAG1)
{
if(check){ matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix of points
}
}
else if (mode1 == ZOOM1)
{
if(check){
Log.e("if elseeeeeee","dfgrtgytey");
// pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f)
{
matrix.set(savedMatrix);
scale = newDist / oldDist; // setting the scaling of the
// matrix...if scale > 1 means
// zoom in...if scale < 1 means
// zoom out
matrix.postScale(scale, scale, mid.x, mid.y);
}
// its for rotation..
if (lastEvent!=null){
newRot = rotation(event);
//if (Constant.TRACE) Log.d("Degreeeeeeeeeee", "newRot="+(newRot));
float r = newRot-d; matrix.postRotate(r, view.getMeasuredWidth()/2, view.getMeasuredHeight()/2);
}
}
}
break;
//rotation..
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) { savedMatrix.set(matrix);
midPoint(mid, event);
mode1 = ZOOM1;
Log.d(TAG, "mode=ZOOM")
}
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
d = rotation(event);
break;
}//switch
view.setImageMatrix(matrix); // display the transformation on screen
return true; // indicate event was handled
}
});
在我的应用程序中拖动图像,旋转图像并缩放图像一切正常。我的要求是当我单击按钮时,绘图功能将正常工作。这就是我在这个块中编写更新方法的原因:“如果(checkPencil){ //update(); }。但它不起作用。