How do I make a background image fit the view but keep its aspect ratio when using <bitmap />
as a background drawable XML?
None of <bitmap>
's android:gravity
values gives the desired effect.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I wanted to do something similar in my custom Drawable class. Here are the important pieces:
With this approach you are flexible to apply any scale logic that you need
In order to fit the image to the available space (or if you have set width and height in dp), I have tried this approach if the image is not too wide.
Here I have set same width and height for square images [or you can wrap_content on both].
adjust view bounds and scale type center fit do the trick.
Old question, but none of the other answers worked for me. This xml code did however:
Another approach would be to create patch 9 images of your regular image and have it stretch scale the way you want it to.
You could have it center the content by putting 9-patch-dots in the corners that will preserve your ratio obviously (assuming the outer most edge of your image is repeatable/transparent).
Hopefully you get the idea.
Try using InsetDrawable (worked well for me).
Just give this your drawable, and insets (or padding) you want from either of the four sides.
It is specifically used for setting background drawable, of size smaller or than the View.
See Here : https://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html
It is impossible to achieve manipulating background attribute within xml-files only. There are two options:
You cut/scale the bitmap programmatically with
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
and set it as someView
's background.You use
ImageView
instead of background placing it as the first layout's element and specifyandroid:scaleType
attribute for it: