简体   繁体   English

如何为imageView设置最终大小(50dp),以便在任何android设备屏幕上看起来都一样大小

[英]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显示图像,则必须生成其他文件:

  • image size 50x50 px (pixel) in folder mdpi 文件夹mdpi中的图片尺寸50x50 px(像素)
  • image size 75x75 px in folder hdpi 文件夹hdpi中的图片尺寸为75x75像素
  • image size 100x100 px in folder xhdpi xhdpi文件夹中的图像尺寸100x100 px
  • image size 150x150 px in folder xxhdpi 文件夹xxhdpi中的图像尺寸150x150像素

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设置widthheight 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.

相关问题 Android中的位图:显示为50dp的资源大小 - Bitmap in Android: Size of resources for displaying as 50dp 如何使用android different values文件夹为所有android设备设置50dp尺寸 - how set 50dp dimension for all android device using android different values folder 确定Android设备上各种屏幕尺寸的DP - Determine DP for various screen size on Android Device 如果我在xhdpi中有50dp宽/高的图像,我要为mdpi制作多大的尺寸? - If I have an image 50dp wide/tall in xhdpi, what size do I make for mdpi? 15dp 是否代表每个设备中相同的物理尺寸(比如屏幕上的 7 毫米)? (安卓) - Does 15dp represent the same physical size (say 7millimeters on screen) in every device? (Android) 视图未显示Android中内容的最后+-50dp - View doesn't show the last +- 50dp of the content in Android 如何一次用@dimen/50_dp 替换所有 dp 值,例如 50dp? - How to replace all dp value such as 50dp with a @dimen/50_dp at once? 如何在Android中将动态创建的TextView的高度精确设置为50dp? - How to make the height of a dynamically created TextView exactly 50dp in Android? Android如何在LienearLayouy顶部重叠ImageView,以使它们具有相同的大小? - Android how to overlap an ImageView on top of a LienearLayouy so that they have the same size? 如何在Android中使用dp大小设置边距 - How to set margin with dp size in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM