ANDROID XML图像资源文件详细讲解(一)
阅读原文时间:2021年04月20日阅读:1

图像资源在android开发过程中使用非常平凡。为了让自己在开发中,能轻松解决任何有关图像的问题,所以必须全面掌握最基本的知识点----各种类型的图像资源文件。

前言:

前言的前言:因为内容较多,此系列将分多篇文章记载。

1、关于图片格式

android使用的图片格式有3种:png、jpg、gif。

官方推荐使用png格式的图片。

jpg格式的图片是不被推荐使用的。

gif格式的图片是建议不使用的。

2、图片资源调用

1、通过resource ID进行Bitmap资源的调用,例如getDrawable(int)、android:drawable或者android:icon等,一般bitmap资源文件放在res/drawable-XXXX文件夹下,这也是推荐的存放位置,因为放在此文件夹下的图片资源android aapt工具会自动优化图片资源文件,例如将24-bit位图或者32-bit位图降色到8-bit位图,以节省内存,同时也不会失真。

2、如果你不希望对图片进行优化处理,可以将图片放到res\raw文件夹下

图像资源的种类

图像资源类型

名称

元素标签

Bitmap File

Nine-Patch File

Layer List

State List

Level List

Transition Drawable

Inset Drawable

Clip Drawable

Scale Drawable

Shape Drawable

以上是android应用程序图像资源的所有类型。下面我们对每一种类型的使用进行说明。

介绍:

此标签可以单独使用在ImageView中,但是主要是结合Layer List或者State List等集合类型,作为其使用。

使用格式为:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

         属性解释如下:

**xmlns:android**

定义命名空间,android的所有根标签都需要定义其命名空间(http://schemas.android.com/apk/res/android),如果对XML比较了解的童鞋,这点应该很容易理解。不了解的童鞋可以先在网上查一下XML的命名空间。

**android:src**

设置bitmap的源文件。通过@drawable/XXXXX获取。

**android:antialias**

是否消除\优化锯齿。取值true或者false

**android:dither** 消除抖动。简单点说就是当图像颜色值很丰富(比如通过高级的单反镜头,有216个色系),和手机显示屏能提供的颜色值(比如只有16个色系)不匹配&nbsp;&nbsp; 时,会自动进行补全优化,让画质更好。_取值true或者false_ **android:filter**

这也是一个改善、优化图片显示效果的属性。当位图被拉伸或者压缩时,使图片表面看起来平整光滑。

**android:gravity**

设置图片的显示位置。

取值如下:

top

显示在所在容器的顶部,不会改变图片的This is the default大小

bottom

显示在所在容器的底部,不会改变图片的大小

left

显示在所在容器的左边,不会改变图片的大小

right

显示在所在容器的右边,不会改变图片的大小

center_vertical

垂直居中,不会改变图片的大小

fill_vertical

垂直拉伸,改变图片垂直方向的大小

center_horizontal

横向居中,不会改变图片的大小

fill_horizontal

横向拉伸,改变图片横向方向的大小

center

居中,不会改变图片的大小

fill

横向和垂直同时拉伸到容器大小。这是默认值。

clip_vertical

附加属性,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪。

clip_horizontal

附加属性,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

android:tileMode

图像重复平铺.如果此属性的值不是disabled,则android:gravity属性将会失效。反之,只有此属性是disabled,设置

取值如下:

disabled:无效,这也是tileMode的默认值。

原图 bbb.png:

clamp:重复图片的边缘颜色。效果如下图:

repeat:重复平铺图片。效果如下图:

mirror:镜像平铺图片.效果如下图:

下面附上主要代码:

1、res/layout/activity_main.xml

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bitmap_elements2"/>


</LinearLayout>

      2、res/drawable/bitmap_elements2.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bbb"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:gravity="fill"  //如果设置了tileMode,gravity将失效
    android:tileMode="mirror"/>

     

完毕。