ImageView详解 ScaleType 安卓无忧第二篇

ImageView的全路径:android.widget.ImageView.显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

1 XML属性

android:layout_width=""---控件宽

android:layout_height=""---控件高

android:scaleType=""---控件如何显示

参数:

center---按图片原来的尺寸居中显示,当图片的长(宽)超过view的长(宽),则截取图片居中部分显示

centerCrop---按比例扩大图片的尺寸居中显示,使得图片长(宽)等于或大于view的长(宽)

centerInside---将图片的内容完整居中显示,通过按比例缩小或原来的尺寸使得图片长(宽)小于或等于view的长(宽)

fitCenter---把图片按比例扩大/缩小到view的宽度,居中显示

fitEnd---把图片按比例扩大/缩小到view的宽度,显示在view的下半部分位置

fitStart---把图片按比例扩大/缩小到view 的宽度,显示在view的上半部分位置

fitXY---把图片不按比例扩大/缩小到view的大小显示

matrix---用矩阵来绘制

2 ImageView中常用方法说明

setAlpha(intalpha) 设置ImageView的透明度

setImageBitmap(Bitmapbm) 设置ImageView所显示的内容

为指定的Bitmap对象

setImageDrawable(Drawabledrawable) 设置ImageView所显示的内容

为指定的Drawable对象

setImageResource(intresId) 设置ImageView所显示的内

容为指定id的资源

setImageURI(Uriuri) 设置ImageView所显示的内

容为指定Uri

setSelected(booleanselected) 设置ImageView的选中状态


3 ImageView例子





例子代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:text="原图" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/head7" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:text="AdjustViewBounds
            ImageView大小自动调整,ImageView 没有红色背景" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:adjustViewBounds="true"
                android:background="#ff0000"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:adjustViewBounds="true"
                android:background="#ff0000"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:adjustViewBounds="true"
                android:background="#ff0000"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:text="cropToPadding属性" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="#ff0000"
                android:cropToPadding="true"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:scrollY="-10dp"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:cropToPadding="true"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:scrollY="10dp"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:cropToPadding="true"
                android:maxHeight="170dp"
                android:maxWidth="140dp"
                android:scrollY="20dp"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="140dp"
                android:background="#ff0000"
                android:scaleType="fitXY"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitXY"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitXY"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="fitStart" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="140dp"
                android:background="#ff0000"
                android:scaleType="fitStart"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitStart"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitStart"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="fitEnd" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="140dp"
                android:background="#ff0000"
                android:scaleType="fitEnd"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitEnd"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitEnd"
                android:src="@drawable/head7" />
        </LinearLayout>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="fitCenter" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="90dp"
                android:background="#ff0000"
                android:scaleType="fitCenter"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitCenter"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="fitCenter"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="center" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="140dp"
                android:background="#ff0000"
                android:scaleType="center"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="center"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="center"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="centerInside" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="90dp"
                android:background="#ff0000"
                android:scaleType="centerInside"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="centerInside"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="centerInside"
                android:src="@drawable/head7" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="centerCrop" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="90dp"
                android:background="#ff0000"
                android:scaleType="centerCrop"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="centerCrop"
                android:src="@drawable/head7" />

            <ImageView
                android:layout_width="170dp"
                android:layout_height="180dp"
                android:layout_marginLeft="10dp"
                android:background="#ff0000"
                android:scaleType="centerCrop"
                android:src="@drawable/head7" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

最后,例子都来源于安卓无忧,请去应用宝下载安卓无忧http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy,源码例子文档一网打尽。百度网盘地址:http://yun.baidu.com/share/link?shareid=996618543&uk=1000858045




最后,例子都来源于安卓无忧,请去应用宝下载安卓无忧http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy,源码例子文档一网打尽。百度网盘地址:http://yun.baidu.com/share/link?shareid=996618543&uk=1000858045

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值