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. The mell hall——坑爹

    The mell hall 题目描述 In HUST,there are always manystudents go to the mell hall at the same time as soo ...

  2. lightoj1038(期望dp)

    给定一个数字d,随机选择一个d的约数,然后让d除以这个约数,形成新的d,不断继续这个步骤,知道d=1为止, 要我们求将d变为1的期望次数 设d1,d2...dj是除以约数后,形成的行的d,且dj==d ...

  3. macbook连接linuxserver后不显示matlab桌面[问题]

    macbook 登录远程linuxserver.Macbook今天,系统版本号10.9.4.这是更新,打开matlab当提醒x11不存在.一个接着安装Xquarz2.7.6. matlab它可以在本地 ...

  4. 《5》CentOS7.0+OpenStack+kvm云平台的部署—组态Horizon

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  5. POJ 1274 The Perfect Stall 水二分匹配

    主题链接:id=1274">点击打开链接 呵呵 #include<cstdio> #include<cstring> #include<cstdlib&g ...

  6. Custom Media Player in WPF (Part 1)

    First of all I would like to welcome everyone to my new blog and wish you all a happy new year… Thro ...

  7. 谈谈Linux内存释放

    上上周吧,一个朋友问我说他公司的服务器内存free 为0 是为什么,意思大概是内存去哪了,这引发了一个小小的讨论,也就是内存释放的问题… 首先我们可能会用free 去查看内存的使用率,它应该是这样的 ...

  8. Codeforces Round #257 (Div. 2) 题解

    Problem A A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes inp ...

  9. java线程池的注意事项

    java线程池是建立一个技术进步的线程.到来之前线程池打造一批在线程的线程尚未,队列上的备用,然后再利用这些资源.减少频繁创建和销毁对象, 1.jdk1.5以上提供了现成的线程池 2.java线程池的 ...

  10. NSOperation 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示

    // // ImageDownloader.h // NSOperationTest // // Created by ydc on 11-10-29. // Copyright 2011年 __My ...