Android Studio之ImageView

发布于:2024-05-10 ⋅ 阅读:(22) ⋅ 点赞:(0)

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 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

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


网站公告

今日签到

点亮在社区的每一天
去签到