Android-自己定义图像资源的使用

2014年4月29日 
 上一篇博客。介绍前面几种图像资源的使用,本篇博客把剩下的所有介绍完:
  • 普通图像资源
  • XML图像资源
  • Nine-patch图像资源
  • XML Nine-patch图像资源
  • 图层(Layer)图像资源
  • 图像状态(state)资源
  • 图像级别(Level)资源
  • 淡入淡出(transition)资源
  • 嵌入(Inset)图像资源
  • 剪切(Clip)图像资源
  • 比例(Scale)图像资源
  • 外形(Shape)图像资源
有兴趣的朋友能够加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)

图像状态资源的使用

注:部分样例来源《Android应用实战-李宁》。经由本人整理。

按钮状态变化,大家应该知道了吧。按下一个效果。松开又一个效果,这就是状态的改变。
/05_KindOfDrawableUse/res/drawable/button.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" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/focused" />
<item android:drawable="@drawable/normal" />
</selector>

在drawable文件夹下。定义一个selector标签的选择器,并在布局文件里使用

/05_KindOfDrawableUse/res/layout/state_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:text="按钮" /> </LinearLayout>

上面的Button通过设置background来引用我们定义好的selector

效果例如以下:

图像级别资源的使用

/05_KindOfDrawableUse/res/drawable/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>
/05_KindOfDrawableUse/res/layout/level_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <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_LampOn"
android:text="开灯" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick_LampOff"
android:text="关灯" /> </LinearLayout>

/05_KindOfDrawableUse/src/com/wwj/drawable/LevelDrawableRes.java

package com.wwj.drawable;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; /**
* 图像级别资源的使用
*
* 在res/drawable建立图像级别资源
*
* 然后在布局文件的控件中使用
*
* @author wwj
*
*/
public class LevelDrawableRes extends Activity { 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,显示lamp_off.png
ivLamp.setImageLevel(8);
} public void onClick_LampOn(View view) {
// LevelListDrawable levelListDrawable =
// (LevelListDrawable)ivLamp.getDrawable();
// levelListDrawable.setLevel(15);
// 设置Level为15,显示lamp_on.png
ivLamp.setImageLevel(15); } public void onClick_LampOff(View view) {
// 设置Level为6。显示lamp_off.png
ivLamp.getDrawable().setLevel(6); }
}
效果图例如以下:

过渡图像资源的使用

这个图像资源是用来展示图像过渡的,比方一盏灯从不亮到亮的缓慢过渡。

/05_KindOfDrawableUse/res/drawable/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>
/05_KindOfDrawableUse/res/layout/cross_fade_res.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <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="关灯" /> </LinearLayout>

/05_KindOfDrawableUse/src/com/wwj/drawable/CrossFadeDrawableRes.java

package com.wwj.drawable;

import android.app.Activity;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; /**
* 淡入淡出资源的使用
*
* @author wwj
*
*/
public class CrossFadeDrawableRes extends Activity {
private ImageView ivLamp; @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);
}
}

效果图例如以下:

嵌入图像资源的使用

/05_KindOfDrawableUse/res/drawable/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>
<!--
android:insetBottom="10dp" 图像距离下边的距离
android:insetLeft="10dp" 图像距离左标的距离
android:insetRight="10dp" 图像距离右边的距离
android:insetTop="10dp" 图像距离上边的距离
-->
/05_KindOfDrawableUse/res/layout/inset_res.xml
<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/inset" /> </LinearLayout>

效果图例如以下:

剪切图像资源的使用

/05_KindOfDrawableUse/res/drawable/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" />
/05_KindOfDrawableUse/res/layout/clip_res.xml
<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical" > <ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/clip" /> </LinearLayout>

/05_KindOfDrawableUse/src/com/wwj/drawable/ClipDrawableRes.java

package com.wwj.drawable;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.widget.ImageView; /**
* 剪切图像的使用
*
* 在res/drawable文件夹下定义clip资源
*
* @author wwj
*
*/
public class ClipDrawableRes extends Activity {
@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);
}
}

效果图例如以下:

比例图像资源的使用

/05_KindOfDrawableUse/res/drawable/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>

这个比例图片没有效果,不知道为何

外形图像资源的使用

外形图像是用得比較多,能够实现自己想要的效果,比方一个文本框
/05_KindOfDrawableUse/res/drawable/shape.xml
<?

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" > <gradient
android:angle="45"
android:endColor="#80FF00FF"
android:startColor="#FFFF0000" /> <padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" /> <stroke
android:width="2dp"
android:color="#FFF" /> <corners android:radius="8dp" /> </shape>

