简体   繁体   中英

Android: How to display IMAGE for Different screen resolutions/size programmatically

I am going through the supporting Multiple Screens documentation on the Android and need some clarification.

It's my understanding that designing three unique interfaces (ldpi, mdpi, and hdpi) would be the best way to go about supporting all the potential android screens.

but i believe this would hold true when you do this while design time.

but if you create a view during run time programmatically and want to set background image then would the above hold true?

i am trying to draw a bitmap on a canvas using following,

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.image_1), 0, 0, null); in my custom view extending SurfaceView and then using this view in my main activity.

now i am using image_1.png which is 480*800 pixels using gimp.

if i use this image in the above code then it is not displaying correctly and the image appears bigger ie the image appears too big for the screen and hence not able to display the full content.

can you please advice what is the best approach to go on this?

thanks, --ad

See. Put the image in each of the 3 folders with different sizes and resolutions ie hdpi, ldpi and mdpi. And you can call the image this way:

R.drawable.image_name

Dont worry android will take care and on large screen its will open hdpi where as in small screens it will open image from ldpi.

When you define, height and width use dp as unit instead of pixels.

Hmm, I'm not too familiar with that drawBitmap method, but I do see this in the documentation:

"If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas."

There is a getDensity and setDensity methods on Bitmap so you could call getDensity to see what it is and then experiment with setDensity, before the drawBitmap to see if that helps.

Note: Canvas also has a getDensity method, so you can print that out to the log and see if setting Bitmap density to the same value prevents the scaling issue.

There is also the: drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint) method, which states in the documentation: " This function ignores the density associated with the bitmap. This is because the source and destination rectangle coordinate spaces are in their respective densities, so must already have the appropriate scaling factor applied."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM