在android中绘制自定义图像视图

时间:2015-01-22 07:40:04

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

我正在制作一个绘图应用。我已经创建了一个允许用户在视图上绘制的绘图视图。

问题是,当我画画时,它超出了图像的区域(请参考图片,黄线超出实际照片区域),如何将画布大小粘贴到实际图像上?

图像视图如何可缩放?这意味着当用户点击笔按钮时,它会进行绘制,当用户再次点击时它会变为缩放功能。

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

enter image description here

这是xml中的自定义绘图视图

  <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);

非常感谢您的帮助

2 个答案:

答案 0 :(得分:3)

首先计算显示的图像尺寸,然后将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具有测量高度和可绘制高度后才能使用该代码。

答案 1 :(得分:1)

如果在ImageView的属性中有一个真正的属性adjustViewBounds,您可以通过编程方式将DrawView的宽度设置为ImageView的宽度。