android通过drawable资源实现常用的自定义效果

1.首先来介绍bitmap标签的使用:/MyProgram/res/drawable/drawable_layer.xml

<?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"] />

节点属性介绍:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="类型:String。定义了XML的命名空间,必须是<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>。
                   如果<bitmap>是根元素,那么他是必须的,如果是嵌套在<itme>里面,那么就不是必须的。"
    android:src="类型:Drawable resource。必需。 引用一个drawableresource"
    android:antialias="类型:Boolean。是否开启抗锯齿。"
    android:dither="类型:Boolean。如果位图与屏幕的像素配置不同时,是否允许抖动"
    android:filter="类型:Boolean。是否允许对位图进行滤波。对位图进行收缩或者延展使用滤波可以获得平滑的外观效果。"
    android:gravity="<em>类型:</em>关键字。定义位图的重力(gravity),如果位图小于其容器,使用重力指明在何处。多个属性之间用  |  分隔。"
    android:mipMap=["true" | "false"]
    android:tileMode="<em>类型:Keyword</em>。定义了tile模式。当tile模式被启用,位图是重复的,并且gravity属性将被忽略。/>

2.nine-patch标签

我们在应用开发当中,时刻需要对图片进行处理,为了让图片被拉伸的时候不会变形和扭曲,让图片边缘部分过渡得更加平滑自然。这时就要用到draw9patch.bat这个工具。

:\ADT\sdk\tools\draw9patch.bat

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="false"
    android:src="@drawable/nine_patch" >

</nine-patch>

src所对应的资源文件需要时.9格式的图片

在布局文件中引用:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/xml_ninepatch" />

3.layer-list标签

将资源文件一层一层的覆盖上去

layertest.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/src01" />
    </item>
    <item
        android:left="10dp"
        android:top="10dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/src02" />
    </item>
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/src03" />
    </item>

</layer-list>

在布局文件中引用:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/layertest" />

4.selector标签

selector.xml用来控制按钮等空间的点击效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/pressed" />
    <item android:state_focused="true"
          android:drawable="@drawable/focused" /> 
    <item android:drawable="@drawable/normal" /> 
</selector>

在布局中的引用

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:text="按钮" />

5.layer-list

图像级别资源的使用

lamp_level.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:drawable="@drawable/lamp_off" android:minLevel="6"
		android:maxLevel="10" />
	<item android:drawable="@drawable/lamp_on" android:minLevel="12"
		android:maxLevel="20" />
</level-list>

在布局文件中的引用

<ImageView
        android:id="@+id/imageview_lamp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/lamp_level" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick_effect1"
        android:text="效果1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick_effect2"
        android:text="效果2" />

在Java文件中的使用

private ImageView ivLamp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.level_res);

		ivLamp = (ImageView) findViewById(R.id.imageview_lamp);
		// 设置Level为8
		ivLamp.setImageLevel(8);
	}

	public void onClick_effect1(View view) {
		// LevelListDrawable levelListDrawable =
		// (LevelListDrawable)ivLamp.getDrawable();
		// levelListDrawable.setLevel(15);
		// 设置Level为15
		ivLamp.setImageLevel(15);

	}

	public void onClick_effect1(View view) {
		// 设置Level为6
		ivLamp.getDrawable().setLevel(6);

	}

6.transition 标签

用来产生过度效果

lamp_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:drawable="@drawable/lamp_off" />
	<item android:drawable="@drawable/lamp_on" />
</transition>

布局文件中引用

<ImageView
        android:id="@+id/imageview_lamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/lamp_transition" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick_LampOn"
        android:text="开灯" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick_LampOff"
        android:text="关灯" />

Java文件中的使用

@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cross_fade_res);

		ivLamp = (ImageView) findViewById(R.id.imageview_lamp);
	}

	public void onClick_LampOn(View view) {
		// 从第一个图像切换到第二个图像。其中使用1秒的时间完成淡入淡出效果
		TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();
		drawable.startTransition(1000);
	}

	public void onClick_LampOff(View view) {
		// 从第二个图像切换第一个图像。其中使用1秒的时间完成淡入淡出效果
		TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();
		drawable.reverseTransition(1000);
	}

7.inset 标签

嵌入图像资源的使用

inset.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:insetBottom="10dp"
    android:insetLeft="10dp"
    android:insetRight="10dp"
    android:insetTop="10dp" >

</inset>

标签属性介绍

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:insetBottom="图像距离下边的距离"
    android:insetLeft="图像距离左标的距离"
    android:insetRight="图像距离右边的距离"
    android:insetTop="图像距离上边的距离" >

</inset>

在布局中的引用

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/inset" />

8.clip 标签

剪切图像资源的使用

clip.xml

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@drawable/progress"
    android:gravity="left" />

在布局文件中的引用

<ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/clip" />

Java代码中的使用

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clip_res);
		ImageView imageview = (ImageView) findViewById(R.id.image);
		ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
		// 截取30%的图像
		drawable.setLevel(3000);
	}

9.scale 标签

比例图像资源的使用

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" >

</scale>

在布局文件中的引用

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/scale" />

10.shape 标签

图形资源使用

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <!-- 圆角 -->
    <corners
	android:radius="9dp"
	android:topLeftRadius="2dp"
	android:topRightRadius="2dp"
	android:bottomLeftRadius="2dp"
	android:bottomRightRadius="2dp"/><!-- 设置圆角半径 -->
    <!-- 渐变 -->
    <gradient
	android:angle="45"
	android:endColor="#80FF00FF"
	android:startColor="#FFFF0000" />

    <!-- 间隔 -->
    <padding
	android:bottom="7dp"
	android:left="7dp"
	android:right="7dp"
	android:top="7dp" /><!-- 各方向的间隔 -->

    <!-- 填充 -->
    <solid
	android:color="@android:color/white"/><!-- 填充的颜色 -->

    <!-- 大小 -->
    <size
	android:width="50dp"
	android:height="50dp"/><!-- 宽度和高度 -->

    <!-- 描边 -->
    <stroke
	android:width="2dp"
	android:color="#FFF" />

    <corners android:radius="8dp" />

</shape>

在布局文件中的引用

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/shape"
        android:text="Shape Label" />
好了,常用的drawable资源就总结到这,效果要自己去尝试,希望多学习和工作有所帮助!



























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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值