Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中的imageview上划线_Android_Imageview_Draw - Fatal编程技术网

如何在android中的imageview上划线

如何在android中的imageview上划线,android,imageview,draw,Android,Imageview,Draw,在我的应用程序中,我想在imageview上画一条线。当我触摸它时,我想用手指写一条线。这是我的要求。为此,我在谷歌上搜索了很多次,并放弃了下面的链接: 在我的活动中,我创建了MyImageView类的对象,在我的OnTouchListener重写onTouch()方法和MotionEvent.ACTION\u DOWN:switch case中,我编写了update()方法。我得到了以下错误:“对于类型new View.OnTouchListener(){},方法update()未定义”.所

在我的应用程序中,我想在imageview上画一条线。当我触摸它时,我想用手指写一条线。这是我的要求。为此,我在谷歌上搜索了很多次,并放弃了下面的链接:

在我的活动中,我创建了MyImageView类的对象,在我的OnTouchListener重写onTouch()方法和MotionEvent.ACTION\u DOWN:switch case中,我编写了update()方法。我得到了以下错误:“对于类型new View.OnTouchListener(){},方法update()未定义”.所以请任何人建议我怎么做。谢谢你的帮助

这是我的代码: imageView.setOnTouchListener(新视图.OnTouchListener(){

@覆盖
公共布尔onTouch(视图v,运动事件){
//TODO自动生成的方法存根
ImageView视图=(ImageView)v;
view.setScaleType(ImageView.ScaleType.MATRIX);
浮标;
开关(event.getAction()&MotionEvent.ACTION\u掩码)
{
case MotionEvent.ACTION_DOWN://仅首指向下
savedMatrix.set(矩阵);
set(event.getX(),event.getY());
og.d(标记,“mode=DRAG”);//写入LogCat
模式1=DRAG1;
if(方格笔){
//更新();
}
打破
case MotionEvent.ACTION_UP://举起第一个手指
case MotionEvent.ACTION\u POINTER\u UP://举起食指
模式1=非模式1;
Log.d(标记“mode1=NONE1”);
打破
case MotionEvent.ACTION\u移动:
如果(模式1==DRAG1)
{ 
if(检查){matrix.set(savedMatrix);matrix.postTranslate(event.getX()-start.x,event.getY()-start.y);//在点矩阵中创建变换
}
} 
else if(mode1==ZOOM1)
{ 
如果(检查){
Log.e(“如果是Elseee”,“dfgrtgytey”);
//收缩变焦
float newDist=间距(事件);
Log.d(标记“newDist=“+newDist”);
如果(新距离>5f)
{
矩阵集(savedMatrix);
scale=newDist/oldDist;//设置
//矩阵…如果比例>1表示
//放大…如果比例小于1表示
//缩小
矩阵。后标度(标度、标度、中x、中y);
}
//是轮换的。。
if(lastEvent!=null){
newRot=旋转(事件);
//if(Constant.TRACE)Log.d(“degreeeee”,“newRot=“+(newRot));
float r=newRot-d;matrix.postRotate(r,view.getMeasuredWidth()/2,view.getMeasuredWidth()/2);
}
}
}
打破
//轮换。。
case MotionEvent.ACTION\u指针\u向下:
oldDist=间距(事件);
Log.d(标记“oldDist=“+oldDist”);
如果(oldDist>10f){savedMatrix.set(矩阵);
中点(中点,事件);
模式1=ZOOM1;
Log.d(标记“模式=缩放”)
}
lastEvent=新浮点[4];
lastEvent[0]=event.getX(0);
lastEvent[1]=event.getX(1);
lastEvent[2]=event.getY(0);
lastEvent[3]=event.getY(1);
d=旋转(事件);
打破
}//开关
view.setImageMatrix(矩阵);//在屏幕上显示转换
返回true;//指示事件已处理
}
});
在我的应用程序中,拖动图像,旋转图像并缩放图像。一切正常。我的要求是,当我单击按钮时,绘图功能将正常工作。这就是我在该块中编写更新方法的原因:“如果(检查铅笔){ //更新();
}。但它不起作用。

您发布的链接应该可以工作。不要使用stock image视图,而是创建一个扩展它的类。您扩展的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);
        }
    }

}
公共类行{
int-x1,y2,x2,y2;
油漆p;
公共线路(int x1、y1、x2、y2){
这是1.x1=x1;
这是1.y1=y1;
这是0.x2=x2;
这1.y2=y2;
p=新油漆(油漆。防漆别名标志);
}
公共空白绘图(画布c){
c、 抽绳(x1、y1、x2、y2、p);
}
}
公共类MyImageView扩展了ImageView{
阵列列表线;
公共无效地址行(整数x1、整数y1、整数x2、整数y2){
行。添加(新行(x1、y1、x2、y2));
//this.invalidate();//将对onDraw方法的调用排队
}
公共MyImageView(上下文){
超级(上下文);
行=新的ArrayList();
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
用于(行l:行){
l、 绘画(画布);
}
}
}
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;
    }
    }
    }

什么是mSurfaceHolder请清楚地告诉我,代码将粘贴到哪里。请说明您所做的。我已经发布了我的代码。请建议我。
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. 
}
    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;
    }
    }
    }