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);
        }
    }
}
相关推荐
落落落sss19 分钟前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.40 分钟前
数据库语句优化
android·数据库·adb
GEEKVIP3 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model20055 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏6895 小时前
Android广播
android·java·开发语言
与衫6 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql
菜鸟一皓6 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea
500了12 小时前
Kotlin基本知识
android·开发语言·kotlin
人工智能的苟富贵13 小时前
Android Debug Bridge(ADB)完全指南
android·adb
小雨cc5566ru18 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app