在我的应用程序中,我想在imageview上绘制一条线。当我触摸它时,我想用手指写一条线。这是我的要求。为此我用googled alot并且休息了下面的链接:
How to draw a Line in ImageView on Android?
在我的活动中,我已经创建了MyImageView类的对象,并在我的OnTouchListener覆盖onTouch()方法和MotionEvent.ACTION_DOWN中:switch case我写了update()方法。我收到此错误:“方法更新()未定义类型new View.OnTouchListener(){}“。所以请任何人建议我如何做到这一点。感谢任何帮助inadvance。
这是我的代码: 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){ //更新(); 但是它没有用。
答案 0 :(得分:0)
您发布的链接应该有效。而不是使用股票图像视图,创建一个扩展它的类。你扩展的imageview将支持一个名为addLine的方法,它将在一个arraylist中添加该行并覆盖onDraw方法以在arraylist中绘制线
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);
}
}
}
答案 1 :(得分: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.
}
答案 2 :(得分:0)
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;
}
}
}