Android-用你自己的自定义图像资源(2)
Android-自己定义图像资源的使用
- 普通图像资源
- XML图像资源
- Nine-patch图像资源
- XML Nine-patch图像资源
- 图层(Layer)图像资源
- 图像状态(state)资源
- 图像级别(Level)资源
- 淡入淡出(transition)资源
- 嵌入(Inset)图像资源
- 剪切(Clip)图像资源
- 比例(Scale)图像资源
- 外形(Shape)图像资源
图像状态资源的使用
<? 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标签的选择器,并在布局文件里使用
<?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
图像级别资源的使用
<?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>
<?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); }
}
过渡图像资源的使用
<?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>
<?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);
}
}
效果图例如以下:
嵌入图像资源的使用
<?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" 图像距离上边的距离
-->
<?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>
效果图例如以下:
剪切图像资源的使用
<? 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" />
<? 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);
}
}
效果图例如以下:
比例图像资源的使用
<?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>
这个比例图片没有效果,不知道为何
外形图像资源的使用
<? 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-用你自己的自定义图像资源(2)的更多相关文章
- Android开发:UI相关(一)自定义样式资源
一.自定义样式资源: 1.在drawble中新建xml资源文件: 如果新建的xml文件没有自动放置在drawable文件夹下,就手动移动到drawable下. 2.编写样式代码: < ...
- Android资源之图像资源(图像级别资源)
图像状态资源仅仅能定义有限的几种状态. 假设须要很多其它的状态,就要使用图像级别资源. 在该资源文件里能够定义随意多个图像级别. 每一个图像级别是一个整数区间,能够通过ImageView.setIma ...
- Android资源之图像资源(状态图像资源)
在上一篇博文中.我主要解说了XML图像资源中的图层资源,在此图像资源博文中我会给大家陆续解说XMl图像资源的图像状态资源.图像级别资源.淡入淡出资源.嵌入图像资源.剪切图像资源和外形资源. 1.图像状 ...
- Android资源之图像资源(图层图像资源)
曾经看别人的程序的drawable目录里有xml资源,说实话第一次见到这种xml图像资源时,我真心不知道是干什么的.抽出时间学习了一下图像资源.才了解了这类图像资源的妙用. 以下我来分享一下这部分知识 ...
- Android-自己定义图像资源的使用(1)
Android-自己定义图像资源的使用 2014年4月28日 周一 天气晴朗 心情平静 本篇博文给大家介绍一下,在Android开发中经经常使用到的一些图像资源,具体内容麻烦请各位认真查看官网,下面附 ...
- 《photoshop cc 新功能 生成图像资源》智能切图逆天啦!
作为一个前端工程师切图这个步骤是必不可少的,方式多种多样,有和切图工具的,也有是把要切的图层元素或者组直接新建保存成文件的,现在photoshop cc2015,可以让你轻松切图,摆脱繁琐的切图步骤. ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- Android开发人员必知的开发资源
developer.android.com 官方开发人员网站推荐资源 在动手编写第一个 Android 应用之前,用心读一读 Android Design 章节.尤其是以下的这些文章: Devices ...
- 4.2、Android Studio压缩你的代码和资源
为了让你的APK文件尽可能的小,你需要在构建的时候开启压缩来移除无用的代码和资源. 代码压缩可在ProGuard中使用,可以检测和清除无用的类,变量,方法和属性,甚至包括你引用的库.ProGuard同 ...
随机推荐
- 关于Hbase的cache配置
关于Hbase的cache配置 在hbase中的hfilecache中,0.96版本号中新添加了bucket cache, bucket cache通过把hbase.offheapcache.perc ...
- 开放搜索服务OpenSearch
开放搜索服务系统架构:从系统.平台到开放服务 搜索是各类网站和数据类APP的标配功能.目前开发者一般基于开源搜索系统,例如ElasticSearch.Solr.Sphinx等自己搭建搜索服务,系统定制 ...
- 全面认识Eclipse中JVM内存设置(转)
这里向大家描述一下Eclipse中如何进行JVM内存设置,JVM主要管理两种类型的内存:堆和非堆.简单来说堆就是Java代码可及的内存,是留给开发人员使用的:非堆就是JVM留给自己用的,所以方法区.J ...
- poj 3101 Astronomy(分数的最小公倍数)
http://poj.org/problem? id=3101 大致题意:求n个运动周期不全然同样的天体在一条直线上的周期. 这题我是看解题报告写的,没想到选用參照物,用到了物理中的角速度什么的. 由 ...
- Java的Log系统介绍和切换(转)
Java的log系统比较繁杂.在这里梳理一下.本文只涉及log系统介绍和处理log系统之间的切换.不涉及如何配置和使用. 具体的log系统 Log4j:准确的说是log4j 1.x版.是之前使用最广泛 ...
- Chromium
Chromium多进程架构 多进程架构 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Bac ...
- Android呼叫开发系列WebService
我在学习Android第一个问题是在发展进程中遇到Androidclient究竟是怎么用server与数据库交互它?问题是,我有初步接触Android这困扰了我一个非常大的问题.天直到几年前,我突然想 ...
- [2014 Regional]牡丹江 H Hierarchical Notation 做题记录
主妇:老年人谁是炮灰牡丹江,我们的团队只是做同步大赛 他决定开爆震H什么时候,A 5min 1Y.I在该限制后,纠结了很久30min+ 1Y,神继续承担各种位置卡D在,hpp见B我认为这是非常熟悉的研 ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- 让你的字ScrollView、ListView充分伸展
android默认ScrollView.ListView在最顶部的下拉上拉时或底部,未与反弹效应,很僵,让你无法继续拖累,不比iOS至于能否反弹.个人觉得,iOS互动还是略胜一筹.因此,我们已经走在A ...