Android Studio之ImageView

ImageView是图像显示控件,与图形显示有关的属性说明如下:

  • scaleType:指定图形的拉伸类型,默认是fitCenter。
  • src:指定图形来源,src图形按照scaleType拉伸。

++注意背景图不按scaleType指定的方式拉伸,背景默认以fitXY方式拉伸++。

fitXY FIT_XY 拉伸图片使其正好填满视图(图片可能被拉伸变形)
fitStart FIT_START 保持宽高比例,拉伸图片使其位于视图上方或左侧
fitCenter FIT_CENTER 保持宽高比例,拉伸图片使其位于视图中间
fitEnd FIT_END 保持宽高比例,拉伸图片使其位于视图下方或右侧
center CENTER 保持图片原尺寸,并使其位于视图中间
centerCrop CENTER_CROP 拉伸图片使其充满视图,并位于视图中间
centerInside CENTER_INSIDE 保持宽高比例,缩小图片使之位于视图中间(只缩小不放大)。 * 当图片尺寸大于视图时,centerInside 等同于fitCenter; * 当图片尺寸小于视图时,centerlnside等同于 center

接下来进行一个实验,把一张图片放入ImageView控件,尝试使用不同的拉伸类型,看看有什么区别:

1、activity_main.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            tools:ignore="MissingConstraints">

            <Button
                android:id="@+id/btnCenter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnCenter"
                android:textAllCaps="false" />

            <Button
                android:id="@+id/btnFitCenter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnFitCenter"
                android:textAllCaps="false" />

            <Button
                android:id="@+id/btnCenterCrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnCenterCrop"
                android:textAllCaps="false" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            tools:ignore="MissingConstraints">

            <Button
                android:id="@+id/btnCenterInside"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnCenterInside"
                android:textAllCaps="false" />

            <Button
                android:id="@+id/btnFitXY"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnFitXY"
                android:textAllCaps="false" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            tools:ignore="MissingConstraints">

            <Button
                android:id="@+id/btnFitEnd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnFitEnd"
                android:textAllCaps="false" />

            <Button
                android:id="@+id/btnFitStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:text="btnFitStart"
                android:textAllCaps="false" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="314dp"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_scale"
                android:layout_width="match_parent"
                android:layout_height="275dp"
                android:layout_marginTop="20dp"
                app:srcCompat="@drawable/d" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

2、MainActivity.java

java 复制代码
package com.example.myadroid1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_scale;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_scale = findViewById(R.id.iv_scale);

        Button btnCenter = findViewById(R.id.btnCenter);
        btnCenter.setOnClickListener(this);
        Button btnFitCenter = findViewById(R.id.btnFitCenter);
        btnFitCenter.setOnClickListener(this);
        Button btnCenterCrop = findViewById(R.id.btnCenterCrop);
        btnCenterCrop.setOnClickListener(this);
        Button btnCenterInside = findViewById(R.id.btnCenterInside);
        btnCenterInside.setOnClickListener(this);
        Button btnFitXY = findViewById(R.id.btnFitXY);
        btnFitXY.setOnClickListener(this);
        Button btnFitStart = findViewById(R.id.btnFitStart);
        btnFitStart.setOnClickListener(this);
        Button btnFitEnd = findViewById(R.id.btnFitEnd);
        btnFitEnd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCenter){
            iv_scale.setScaleType(ImageView.ScaleType.CENTER);
        }else if (v.getId() == R.id.btnFitCenter){
            iv_scale.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }else if (v.getId() == R.id.btnCenterCrop){
            iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }else if (v.getId() == R.id.btnCenterInside){
            iv_scale.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        }else if (v.getId() == R.id.btnFitXY){
            iv_scale.setScaleType(ImageView.ScaleType.FIT_XY);
        }else if (v.getId() == R.id.btnFitStart){
            iv_scale.setScaleType(ImageView.ScaleType.FIT_START);
        }else if (v.getId() == R.id.btnFitEnd){
            iv_scale.setScaleType(ImageView.ScaleType.FIT_END);
        }
    }
}
相关推荐
晚霞的不甘28 分钟前
Flutter for OpenHarmony从零到一:构建《冰火人》双人合作闯关游戏
android·flutter·游戏·前端框架·全文检索·交互
2601_9498333932 分钟前
flutter_for_openharmony口腔护理app实战+饮食记录实现
android·javascript·flutter
独自破碎E34 分钟前
【滑动窗口+字符计数数组】LCR_014_字符串的排列
android·java·开发语言
stevenzqzq41 分钟前
compose 中 align和Arrangement的区别
android·compose
VincentWei951 小时前
Compose:MutableState 和 mutableStateOf
android
jian110581 小时前
Android studio 调试flutter 运行自己的苹果手机上
flutter·智能手机·android studio
jian110581 小时前
Android studio配置flutter,mac Android studio 发现苹果手机设备
android·flutter·android studio
2501_940007892 小时前
Flutter for OpenHarmony三国杀攻略App实战 - 性能优化与最佳实践
android·flutter·性能优化
Rysxt_2 小时前
UniApp获取安卓系统权限教程
android·uni-app
WarmSword3 小时前
mac上用cursor/vscode调试root权限进程
c++·ide·vscode·macos·mac