activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="20dp"
tools:context=".ImageViewActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_marginHorizontal="10dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Center" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="centerCrop" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="centerInside" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitCenter" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitEnd" />
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitStart" />
<Button
android:id="@+id/button9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitXY" />
</LinearLayout>
<ImageView
android:scaleType="center"
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:srcCompat="@drawable/vitdonnald" />
</LinearLayout>
MainActivity.java
public class ImageViewActivity extends AppCompatActivity implements
View.OnClickListener {
TextView textView;
ImageView imageView2;
private int[] listButtonID = { R.id.button3, R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.button9};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_image_view);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
init();
}
public void init()
{
textView = findViewById(R.id.textView);
imageView2 = findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.vitdonnald);
for(int id:listButtonID) {
Button btnTemp = (Button) findViewById(id);
btnTemp.setOnClickListener(this);
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button3:
imageView2.setScaleType(ImageView.ScaleType.CENTER);
textView.setText("Center Style");
break;
case R.id.button4:
imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
textView.setText("Center Crop Style");
break;
case R.id.button5:
imageView2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
textView.setText("Center Inside Style");
break;
case R.id.button6:
imageView2.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setText("Center Center Style");
break;
case R.id.button7:
imageView2.setScaleType(ImageView.ScaleType.FIT_END);
textView.setText("Center FitEnd Style");
break;
case R.id.button8:
imageView2.setScaleType(ImageView.ScaleType.FIT_START);
textView.setText("Center FitStart Style");
break;
case R.id.button9:
imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
textView.setText("Center FitXY Style");
break;
}
}
}