开发者

how to scale an image to fill the screen

开发者 https://www.devze.com 2023-01-25 15:11 出处:网络
My issue is this: I use the android emulator, targeting android 2.2. I created a custom view which I want to handle the background of my application, in this view I only want to display an image but I

My issue is this: I use the android emulator, targeting android 2.2. I created a custom view which I want to handle the background of my application, in this view I only want to display an image but I want it to fill the entire screen.

I initialize the view from my "Activity" like this: BackgroundView bw = new BackgroundView(this);

The problem: On my HDD, the resource image I'm using is 500x375 pixels. After calling:

Bitmap m_resourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.landscape_1); int nWidth = m_resourceBitmap.getWidth(); int nHeight = m_resourceBitmap.getHeight();

the returned nWidth and nHeight depend on where I placed the resource, if i place it in the "drawable-mdpi" I get the original size, otherwise I get other sizes.

On the other hand if I ask the view for it's width and height via this.getWidth(); this.getHeight();

I get fixed values, but different from t开发者_如何学Che resolution I expected from the device (I set a 480x800 device and I get 320x533)

and finally if I try to create a scale matrix using view dimensions divided by the image dimensions, the result, on screen, is an image much smaller than the screen...

My question is: What questions should I ask and from which objects so that inside a View I can draw a resource Bitmap so that it occupies the entire screen?


Cant you do it all in the layout XML file? (Here's an example that I use for my menu background image)

<ImageView android:id="@+id/menuBackground"
        android:src="@drawable/imageResource"
        android:adjustViewBounds="true"
        android:gravity="center_vertical"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp" 
        android:layout_y="0dp" />

Then if you need to change it in runtime all you have to do is change the drawable.

0

精彩评论

暂无评论...
验证码 换一张
取 消