0

在我的应用程序中,我想在 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(); }。但它不起作用。

4

3 回答 3

0
Canvas c = null;
c = mSurfaceHolder.lockCanvas();
imageview.line(canvas);

mSurfaceHolder.unlockCanvasAndPost(c);

public void line(Canvas draw){
// set the layout parameters
// get the background image as ImageView
// Use the gesture Overlay concept. 
}
于 2013-03-28T09:42:04.027 回答
0

您发布的链接应该可以工作。不要使用股票图像视图,而是创建一个扩展它的类。您扩展的图像视图将支持一个名为 addLine 的方法,该方法将在数组列表中添加线条并覆盖 onDraw 方法以在数组列表中绘制线条

public class Line{
   int x1,y2, x2,y2;
   Paint p;
   public Line(int x1, y1, x2, y2){
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
      p= new Paint(Paint.ANTI_ALIAS_FLAG);
   }

   public void draw(Canvas c){
    c.drawLine(x1, y1, x2, y2, p);
   }
}

public class MyImageView extends ImageView{
    ArrayList<Line> lines;

    public void addLine(int x1, int y1, int x2, int y2){
        lines.add(new Line(x1,y1,x2,y2));
        //this.invalidate(); //queue a call to the onDraw method
    }

    public MyImageView(Context context) {
        super(context);
        lines = new ArrayList<Line>();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(Line l : lines){
            l.draw(canvas);
        }
    }

}
于 2013-03-28T08:10:53.103 回答
0

hye friends 用画布简单画线:-

    public class MainActivity extends Activity {

    private RelativeLayout relativelayout;

    private Paint mPaint, mBitmapPaint;
    private MyView mView;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    relativelayout = (RelativeLayout) findViewById(R.id.item);

    DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();
    int w = metrics.widthPixels;
    int h = metrics.heightPixels;

    System.out.println(" width " + w);
    System.out.println(" height " + h);

    mView = new MyView(this, w, h);
    mView.setDrawingCacheEnabled(true);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.Black);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(5);

    relativelayout.addView(mView);
    }

    public class MyView extends View {

   public MyView(Context c, int w, int h) {
    super(c);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mBitmapPaint
    .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 5;

    private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
    }

    private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
    mX = x;
    mY = y;
    }
    }

    private void touch_up() {
    mPath.lineTo(mX, mY);

    mCanvas.drawPath(mPath, mPaint);

    mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    touch_start(x, y);
    invalidate();
    break;
    case MotionEvent.ACTION_MOVE:
    touch_move(x, y);
    invalidate();
    break;
    case MotionEvent.ACTION_UP:
    touch_up();
    invalidate();
    break;
    }
    return true;
    }
    }
    }
于 2014-04-02T02:48:51.040 回答