【问题标题】:How to draw a rectangle with black border and background transparent on Imageview?如何在Imageview上绘制一个黑色边框和背景透明的矩形?
【发布时间】:2017-06-20 19:54:13
【问题描述】:

我想在 ImageView 上绘制一个矩形作为下面的图片(黑色边框和透明作为背景)。 基本上我下载一个图像,放入一个 ImageView 并在我收到 4 个点后绘制一个矩形。 提前致谢

【问题讨论】:

  • 参考this answer。你可以在你的类中重写onDraw()方法。
  • @Jas 我已经创建了这个类,但是当我尝试包含 DrawView 而不是我当前的 ImageView 时出现一些错误
  • @alfo888_ibg 如果您遇到一些错误,您需要以某种方式修复它
  • @pskink 我收到此错误:java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line: Error inflating class it.path.DrawView.
  • @alfo888_ibg 我不知道第 25 行是什么

标签: android canvas imageview draw paint


【解决方案1】:

android.view.InflateException 的问题可以通过从 DrawView 类中删除构造函数并再次自动生成来解决。现在对于矩形,你必须有类似这样的 onDraw:

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setColor(Color.TRANSPARENT);
    paint.setStyle(Paint.Style.FILL);
    float leftx = 50;
    float topy =  50;
    float rightx =  150;
    float bottomy =  150;
    // FILL
    canvas.drawRect(leftx, topy, rightx, bottomy, paint);

    paint.setStrokeWidth(10);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    // BORDER
    canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}

如果您想在单击时获取坐标,然后绘制矩形覆盖 onTouchEvent 方法并执行类似的操作

class DrawView extends ImageView {

    float x, y;
    float width = 60.0f;
    float height = 50.0f;
    boolean touched = false;

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        if (touched) {
            Paint paint = new Paint();
            paint.setColor(Color.TRANSPARENT);
            paint.setStyle(Paint.Style.FILL);
            // FILL
            canvas.drawRect(x, y, (x + width) / 0.5f, (y + height) / 0.5f, paint);

            paint.setStrokeWidth(10);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            // BORDER
            canvas.drawRect(x, y, (x + width) / 0.5f, (y + height) / 0.5f, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        touched = true;
        //getting the touched x and y position
        x = event.getX();
        y = event.getY();
        invalidate();
        return true;
    }

}

【讨论】:

  • 是的,这是真的。我不知道为什么我没有想到!最后一个问题。我的图像之前没有矩形,当我得到坐标 x y 时我必须绘制矩形。如何使坐标动态并用矩形重绘图像?
  • 好的,那么你必须重写 onTouchEvent 方法并获取 x 和 y。
  • 不,基本上我有坐标(我从 MainActivity 中的 Json 接收它)。我需要,获取坐标,将矩形添加到上一个图像视图。
  • 嗯,那么你必须在类上创建两个列表,一个用于 x 坐标,一个用于 y 坐标,并用 json 中的值填充它们。然后在 onDraw 方法上创建一个 for 循环并将 x 和 y 替换为列表中的值。
  • 我很高兴能帮上忙! :)
【解决方案2】:

感谢@vasilis 的帮助,我解决了我的问题。

我创造了:

class DrawView extends ImageView {
   private int numberOfRectangles=0;
   private ArrayList<Rectangles> listRect;
   public DrawView(Context context) {
       super(context);
   }
   public DrawView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }
   public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
   }

   @Override
   public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (numberOfRectangles> 0 && listRect!= null)
    {
        for (int i=0; i< numberOfRectangles; i++)
        {
            Paint paint = new Paint();
            paint.setColor(Color.TRANSPARENT);
            paint.setStyle(Paint.Style.FILL);
            float leftx = listRect.get(i).getLeftx();
            float topy = listRect.get(i).getTopy();
            float rightx = listRect.get(i).getRightx();
            float bottomy = listRect.get(i).getBottomy();
            // FILL
            canvas.drawRect(leftx, topy, rightx, bottomy, paint);

            paint.setStrokeWidth(10);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            // BORDER
            canvas.drawRect(leftx, topy, rightx, bottomy, paint);
        }
    }
}
 public void creatRectangles(ArrayList<Rectangles> arrayRettangoliTest) {
    numberOfRectangles=arrayRettangoliTest.size();
    this.listRect= arrayRettangoliTest;
    this.invalidate();
  }

}

在我的 xml 文件中:

 <it.path.DrawView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/placeholder" />

我创建了一个矩形类:

public class Rectangles {
   private float leftx;
   private float topy ;
   private float rightx;
   private float bottomy;

    public Rectangles(float leftx, float topy, float rightx, float bottomy)      {
        this.leftx = leftx;
        this.topy = topy;
        this.rightx = rightx;
        this.bottomy = bottomy;
    }

    @Override
    public String toString() {
        return "Rectangles{" +
            "leftx=" + leftx +
            ", topy=" + topy +
            ", rightx=" + rightx +
            ", bottomy=" + bottomy +
            '}';
    }

    public void setLeftx(float leftx) {
        this.leftx = leftx;
    }

    public void setTopy(float topy) {
        this.topy = topy;
    }

    public void setRightx(float rightx) {
        this.rightx = rightx;
    }

    public void setBottomy(float bottomy) {
        this.bottomy = bottomy;
    }

    public float getLeftx() {
        return leftx;
    }

    public float getTopy() {
        return topy;
    }

    public float getRightx() {
       return rightx;
    }

    public float getBottomy() {
        return bottomy;
   }

}

在我的 MainActivity 中(例如在 onClick() 之后):

  ...
  ArrayList<Rectangles> arrayRectanglesTest= new ArrayList<>(4);
  arrayRectanglesTest.add(new Rectangles(50, 50, 100, 100));
  arrayRectanglesTest.add(new Rectangles(150, 150, 200, 200));
  arrayRectanglesTest.add(new Rectangles(250, 250, 300, 300));
  arrayRectanglesTest.add(new Rectangles(350, 350, 400, 400));
  arrayRectanglesTest.add(new Rectangles(450, 450, 500, 500));
  imageView.creatRectangles(arrayRectanglesTest);
  ...

所以我将矩形的数量设为动态 这里是结果

【讨论】:

  • x,y 位置没问题,但矩形大小没有改变。我想知道如何绘制从 x、y 位置开始的矩形,并且我有矩形的宽度和高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多