/05_KindOfDrawableUse/res/layout/shape_res.xml

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/shape"
android:text="Shape Label" /> </LinearLayout>
效果图例如以下:
关于Android提供的所有图像资源已经通过两篇博文给大家介绍完了。一些详细的參数没有列出来,也没有详解,各位想了解很多其它的话,能够到官网查看详细參数的使用。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Android-用你自己的自定义图像资源(2)的更多相关文章

  1. Android开发:UI相关(一)自定义样式资源

    一.自定义样式资源:   1.在drawble中新建xml资源文件:     如果新建的xml文件没有自动放置在drawable文件夹下,就手动移动到drawable下. 2.编写样式代码: < ...

  2. Android资源之图像资源(图像级别资源)

    图像状态资源仅仅能定义有限的几种状态. 假设须要很多其它的状态,就要使用图像级别资源. 在该资源文件里能够定义随意多个图像级别. 每一个图像级别是一个整数区间,能够通过ImageView.setIma ...

  3. Android资源之图像资源(状态图像资源)

    在上一篇博文中.我主要解说了XML图像资源中的图层资源,在此图像资源博文中我会给大家陆续解说XMl图像资源的图像状态资源.图像级别资源.淡入淡出资源.嵌入图像资源.剪切图像资源和外形资源. 1.图像状 ...

  4. Android资源之图像资源(图层图像资源)

    曾经看别人的程序的drawable目录里有xml资源,说实话第一次见到这种xml图像资源时,我真心不知道是干什么的.抽出时间学习了一下图像资源.才了解了这类图像资源的妙用. 以下我来分享一下这部分知识 ...

  5. Android-自己定义图像资源的使用(1)

    Android-自己定义图像资源的使用 2014年4月28日 周一 天气晴朗 心情平静 本篇博文给大家介绍一下,在Android开发中经经常使用到的一些图像资源,具体内容麻烦请各位认真查看官网,下面附 ...

  6. 《photoshop cc 新功能 生成图像资源》智能切图逆天啦!

    作为一个前端工程师切图这个步骤是必不可少的,方式多种多样,有和切图工具的,也有是把要切的图层元素或者组直接新建保存成文件的,现在photoshop cc2015,可以让你轻松切图,摆脱繁琐的切图步骤. ...

  7. 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)

    发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...

  8. Android开发人员必知的开发资源

    developer.android.com 官方开发人员网站推荐资源 在动手编写第一个 Android 应用之前,用心读一读 Android Design 章节.尤其是以下的这些文章: Devices ...

  9. 4.2、Android Studio压缩你的代码和资源

    为了让你的APK文件尽可能的小,你需要在构建的时候开启压缩来移除无用的代码和资源. 代码压缩可在ProGuard中使用,可以检测和清除无用的类,变量,方法和属性,甚至包括你引用的库.ProGuard同 ...

随机推荐

  1. 线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的使用方法

    线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的使用方法 注:使用结构CRITICAL_SECTION 需增加头文件#include “afxmt. ...

  2. 日历的问题C语言,C++(boost),python,Javascript,Java和Matlab实现

    今天看到一个很有意思的话题,例的标题叙述性描述,下面: 根据以下信息来计算1901年1月1至2000年12月31适逢星期日每个月的第一天的合伙人数量? a)  1900.1.1星期一 b)  1月,3 ...

  3. 【安德鲁斯】基于脚本的数据库&quot;增量更新&quot;,如果不改变,每次更新java代码、!

    思维: 1.当然,它是基于SQLiteOpenHelper.onCreate(第一个呼叫建立).onUpdate(当所谓的升级计划) 2.用"脚本"(脚本制作详细方法问度娘)做数据 ...

  4. vim使用(三):.viminfo和.vimrc

    1. viminfo 在vim中操作的行为,vim会自己主动记录下来,保存在 ~/.viminfo 文件里. 这样为了方便下次处理, 如:vim打开文件时,光标会自己主动在上次离开的位置显示. 原来搜 ...

  5. Android适应方案汇总(三)

    在Android适应方案汇总(一个).(两)在.我们理解一些基本概念. 那么详细的开发,我们应该重视起来. 首先,我们需要知道.关键的事实是,这两个适配器: (1).这点在单位的使用上用dp.sp以及 ...

  6. java实现大数相加问题

    闲来没事.写了个acm中常常遇到的大数加减问题的java 解决代码,我想说.用java的BigInteger 非常easy. 大爱java!! 比如: 实现多组输入的大数加减问题: import ja ...

  7. 找呀志_java网络编程(5)TCP和udp差额

    1.TCP定向链接,尽管该网络的不稳定性质,所述不安全确定多少次握手不能保证连接的可靠性.但TCP的三次握手至少(事实上确保了相当大的程度)以确保连接的可靠性; 和UDP不面向连接的,UDP前传送的数 ...

  8. Zygote过程【3】——SystemServer诞生

    欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 在ZygoteInit的main()方法中做了几件大事.当中一件便是启动Systemserver进程.代码例如以下: ...

  9. setsockopt角色

    功能描写叙述: 获取或者设置与某个套接字关联的选 项. 选项可能存在于多层协议中.它们总会出如今最上面的套接字层. 当操作套接字选项时.选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 ...

  10. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...