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

android – 如何在我的自定义视图的画布中设置位图图像?,第1张

概述我的主类中有一个Bitmap对象.我需要将此位图发送到我的自定义视图类,以将其设置为在画布上进一步处理的背景. 例如,有一个名为setPicture的方法,它接收位图作为参数.那么,如何在画布上绘制这个位图呢? 请参阅以下代码: public class TouchView extends View { final int MIN_WIDTH = 75; final int MIN_HEIG 我的主类中有一个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;}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {    // Todo auto-generated method stub    setMeasuredDimension(MeasureSpec.getSize(wIDthMeasureSpec),MeasureSpec.getSize(heightmeasureSpec));}@OverrIDeprotected 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;}@OverrIDepublic 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?

解决方法 在你的onDraw()方法里面,

做就是了

canvas.drawBitmap(myBitmap,null);

myBitmap是你的位图变量.

0,0指的是绘制的坐标,即左上角.

还有其他的APIs,可以吸引到某些地区等.

More info can be found here in the api docs.

或者:改为扩展ImageVIEw,并使用setimageBitmap(Bitmap src);实现这一目标的方法.

总结

以上是内存溢出为你收集整理的android – 如何在我的自定义视图的画布中设置位图图像?全部内容,希望文章能够帮你解决android – 如何在我的自定义视图的画布中设置位图图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1130292.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存