zoukankan      html  css  js  c++  java
  • ImageView属性

    一、结构

        java.lang.Object

          android.view.View

            android.widget.ImageView

        已知直接子类:

        ImageButton, QuickContactBadge 

        已知间接子类:

        ZoomButton

    二、类概述

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

    三、XML属性

    属性名称 描述
    android:adjustViewBounds 是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。
    android:cropToPadding

    是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:

    android:maxHeight

    设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

    1) 设置setAdjustViewBounds为true;

    2) 设置maxWidth、MaxHeight;

    3) 设置设置layout_width和layout_height为wrap_content。

    android:maxWidth

    设置View的最大宽度。同上。

    android:scaleType

    设置图片的填充方式。

    matrix:用矩阵来绘图

    fitXY:拉伸图片(不按比例)以填充View的宽高,layout_height:30px,layout_120px

    fitStart:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边

    fitCenter:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

    fitEnd:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边

    center:按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示,layout_height:60px,layout_80px,padding:10px

    centerCrop:按比例放大原图直至等于某边View的宽高显示。

    centerInside:当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。

     android:src  设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)
     android:tint  

    将图片渲染成指定的颜色。见下图:

    左边为原图,右边为设置后的效果,见后面代码。

    四、代码  

    4.1  android:tint

    1 <ImageView android:background="@android:color/white" android:src="@drawable/btn_mode_switch_bg"  
    2         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>  
    3     <ImageView android:layout_marginLeft="5dp" android:background="@android:color/white" android:tint="#ffff00" android:src="@drawable/btn_mode_switch_bg"  
    4         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

    4.2  android:cropToPadding

    1 <ImageView android:background="@android:color/white" android:scrollY="-10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"  
    2         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>  
    3     <ImageView android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"  
    4         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>  
    5     <ImageView android:paddingTop="10px"  android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"  
    6         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>  
    7     <ImageView android:paddingTop="10px" android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="false" android:src="@drawable/btn_mode_switch_bg"  
    8         android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
  • 相关阅读:
    7个技巧,帮你完美搞定网页首图设计(必看)
    听说你想跳槽?ARM和PowerPC分析,你知道吗?(速进)
    C语言必学7大步骤!(必看)
    单片机电机必不可少的30条常识!你知道吗?(欢迎大家进行补充)
    单片机外围电路设计攻略(终结版)! 不看哭一年!
    3天”速成“嵌入式之后,我明白了六件事!!!
    前端就不需要掌握算法与数据结构?
    嵌入式软件必学知识总结!
    字节跳动2017客户端工程师实习生笔试题-第四题
    并查集
  • 原文地址:https://www.cnblogs.com/heimianshusheng/p/5017343.html
Copyright © 2011-2022 走看看