<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ImageView 图片控件;Drawable对象或bitmap对象
src:路径图片
maxHeight/maxWidth
scaleType:调整尺寸-->
<androidx.constraintlayout.utils.widget.ImageFilterView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/ic_launcher_round"
android:id="@+id/myImg"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg_scale">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_center"
android:text="Center"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_centerCrop"
android:text="centerCrop"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_centerInside"
android:text="centerInside"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_fitEnd"
android:text="fitEnd"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_fitStart"
android:text="fitStart"/>
</RadioGroup>
</LinearLayout>
package com.aaa.proimg2;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
//赋值
ImageView myImg = (ImageView) findViewById(R.id.myImg);
RadioGroup rg_scale = (RadioGroup) findViewById(R.id.rg_scale);
RadioButton rb_center=(RadioButton) findViewById(R.id.rb_center);
RadioButton rb_centerCrop=(RadioButton) findViewById(R.id.rb_centerCrop);
RadioButton rb_centerInside=(RadioButton) findViewById(R.id.rb_centerInside);
RadioButton rb_fitEnd=(RadioButton) findViewById(R.id.rb_fitEnd);
RadioButton rb_fitStart=(RadioButton) findViewById( R.id.rb_fitStart);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
//针对单选按钮做测试:此处没有写完
rg_scale.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int i) {
switch (i){
case R.id.rb_center:
myImg.setScaleType(ImageView.ScaleType.CENTER);break;
case R.id.rb_centerCrop:
myImg.setScaleType(ImageView.ScaleType.CENTER_CROP);break;
case R.id.rb_centerInside:
myImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);break;
case R.id.rb_fitEnd:
myImg.setScaleType(ImageView.ScaleType.FIT_END);break;
case R.id.rb_fitStart:
myImg.setScaleType(ImageView.ScaleType.FIT_START);break;
}
}
});
}
}
标签:findViewById,import,AndroidStudio,RadioButton,rb,按钮,ImageView,id
From: https://blog.csdn.net/weixin_53406338/article/details/137523544