问题描述
对于
QVGA(ldpi)放大,按钮为57X13像素:
所有这些都表示,我是否需要缩小布局而不是考虑我在可绘制文件夹(xhdpi,hdpi,mdpi和ldpi)中实现了缩放按钮图像,在可绘制资源文件中调用它,然后将可绘制资源文件设置为ImageButton的背景图像?这是我的按钮btw的XML:
< ImageButton
android:layout_width = wrap_content
android:layout_height = wrap_content
android:adjustViewBounds = true
android:id = @ + id / exitBtn
android:layout_marginBottom = 57dp
android: src = @ drawable / popup_btn
android:layout_alignParentBottom = true
android:layout_centerHorizontal = true
android:scaleType = centerCrop
android:background = @android:color / transparent />
PS 是缩放绘图文件夹xhdpi,它是默认位置xxhdpi屏幕也是如此?对于xxxhdpi及更高版本呢?
通过实例化显示指标。现在从显示指标中检索您的宽度和高度整个根视图
一旦完成,请在活动的Oncreate中实现globallayoutchangelistener并将缩放代码放入侦听器中。
想法是首先计算设备密度,然后根据globallayoutchangelistener中的密度缩放按钮
让我知道它是否有效。
For my app (runs Android 2.1 and above),I was wondering how to scale buttons for various screen sizes (virtual emulators in this case) in terms of dpi, because my scaled background images seem to be "unpixelated" perfectly fine... I scaled my button ("GOT IT" as shown below) accordingly with say, an lpdi and a xxhdpi screen, and I get the following results:
For the Nexus 5 virtual emulator (xxhdpi), button supposedly at 150X34 pixels which seems to navigate to the ldpi resource director instead:
Zoomed-in for the QVGA (ldpi), button at 57X13 pixels:
All of that said, do I need to scale down the layout(s) instead considering that I implemented the scale-button images in drawable folders (xhdpi, hdpi, mdpi, and ldpi), called it in a drawable resource file, and then set the drawable resource file as the background image of the ImageButton? Here's the XML of my button btw:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="@+id/exitBtn"
android:layout_marginBottom="57dp"
android:src="@drawable/popup_btn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:background="@android:color/transparent" />
P.S. Is the scaled-drawable folder, xhdpi, the default location for xxhdpi screens as well? How about for xxxhdpi and above?
create display metrics by instantiating it .now from display metrics retrieve the width and height of your whole root view
Once done implement the globallayoutchangelistener in Oncreate of your activity and place your scaling code inside the listener .
Idea is to calculate the device density first and then scale the button according to density in globallayoutchangelistener
Let me know if it works .
这篇关于缩放ImageButton以适合各种屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!