使用按钮在运行时调整 ImageView 的大小 - Android

发布于 2024-12-11 05:24:56 字数 653 浏览 0 评论 0原文

我正在使用 ImageView 容器显示图像。当用户输入特定值并单击“更新调整大小”时,需要调整图像大小

,仅用于显示,原始资源保持原样。无需编辑、保存等

xml 布局

<ImageView 
android:background="@drawable/picture01" 
android:id="@+id/picture01holder" 
android:layout_below="@+id/TextView01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
</ImageView>

main.java

final ImageView pic01 = (ImageView)findViewById(R.id.picture01);

现在我该怎么做才能动态分配此 pic01 我选择的大小。只是功能,我可以自己在按钮内实现它。我相信我必须使用像 pic01.setLayoutParams 这样的东西,但我不知道如何使用它。

基本上我想覆盖.java中的layout_height =“wrap_content”和layout_width =“wrap_content”

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update

resize is just for display, the original resource remains as it is. NO editing, save, etc

xml layout

<ImageView 
android:background="@drawable/picture01" 
android:id="@+id/picture01holder" 
android:layout_below="@+id/TextView01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
</ImageView>

main.java

final ImageView pic01 = (ImageView)findViewById(R.id.picture01);

now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.

basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java

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

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

发布评论

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

评论(3

只怪假的太真实 2024-12-18 05:24:56

通过获取 LayoutParams 更改 Imageview 大小

    final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
    pic01.setLayoutParams(layoutParams);

change the Imageview size by taking LayoutParams

    final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
    pic01.setLayoutParams(layoutParams);
一场春暖 2024-12-18 05:24:56

首先,您必须设置 图像的缩放类型CENTER_INSIDE 或CENTER_CROP取决于您的喜好。您应该在 onCreate() 方法中执行此操作。

myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

之后,您只需调整布局中图像视图的大小即可。这将取决于您使用的布局类型。 LinearLayout 的一个示例是:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);

或者您可以设置组件最小尺寸:

myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);

在这两种情况下,请确保可以在布局内实现该尺寸。如果您有多个重量较小的组件,则图像视图可能无法占用您所需的空间。

First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.

myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);

Or you can set the components minimal size:

myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);

In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.

森罗 2024-12-18 05:24:56
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);

pic01.setLayoutParams(myParams)
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);

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