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);
        }
    }
}
相关推荐
liang_jy21 小时前
Android SparseArray
android·源码
liang_jy1 天前
Activity 启动流程扩展篇(一)—— startActivityInner 任务决策全解析
android·源码
NPE~1 天前
[App逆向]脱壳实战
android·教程·逆向·android逆向·逆向分析
木易 士心1 天前
别再只会用 drawCircle 了!一文搞懂 Android Canvas 底层机制
android
AtOR CUES1 天前
MySQL——表操作及查询
android·mysql·adb
怣疯knight1 天前
安卓App无法增加自定义图片作为图标功能
android
jinanwuhuaguo1 天前
OpenClaw联邦之心——从孤岛记忆到硅基集体潜意识的拓扑学革命(第二十三篇)
android·人工智能·kotlin·拓扑学·openclaw
读书札记20221 天前
visual studio 调试技巧总结
ide·visual studio
Gary Studio1 天前
安卓HAL C++基础-命名域
android
诸神黄昏EX1 天前
Android Google XTS
android