Android - Setting background on canvas using PNG file

半世苍凉 提交于 2019-12-12 02:54:40

问题


I'm trying to set a .PNG file as background on Canvas in my app. I've made an image 480 x 800 and used this method:

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), 
                  R.drawable.image_1), 0, 0, null);

I've started an emulator (WVGA800) but my image looks greater than screen of the device.

How do I resize this image or what kind of methods should I use to make this image well-matched.

Secondly, is there any way to make backgrounds like this universal for devices with different screen resolutions?

Thank you in advance.


回答1:


where did you put the image? if it's in the drawable or the drawable-mdpi , it will be larger than what you've told , since WVGA800 has a high density (hdpi) .

even if you put it on the drawable-hdpi folder , it will work for WVGA800 , but it might not show well on other devices , which have different resolutions and aspect ratio .

you need to handle the scaling and keeping of aspect ratio (if you wish) . otherwise , you will have the same problems on other devices.




回答2:


Try this one ...

Set bitmap

Bitmap mFinalbitmap= BitmapFactory.decodeResource(getResources(), R.drawable.image_1);

Resize bitmap as per your width and height

mFinalbitmap= resizeImage(mFinalbitmap, width ,height);

Set Canvas of Bitmap

canvas.drawBitmap(mFinalbitmap, 0, 0, null);

Resize Function: As per maintain x and y of image

public Bitmap resizeImage(Bitmap image,int maxWidth, int maxHeight)
{
    Bitmap resizedImage = null;
    try {
        int imageHeight = image.getHeight();


        if (imageHeight > maxHeight)
            imageHeight = maxHeight;
        int imageWidth = (imageHeight * image.getWidth())
                / image.getHeight();

        if (imageWidth > maxWidth) {
            imageWidth = maxWidth;
            imageHeight = (imageWidth * image.getHeight())
                    / image.getWidth();
        }

        if (imageHeight > maxHeight)
            imageHeight = maxHeight;
        if (imageWidth > maxWidth)
            imageWidth = maxWidth;


        resizedImage = Bitmap.createScaledBitmap(image, imageWidth,
                imageHeight, true);
    } catch (OutOfMemoryError e) {

        e.printStackTrace();
    }catch(NullPointerException e)
    {
        e.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return resizedImage;
}



回答3:


The easiest way: declare static Bitmap in your class:

Bitmap bitmap;

setup the resized bitmap, for example you want resized bitmap to 100x100:

    private void initBitmap(){
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.Your_bitmap);
            bitmap = Bitmap.createScaledBitmap(bitmap, 100,100,true);
    }

and call method in constructor



来源:https://stackoverflow.com/questions/10765300/android-setting-background-on-canvas-using-png-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!