<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher_background"/>
<Button
android:id="@+id/btn_center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="center"/>
<Button
android:id="@+id/btn_fitCenter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitcenter"/>
<Button
android:id="@+id/btn_centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="centerCrop"/>
<Button
android:id="@+id/btn_centerInside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="centerInside"/>
<Button
android:id="@+id/btn_fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitXY"/>
<Button
android:id="@+id/btn_fitStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitStart"/>
<Button
android:id="@+id/btn_fitEnd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fitEnd"/>
</LinearLayout>
package com.example.second;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_center).setOnClickListener(this);
findViewById(R.id.btn_fitCenter).setOnClickListener(this);
findViewById(R.id.btn_centerCrop).setOnClickListener(this);
findViewById(R.id.btn_centerInside).setOnClickListener(this);
findViewById(R.id.btn_fitXY).setOnClickListener(this);
findViewById(R.id.btn_fitStart).setOnClickListener(this);
findViewById(R.id.btn_fitEnd).setOnClickListener(this);
// Toolbar toolbar = findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// FloatingActionButton fab = findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
ImageView iv =(ImageView)findViewById(R.id.imageView);
if (view.getId()==R.id.btn_center){
iv.setScaleType(ImageView.ScaleType.CENTER);
}else if (view.getId()==R.id.btn_fitCenter){
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
}else if (view.getId()==R.id.btn_centerCrop){
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else if (view.getId()==R.id.btn_centerInside){
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}else if (view.getId()==R.id.btn_fitXY){
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}else if (view.getId()==R.id.btn_fitStart){
iv.setScaleType(ImageView.ScaleType.FIT_START);
}else if (view.getId()==R.id.btn_fitEnd){
iv.setScaleType(ImageView.ScaleType.FIT_END);
}
}
}