AndroidStudio学习记录(5):图像按钮ImageView的实现

<?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;
                }
            }

        });
    }
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值