Android-用你自己的自定义图像资源(2) 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
在drawable文件夹下。定义一个selector标签的选择器,并在布局文件里使用
/05_KindOfDrawableUse/res/layout/state_res.xml
上面的Button通过设置background来引用我们定义好的selector
效果例如以下:
图像级别资源的使用
/05_KindOfDrawableUse/res/drawable/lamp_level.xml
/05_KindOfDrawableUse/res/layout/level_res.xml
/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
> <transition xmlns:andro> <item android:drawable="@drawable/lamp_off" /> <item android:drawable="@drawable/lamp_on" /> </transition>
/05_KindOfDrawableUse/res/layout/cross_fade_res.xml
/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
> <inset xmlns:andro 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
> <LinearLayout xmlns:andro 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
/05_KindOfDrawableUse/res/layout/clip_res.xml
/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
这个比例图片没有效果,不知道为何
外形图像资源的使用
外形图像是用得比較多,能够实现自己想要的效果,比方一个文本框
/05_KindOfDrawableUse/res/drawable/shape.xml
/05_KindOfDrawableUse/res/layout/shape_res.xml
> <LinearLayout xmlns:andro 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提供的所有图像资源已经通过两篇博文给大家介绍完了。一些详细的參数没有列出来,也没有详解,各位想了解很多其它的话,能够到官网查看详细參数的使用。
版权声明:本文博客原创文章,博客,未经同意,不得转载。