首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义View的画布中设置位图图像?

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

Stack Overflow用户
提问于 2013-01-06 00:58:44
回答 3查看 9.9K关注 0票数 6

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

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

请参考下面的代码:

代码语言:javascript
复制
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?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-06 01:05:50

在您的onDraw()方法中,

只管去做

代码语言:javascript
复制
canvas.drawBitmap(myBitmap, 0, 0, null);

myBitmap是您的位图变量。

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

还有其他Apis可用,用于绘制特定区域等。

More info can be found here in the api docs.

或者:改为扩展ImageView,并使用setImageBitmap(Bitmap src);方法来实现。

票数 6
EN

Stack Overflow用户

发布于 2013-01-06 01:23:48

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

代码语言:javascript
复制
public void setPicture (Bitmap bitmap) {
    setBackgroundDrawable(new BitmapDrawable(bitmap));
}
票数 0
EN

Stack Overflow用户

发布于 2019-02-15 14:53:45

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14174104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档