总结android:scaleType的使用

本文详细介绍了Android中ImageView的ScaleType属性及其对不同尺寸图片的影响。包括CENTER、CENTER_CROP、CENTER_INSIDE等模式的视觉效果对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备两张图片:

小图片:(72*72)

大图片:(600*403)

使用的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

java文件:

package zy.just.com.scaletypedemo;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);
//        image.setScaleType(ImageView.ScaleType.CENTER);
 //   image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
 //       image.setScaleType(ImageView.ScaleType.CENTER_CROP);
 //       image.setScaleType(ImageView.ScaleType.FIT_CENTER);
 image.setScaleType(ImageView.ScaleType.FIT_END);
 //      image.setScaleType(ImageView.ScaleType.FIT_START);
  //     image.setScaleType(ImageView.ScaleType.FIT_XY);
//        image.setScaleType(ImageView.ScaleType.MATRIX);
        //使用小图片
        //image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.small_picture));
        //使用大图片
        image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.big_picture));
    }
}

android:scaleType设置为MATRIX,其中MATRIX为默认值。

解释:

不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分作裁剪处理。

小图片效果图:

大图片效果图:


当android:scaleType设置为CENTER时。

解释:

保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。

使用小图片的效果:


使用大图片的效果:


当android:scaleType设置为CENTERCROP时。

解释:

以填满整个ImageView为目的,将原图的中心对准ImageView的中心,等比例放大原图,直到填满ImageView为止(指的是ImageView的宽和高都要填满),原图超过ImageView的部分作裁剪处理。

使用小图片的效果:


使用大图片的效果:


当android:scaleType设置为CENTERINSIDE时。

解释:

以原图完全显示为目的,将图片的内容完整居中显示,通过按比例缩小原图的size()等于或小于ImageView的宽()如果原图的size本身就小于ImageViewsize,则原图的size不作任何处理,居中显示在ImageView

小图片显示效果:

使用大图片效果:



当android:scaleType设置为FITCENTER时。

解释:

其实是将图片的宽比上imageview的宽。假设值为a1,图片的高比上imageview的高,假设值为a2。如果图片超出imageview,不管是宽度上,还是高度上。就要对图片进行缩小处理,并且居中显示,而且缩小的比例值k的取值为a1a2中较大的一个值,才能完整的将图片缩进到imageview中。如果图片小于imageview,即图片的宽高都要小于Imageview,那么取a1a2中较大的一个值来作为扩大的比例值k。根据这个k值来扩大图片。

小图片效果:

大图片显示效果:

当android:scaleType设置为FITXY时。

解释:

把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageView.

小图显示结果:

使用大图片的效果:

当android:scaleType设置为FITSTART时。

解释:

把原图按比例扩大(缩小)到ImageView的宽度(或高度),去最先达到的,显示在ImageView的上部分位置。

小图片显示结果:

使用大图片效果:


当android:scaleType设置为FITEND

把原图按比例扩大(缩小)到ImageView的宽度(或高度),取最先达到的,显示在ImageView的下部分位置。

小图片效果:

使用大图片效果:






<?xml version="1.0" encoding="utf-8"?> <com.android.keyguard.AlphaOptimizedImageButton android:layout_gravity=“centerandroid:id=“@id/ringer_icon” android:background=“@drawable/rounded_ripple” android:padding=“@dimen/volume_dialog_ringer_icon_padding” android:layout_width=“match_parent” android:layout_height=“match_parent” android:scaleType=“fitCenterandroid:tint=“@color/accent_tint_color_selector” android:soundEffectsEnabled=“false” style=“@style/VolumeButtons” /> <com.android.keyguard.AlphaOptimizedImageButton android:layout_gravity=“centerandroid:id=“@id/settings” android:background=“@drawable/ripple_drawable_20dp” android:layout_width=“@dimen/volume_dialog_tap_target_size” android:layout_height=“@dimen/volume_dialog_tap_target_size” android:src=“@drawable/ic_tune_black_16dp” android:tint=“?android:attr/textColorSecondary” android:soundEffectsEnabled=“false” android:contentDescription=“@string/accessibility_volume_settings” /> <com.android.systemui.volume.CaptionsToggleImageButton android:layout_gravity=“centerandroid:id=“@id/odi_captions_icon” android:background=“@drawable/rounded_ripple” android:layout_width=“match_parent” android:layout_height=“match_parent” android:src=“@drawable/ic_volume_odi_captions_disabled” android:tint=“@color/caption_tint_color_selector” android:soundEffectsEnabled=“false” app:optedOut=“false” style=“@style/VolumeButtons” /> 帮我修改安卓11的。volume_dialog.xml的背景为半透明
07-14
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:title="" app:navigationIcon="@drawable/back"> </androidx.appcompat.widget.Toolbar> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@mipmap/picture_8" android:scaleType="centerCrop"/> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_margin="10dp" android:orientation="vertical" android:layout_height="wrap_content"> </androidx.appcompat.widget.LinearLayoutCompat> <TextView android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="泰山,又名岱山、岱宗、岱岳、东岳、泰岳,为五岳之一,有“五岳之首”、“五岳独尊”、“天下第一山”之称,被中外学者称为“中国的奥林匹斯山”,位于山东省中部,隶属于泰安市,绵亘于泰安、济南、淄博三市之间,总面积25000公顷,主峰玉皇顶海拔约1545米"/> </androidx.appcompat.widget.LinearLayoutCompat> </RelativeLayout>
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值