android - 在android中自定义 ImageView 上绘制

标签 android canvas android-imageview android-view android-drawable

我正在开发一个绘图应用程序。我已经创建了一个绘图 View ,允许用户在 View 上绘图。

问题是,当我在上面绘图时,它超出了图像的区域(请引用图片,黄线超出了实际照片的区域),如何将 Canvas 尺寸粘贴到实际图像上?

ImageView 如何缩放?这意味着当用户单击笔按钮时,它会绘制,当用户再次单击时,它会变成缩放功能。

谢谢。缩放功能可以稍后解决,超出区域的问题需要先解决

enter image description here

这是 xml 中的自定义绘制 View

  <com.example.tool.DrawView
        android:id="@+id/draw"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

这里是java

public class DrawView extends ImageView {

    private int color = Color.BLACK;
    private float width = 4f;
    private List<Holder> holderList = new ArrayList<Holder>();

    private class Holder {      
        Path path;
        Paint paint;

        Holder(int color, float width) {
            path = new Path();
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(width);
            paint.setColor(color);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
        }
    }

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

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

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

    private void init() {
        holderList.add(new Holder(color, width));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Holder holder : holderList) {
            canvas.drawPath(holder.path, holder.paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                holderList.add(new Holder(color,width));
                holderList.get(holderList.size() - 1).path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                holderList.get(holderList.size() - 1).path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }

    public void resetPaths() {
        for (Holder holder : holderList) {
            holder.path.reset();
        }
        invalidate();
    }

    public void setBrushColor(int color) {
        this.color = color;
    }

    public void setWidth(float width) {
        this.width = width;
    }
}

我这样调用它:

DrawView pic = findViewById(R.id.draw);
pic.setImageBitmap(bitmap);

非常感谢您的帮助

最佳答案

首先计算显示图像的大小,然后将 eventX 和 eventY 钳位到显示图像的边界。代码如下:

   int imageViewH=imageView.getMeasuredHeight();//height of imageView
   int imageViewW =imageView.getMeasuredWidth();//width of imageView
   int drawableH =imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
   int drawableW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
   int displayH, displayW;  // the shown height and width of the picture.
   int leftX, rightX, topY, bottomY; // the shown edges of the picture.
   if (imageViewH/drawableH <= imageViewW/drawableW){
       displayW = drawableW*imageViewH/drawableH;//rescaled width of image within ImageView
       displayH = imageViewH;
       leftX = (imageViewW - displayW)/2; // left edge of the displayed image.
       rightX = leftX + displayW; // right edge of the displayed image.
       topY = 0; // top edg
       bottomY = displayH; // bottom edge.
   }else{
       displayH = drawableH*imageViewW/drawableH;//rescaled height of image within ImageView
       displayW = imageViewW;
       leftX = 0; // left edge of the displayed image.
       rightX = displayW; // right edge of the displayed image.
       topY = (imageViewH - displayH)/2; // top edg
       bottomY = topY + displayH; // bottom edge.
   }
   //TODO: clamp the eventX and eventY to the bound of leftX, rightX, topY, bottomY

注意:只有在 ImageView 具有测量的高度和可绘制对象后才应使用该代码。

关于android - 在android中自定义 ImageView 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28083503/

相关文章:

android - 如何修复 'Process unexpectedly exit'

android - 停止服务后如何从传感器注销监听器?

ruby-on-rails - 使用 Rspec/Capybara/Selenium 与 Canvas 元素交互

android - ImageView 上方的按钮?

java - 如何从另一个类更改 imageview 源?

java - 简单的 Drawable 装饰器不起作用

android - 写入嵌套字典 (Kotlin)

javascript - 在 JavaScript 中尝试使用 localStorage 保存和加载包含对象的数组时出错

javascript - 循环更改 Rect html Canvas 的颜色

android - 防止 ImageView 将 View 推离屏幕