drawBitmap 不是全屏的

发布于 2024-12-11 11:11:52 字数 389 浏览 7 评论 0原文

我使用 onDraw 方法显示图像,如下所示:

public void onDraw(Canvas canvas) {
     super.onDraw(canvas);
     Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.photo0);
     canvas.drawColor(Color.BLACK);
     canvas.drawBitmap(background, 0, 0, null);

我想将此图像设置为背景,但它仅显示在屏幕的一部分上。如何设置为全屏?

有没有办法从 xml 将图像设置为背景并通过 onDraw 方法在此图像上绘制其他图像?

I display an image using onDraw Method like this :

public void onDraw(Canvas canvas) {
     super.onDraw(canvas);
     Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.photo0);
     canvas.drawColor(Color.BLACK);
     canvas.drawBitmap(background, 0, 0, null);

I want to set this image as background, but it is displayed only on a part of the screen. How to set it as fullscreen?

There is a way to set the image as background from xml and to draw other images on this image from onDraw Method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一笔一画续写前缘 2024-12-18 11:11:52

试试这个:

...
Rect dest = new Rect(0, 0, getWidth(), getHeight());
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(background, null, dest, paint);

这会将位图的一部分(“null”表示整个位图)渲染到 dest 指定的屏幕区域(在本例中是视图的整个区域)。

请注意,这将/可能会改变纵横比,具体取决于背景,您可能需要修复它。

try this:

...
Rect dest = new Rect(0, 0, getWidth(), getHeight());
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(background, null, dest, paint);

This renders a part of the bitmap ("null" means the whole bitmap) to the screen area specified by dest (which is the whole area of the view in this case).

Note that this will/might change the aspect ratio, depending on the background, you might need to fix that.

﹉夏雨初晴づ 2024-12-18 11:11:52

这对我有用。

      WindowManager wm = (WindowManager) mContext
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width=size.x;
    int height= size.y;

    Rect dest = new Rect(0, 0, width, height);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bm, null, dest, paint); //bm is your bitmap

This is working for me.

      WindowManager wm = (WindowManager) mContext
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width=size.x;
    int height= size.y;

    Rect dest = new Rect(0, 0, width, height);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bm, null, dest, paint); //bm is your bitmap
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文