以编程方式更改 ImageView 大小

发布于 2024-11-19 21:50:31 字数 3215 浏览 2 评论 0原文

我有一个带有 3 个 ImageView 的相对布局。第一个是方形图像,第二个仅用于间距,第三个是另一个方形图像。

下面是该布局的 xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioSV"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/radioLayout"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center">

    <ImageView
        android:id="@+id/sssImageView"
        android:src="@drawable/radio_sss_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop">
    </ImageView>

    <!-- spacing -->

    <ImageView
        android:id="@+id/spacing1"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="10dip"
        android:layout_below="@id/sssImageView">
    </ImageView>

    <ImageView
        android:id="@+id/gaImageView"
        android:src="@drawable/radio_ga_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:layout_below="@id/spacing1">
    </ImageView>

    </RelativeLayout>
</LinearLayout>
</ScrollView>

现在,我希望我的应用程序对于各种屏幕密度都很有用,因此,在 java 文件中,我要求提供密度并随后使用 switch-case 语句。在此语句中,必须更改 2 个 ImageView(sssImageViewgaImageView)的大小(宽度、高度)。

例如,如果屏幕的密度很高,我希望 ImageView 的宽度和高度为 200sp。我刚刚将 10sp 作为“标准”值放入 xml 中。

这是高屏幕密度的用例语句的一部分:

case DisplayMetrics.DENSITY_HIGH:
    layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
    sssImageView.setLayoutParams(layoutParams);
    sssImageView.setMaxHeight(val_high);
    sssImageView.setMaxWidth(val_high);

    gaImageView.setLayoutParams(layoutParams);
    gaImageView.setMaxHeight(val_high);
    gaImageView.setMaxWidth(val_high);
    break;

此外,val_high 是 200sp:

int val_high = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());

现在,带有 ImageView sssImageView 的部分可以完美工作。它正确地将图像从 10sp 放大到 200sp(不用担心 - 图像最初不是 10sp!)。 问题是另一个 ImageView,gaImageView,将自身置于 sssImageView 之上并破坏了布局。如果我用 gaImageView 注释掉这 3 行,它就位于布局中的正确位置,但当然仍然很小(10sp)。

我也尝试了相反的方法:用 sssImageView 注释掉 3 行,并且仅操作 gaImageView。现在,顶部 ImageView (sssImageView) 位于正确的位置,并且如预期的那样小。然而,底部的 ImageView,gaImageView,将自身定位在 sssImageView 之上,而不是像 xml 布局文件中所写的那样位于下面。

这里有什么问题吗?

I have a relative layout with 3 ImageViews. The first one is a square image, the second is just used for spacing, and the third one is another square image.

Here is the xml code for that layout:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioSV"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/radioLayout"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center">

    <ImageView
        android:id="@+id/sssImageView"
        android:src="@drawable/radio_sss_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop">
    </ImageView>

    <!-- spacing -->

    <ImageView
        android:id="@+id/spacing1"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="10dip"
        android:layout_below="@id/sssImageView">
    </ImageView>

    <ImageView
        android:id="@+id/gaImageView"
        android:src="@drawable/radio_ga_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:layout_below="@id/spacing1">
    </ImageView>

    </RelativeLayout>
</LinearLayout>
</ScrollView>

Now, I want my app to be useful for various screen densities, so therefore, in the java file, I ask for the density and use a switch-case statement afterwards. In this statement, the size (width, height) of the 2 ImageViews (sssImageView and gaImageView) have to be changed.

For instance, if the density of the screen is high, I want the width and height of the ImageViews to be 200sp. I've just put 10sp in the xml as a 'standard' value.

This is the part of the use-case statement for high screen density:

case DisplayMetrics.DENSITY_HIGH:
    layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
    sssImageView.setLayoutParams(layoutParams);
    sssImageView.setMaxHeight(val_high);
    sssImageView.setMaxWidth(val_high);

    gaImageView.setLayoutParams(layoutParams);
    gaImageView.setMaxHeight(val_high);
    gaImageView.setMaxWidth(val_high);
    break;

Also, val_high is 200sp:

int val_high = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());

Now, the part with the ImageView sssImageView works perfectly. It correctly enlarges the image from 10sp to 200sp (don't worry - the image is not 10sp originally!).
The problem is the fact that the other ImageView, gaImageView, puts itself on top of sssImageView and destroys the layout. If I comment out the 3 lines with gaImageView, it is on its correct place in the layout, but is still small (10sp) of course.

I have also tried the opposite: commenting out the 3 lines with sssImageView and only manipulating gaImageView. Now the top ImageView (sssImageView) is on its correct place and small as expected. However, the bottom ImageView, gaImageView, positions itself on top of sssImageView, and not below as written in the xml layout file.

What is wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-11-26 21:50:32

我设法解决了这个问题。

以下是 DENSITY_HIGH case 语句的代码:

case DisplayMetrics.DENSITY_HIGH:               
    display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();
    height = display.getHeight();               

    // 0.36 is a scaling factor
    int val_high = (int) (height*0.36);

    sssImageView.setId(1);
    gaImageView.setId(2);
    spacing.setId(3);

    layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
    layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
    layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
        (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));

    sssImageView.setMaxHeight(val_high);
    sssImageView.setMaxWidth(val_high);
    relativeLayout.updateViewLayout(sssImageView, layoutParams);            

    layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
    relativeLayout.updateViewLayout(spacing, layoutParams3);

    layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
    gaImageView.setMaxHeight(val_high);
    gaImageView.setMaxWidth(val_high);
    relativeLayout.updateViewLayout(gaImageView, layoutParams2);                
    break;

因此,一般来说,看起来我必须以编程方式“重新创建”xml 文件。

I managed to solve this problem.

Here is the code for the DENSITY_HIGH case statement:

case DisplayMetrics.DENSITY_HIGH:               
    display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();
    height = display.getHeight();               

    // 0.36 is a scaling factor
    int val_high = (int) (height*0.36);

    sssImageView.setId(1);
    gaImageView.setId(2);
    spacing.setId(3);

    layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
    layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
    layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
        (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));

    sssImageView.setMaxHeight(val_high);
    sssImageView.setMaxWidth(val_high);
    relativeLayout.updateViewLayout(sssImageView, layoutParams);            

    layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
    relativeLayout.updateViewLayout(spacing, layoutParams3);

    layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
    gaImageView.setMaxHeight(val_high);
    gaImageView.setMaxWidth(val_high);
    relativeLayout.updateViewLayout(gaImageView, layoutParams2);                
    break;

So generally, it looks like I had to "re-create" the xml file programmatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文