从Android的PNG文件绘制自定义视图背景 [英] Draw background of custom View from .png file on Android

查看:145
本文介绍了从Android的PNG文件绘制自定义视图背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过查看扩展创建一个自定义视图。在的OnDraw()我设法画一些圆和其他的东西。但现在我想补充的资源(SD卡或流)的背景下这实际上是一个地图,我从我们的服务器下载,比利用它。这是为Android 8 +

I created a custom View by extending from View. In onDraw() I managed to draw some circles and other stuff. But now I want to add a background from a resource (sd card or a stream) which is actually a map I download from our server and than draw on it. It's for Android 8+

@Override
protected void onDraw(Canvas canvas) {
    Canvas g = canvas;
    String file = "/mnt/sdcard/download/tux.png";
    Bitmap bg = null;
    try {
        bg = BitmapFactory.decodeFile(file);
        g.setBitmap(bg);
    } catch (Exception e) {
        Log.d("MyGraphics", "setBitmap() failed according to debug");
    }
}

不知怎的g.setBitmap(BG)不断失败,我还没有看的图像规格,但实际上它只是一个晚礼服的图像PNG格式(无24位色)。 有人可以给我一些提示如何添加背景图片,所以我可以在上面画画吗? 谢谢你。

Somehow g.setBitmap(bg) keeps failing, I haven't looked at the image specs, but actually it's just a tux image (no 24 bits colors) of PNG format. Can someone give me some tips how to add a background image so I can draw on it? Thank you.

推荐答案

您其实并不想画的的加载位图,你只是想绘制在画布上,所以你应该使用Canvas.drawBitmap()。你也真的不应该加载一个位图中的每个的OnDraw(),这样做在构造函数来代替。试试这个类:

You actually don't want to draw on to the bitmap you load, you just want to draw it on the Canvas, so you should use Canvas.drawBitmap(). You also really should not load a Bitmap in each onDraw(), do it in the constructor instead. Try this class:

package com.example.android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
    private final Bitmap mBitmapFromSdcard;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBitmapFromSdcard = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Canvas g = canvas;
        if (mBitmapFromSdcard != null) {
            g.drawBitmap(mBitmapFromSdcard, 0, 0, null);
        }
    }
}

您也可以让Android的绘制位图的背景:

You can also let Android draw the bitmap in the background:

package com.example.android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
        if (bm != null) {
            setBackgroundDrawable(new BitmapDrawable(bm));
        }
    }
}

这篇关于从Android的PNG文件绘制自定义视图背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