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);
        }
    }
}
相关推荐
openinstall全渠道统计2 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
双鱼大猫3 小时前
一句话说透Android里面的ServiceManager的注册服务
android
双鱼大猫3 小时前
一句话说透Android里面的查找服务
android
双鱼大猫3 小时前
一句话说透Android里面的SystemServer进程的作用
android
双鱼大猫3 小时前
一句话说透Android里面的View的绘制流程和实现原理
android
Earth explosion3 小时前
ide使用技巧与插件推荐
ide
双鱼大猫4 小时前
一句话说透Android里面的Window的内部机制
android
双鱼大猫4 小时前
一句话说透Android里面的为什么要设计Window?
android
双鱼大猫4 小时前
一句话说透Android里面的主线程创建时机,frameworks层面分析
android
dringlestry4 小时前
pycharm将当前项目上传到github
ide·pycharm·github