[英]how to set final size(50dp) for an imageView so that it looks same size on any android device screen
Hi I have a drawable object which contains a app icon.The app icon are stored in drawable object in a runtime
process, and then then setting that drawable object with an ImageView
. 嗨,我有一个包含应用程序图标的可绘制对象。应用程序图标在
runtime
过程中存储在可绘制对象中,然后使用ImageView
设置该可绘制对象。 But the size of the app icon changes depending on the device on which my app is running.I had set the width and height of imageView
in xml as 50dp and used this code also: 但是,根据设备上我的应用程序running.I已设定的宽度和高度的应用程序图标的大小变化
imageView
在XML作为50dp和也用这种代码:
image.setAdjustViewBounds(true);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
But it didn't worked.So how can I set final size(50dp) for my imageView
so that it can appear on same size on any android device screen. 但事与愿违worked.So我怎么可以设置为我的最终大小(50dp)
imageView
,这样它可以在任何Android设备屏幕上相同尺寸的出现。
Setting width and height in xml
file is enough. 在
xml
文件中设置宽度和高度就足够了。 You don't need to set it programmatically. 您无需以编程方式进行设置。
If you set width and height like this 如果您这样设置宽度和高度
image.setMaxHeight(50);
image.setMaxWidth(50);
The value 50 is in pixel, not dp. 值50以像素为单位,而不是dp。 An image with size 50x50 pixel will look different in different devices, because the devices have different dpi.
由于设备的dpi不同,尺寸为50x50像素的图像在不同的设备中看起来会有所不同。
There are folders mdpi, hdpi, xhpdi and xxhdpi in your projects. 项目中有文件夹mdpi,hdpi,xhpdi和xxhdpi。 If you want display your image in 50dp, you have to generate different files:
如果要以50dp显示图像,则必须生成其他文件:
It is because in mdpi 1 px = 1 dp, in hdpi 1dp = 1.5 px, in xhdpi 1dp = 2px and in xxhdpi 1dp = 3px 这是因为mdpi 1 px = 1 dp,hdpi 1dp = 1.5 px,xhdpi 1dp = 2px和xxhdpi 1dp = 3px
UPDATE You can just generate one image with size 150x150 pixel and put it in folder xxhdpi. 更新您只可以生成一张大小为150x150像素的图像,并将其放在文件夹xxhdpi中。 App will automatically scale your image to fit other density.
应用程序将自动缩放图像以适合其他密度。
By doing 通过做
image.setMaxHeight(50);
image.setMaxWidth(50);
you are setting width
and height
in pixels
. 您正在以
pixels
设置width
和height
。 And for different devices as resulution changes it will not look same so you need to set those values in density independent pixels(dp)
. 对于不同的设备,随着重新溶解度的变化,外观将不一样,因此您需要将这些值设置为与
density independent pixels(dp)
。 Use following method to convert your unit from dp
to pixels
according to devices density
. 使用以下方法根据设备
density
将单位从dp
转换为pixels
。
public final class DimensionUtils {
private static boolean isInitialised = false;
private static float pixelsPerOneDp;
// Suppress default constructor for noninstantiability.
private DimensionUtils() {
throw new AssertionError();
}
private static void initialise(View view) {
pixelsPerOneDp = view.getResources().getDisplayMetrics().densityDpi / 160f;
isInitialised = true;
}
public static float pxToDp(View view, float px) {
if (!isInitialised) {
initialise(view);
}
return px / pixelsPerOneDp;
}
public static float dpToPx(View view, float dp) {
if (!isInitialised) {
initialise(view);
}
return dp * pixelsPerOneDp;
}
}
您可以使用TypedValue
根据提供的dp
获取px
值:
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DP, 50, getResources().getDisplayMetrics());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.