我正在尝试以编程方式更改宽度ImageView
而不调整图像大小。在ImageView
最初显示的整个图像,但是当图像的宽度减小时,图像的一部分应切断,如下所示:
但是,当我的代码减小ImageView
from 350dp
to的宽度时200dp
,图像会自动调整大小:
当我减小其宽度时,如何确保 ImageView不会调整图像大小?下面是我的代码:
活动_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:background="@drawable/android"/>
</LinearLayout>
主活动.java :
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reduce the width of the image preview box
ImageView imagePreviewBox = findViewById(R.id.photo_1_preview);
imagePreviewBox.getLayoutParams().width = 250;
}
}
将比例类型设置为 CENTER_CROP。这将使图像在视图中居中,并裁剪掉任何不适合的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句