onDraw() 被调用但没有显示任何内容

发布于 2024-12-11 09:40:31 字数 927 浏览 5 评论 0原文

我需要在屏幕上的背景图像上绘制一些矩形。为此,我创建了这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-12-18 09:40:31

只需尝试canvas.drawBitmap(background, 0, 0, new Paint());

just try canvas.drawBitmap(background, 0, 0, new Paint());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文