onDraw() 被调用但没有显示任何内容
我需要在屏幕上的背景图像上绘制一些矩形。为此,我创建了这个类:
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println("draw");
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background, 0, 0, null);
}
}
并在我放置的活动的 xml 文件上:
<com.www.MyImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="150dip"
android:layout_marginTop="50dip">
在 logCat
中,我从 onDraw()
方法中获取消息,但没有显示任何内容。我的问题出在哪里?我想念什么?
I need to draw some rectangles on screen, over the background image. For this, I create this class :
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println("draw");
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background, 0, 0, null);
}
}
and on xml file of the activity I put :
<com.www.MyImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="150dip"
android:layout_marginTop="50dip">
In logCat
I get the message down from onDraw()
method but nothing is displayed. Where is my problem? What do I miss?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需尝试canvas.drawBitmap(background, 0, 0, new Paint());
just try
canvas.drawBitmap(background, 0, 0, new Paint());