0% found this document useful (0 votes)
3 views4 pages

DemoImageView

The document contains an XML layout for an Android application with a LinearLayout that includes a TextView and several buttons for changing the scale type of an ImageView. The associated Java class, ImageViewActivity, initializes the layout, sets an image resource, and implements button click listeners to update the ImageView's scale type and corresponding TextView text. The scale types include CENTER, CENTER_CROP, CENTER_INSIDE, FIT_CENTER, FIT_END, FIT_START, and FIT_XY.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

DemoImageView

The document contains an XML layout for an Android application with a LinearLayout that includes a TextView and several buttons for changing the scale type of an ImageView. The associated Java class, ImageViewActivity, initializes the layout, sets an image resource, and implements button click listeners to update the ImageView's scale type and corresponding TextView text. The scale types include CENTER, CENTER_CROP, CENTER_INSIDE, FIT_CENTER, FIT_END, FIT_START, and FIT_XY.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;

}
}
}

You might also like