如何在自定义视图的画布中设置位图图像?

7

我在主类中有一个Bitmap对象。我需要将此位图发送到我的自定义视图类中,以将其设置为进一步在画布上处理的背景。

例如,有一个名为setPicture的方法,它接收位图作为参数。那么,如何在画布上绘制这个位图呢?

请参见下面的代码:

public class TouchView extends View {

 final int MIN_WIDTH = 75;
 final int MIN_HEIGHT = 75;
 final int DEFAULT_COLOR = Color.RED;
 int _color;
 final int STROKE_WIDTH = 2;

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float x, y;
private boolean touching = false;

public TouchView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    init();
}

public TouchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    init();
}

public TouchView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    init();
}

private void init() {
    setMinimumWidth(MIN_WIDTH);
    setMinimumHeight(MIN_HEIGHT);
    _color = DEFAULT_COLOR;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    if (touching) {
        paint.setStrokeWidth(STROKE_WIDTH);
        paint.setColor(_color);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(x, y, 75f, paint);
    }
}

public void setPicture (Bitmap bitmap) {

        ///////
         This method must receive my bitmap to draw it on canvas!!!!!!!!!!!!!!!
        ///////


}

public void setColor(int color) {
    _color = color;
}

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
    // TODO Auto-generated method stub

    switch (motionEvent.getAction()) {
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_DOWN:
        x = motionEvent.getX();
        y = motionEvent.getY();
        touching = true;
        break;
    default:
        touching = false;
    }
    invalidate();
    return true;
}

我应该如何将这个位图发送到onDraw中?

3个回答

6
在您的onDraw()方法中,只需要执行以下操作:
canvas.drawBitmap(myBitmap, 0, 0, null);

myBitmap是您的位图变量。

0,0指的是要绘制的坐标,也就是左上角。

还有其他可用的API,可以绘制到特定区域等。

更多信息可以在API文档中找到。

或者: 扩展ImageView,使用setImageBitmap(Bitmap src);方法实现这一点。


我认为问题是如何从MainActivity获取myBitmap变量到自定义视图中。在你的回答中,myBitmap将未定义。 - Daniel Viglione
@Donato 我猜他在绘制图像方面遇到的问题比将其传递给自定义视图更多。只需创建一个设置引用位图/ID的方法,或将其添加为构造函数的参数即可。 - IAmGroot

0

将位图转换为可绘制对象,并使用View类的setBackgroundDrawable方法。

public void setPicture (Bitmap bitmap) {
    setBackgroundDrawable(new BitmapDrawable(bitmap));
}

Leonidos,谢谢,它也起作用了,但我注意到这种方法会拉伸图片以适应组件。是否有可能以实际大小或正确比例适配我的图片? - Carlos
是的,例如将BitmapDrawable的gravity设置为“top”。阅读有关BitmapDrawable的文档。它具有许多功能。 - Leonidos

0
decodedString = Base64.decode(datadtlimgsItem.getImageStr(), Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            decodedByte = Util.resize(decodedByte, 1500, 1500);
            mDrawingView.addBitmap(decodedByte);

 public void addBitmap(Bitmap bitmap){
        canvasBitmap = bitmap;
        invalidate();
    }
// set bitmap on draw canvas in your custom view class
 @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    if (canvasBitmap == null){

        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }
    drawCanvas = new Canvas(canvasBitmap);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接