Android-自定义图像资源的使用(续)

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


  • 普通图像资源
  • XML图像资源
  • Nine-patch图像资源
  • XML Nine-patch图像资源
  • 图层(Layer)图像资源
  • 图像状态(state)资源
  • 图像级别(Level)资源
  • 淡入淡出(transition)资源
  • 嵌入(Inset)图像资源
  • 剪切(Clip)图像资源
  • 比例(Scale)图像资源
  • 外形(Shape)图像资源

图像状态资源的使用

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

按钮状态变化,大家应该知道了吧。按下一个效果,松开又一个效果,这就是状态的改变。

/05_KindOfDrawableUse/res/drawable/button.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true"  
  4.           android:drawable="@drawable/pressed" /> <!-- pressed -->  
  5.     <item android:state_focused="true"  
  6.           android:drawable="@drawable/focused" />   
  7.     <item android:drawable="@drawable/normal" />   
  8. </selector>  

在drawable目录下,定义一个selector标签的选择器,并在布局文件中使用
/05_KindOfDrawableUse/res/layout/state_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="@drawable/button"  
  11.         android:text="按钮" />  
  12.   
  13. </LinearLayout>  

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

效果如下:




图像级别资源的使用

/05_KindOfDrawableUse/res/drawable/lamp_level.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <level-list xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/lamp_off" android:minLevel="6"  
  4.         android:maxLevel="10" />  
  5.     <item android:drawable="@drawable/lamp_on" android:minLevel="12"  
  6.         android:maxLevel="20" />  
  7. </level-list>  


/05_KindOfDrawableUse/res/layout/level_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageview_lamp"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/lamp_level" />  
  12.   
  13.     <Button  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:onClick="onClick_LampOn"  
  17.         android:text="开灯" />  
  18.   
  19.     <Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:onClick="onClick_LampOff"  
  23.         android:text="关灯" />  
  24.   
  25. </LinearLayout>  

/05_KindOfDrawableUse/src/com/wwj/drawable/LevelDrawableRes.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.wwj.drawable;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.ImageView;  
  7.   
  8. /** 
  9.  * 图像级别资源的使用 
  10.  *  
  11.  * 在res/drawable建立图像级别资源 
  12.  *  
  13.  * 然后在布局文件的控件中使用 
  14.  *  
  15.  * @author wwj 
  16.  *  
  17.  */  
  18. public class LevelDrawableRes extends Activity {  
  19.   
  20.     private ImageView ivLamp;  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.level_res);  
  26.   
  27.         ivLamp = (ImageView) findViewById(R.id.imageview_lamp);  
  28.         // 设置Level为8,显示lamp_off.png  
  29.         ivLamp.setImageLevel(8);  
  30.     }  
  31.   
  32.     public void onClick_LampOn(View view) {  
  33.         // LevelListDrawable levelListDrawable =  
  34.         // (LevelListDrawable)ivLamp.getDrawable();  
  35.         // levelListDrawable.setLevel(15);  
  36.         // 设置Level为15,显示lamp_on.png  
  37.         ivLamp.setImageLevel(15);  
  38.   
  39.     }  
  40.   
  41.     public void onClick_LampOff(View view) {  
  42.         // 设置Level为6,显示lamp_off.png  
  43.         ivLamp.getDrawable().setLevel(6);  
  44.   
  45.     }  
  46. }  


效果图如下:



过渡图像资源的使用

这个图像资源是用来展示图像过渡的,比如一盏灯从不亮到亮的缓慢过渡。
/05_KindOfDrawableUse/res/drawable/lamp_transition.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <transition xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/lamp_off" />  
  4.     <item android:drawable="@drawable/lamp_on" />  
  5. </transition>  


/05_KindOfDrawableUse/res/layout/cross_fade_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageview_lamp"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/lamp_transition" />  
  12.   
  13.     <Button  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:onClick="onClick_LampOn"  
  17.         android:text="开灯" />  
  18.   
  19.     <Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:onClick="onClick_LampOff"  
  23.         android:text="关灯" />  
  24.   
  25. </LinearLayout>  

/05_KindOfDrawableUse/src/com/wwj/drawable/CrossFadeDrawableRes.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.wwj.drawable;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.TransitionDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.ImageView;  
  8.   
  9. /** 
  10.  * 淡入淡出资源的使用 
  11.  *  
  12.  * @author wwj 
  13.  *  
  14.  */  
  15. public class CrossFadeDrawableRes extends Activity {  
  16.     private ImageView ivLamp;  
  17.   
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.cross_fade_res);  
  22.   
  23.         ivLamp = (ImageView) findViewById(R.id.imageview_lamp);  
  24.     }  
  25.   
  26.     public void onClick_LampOn(View view) {  
  27.         // 从第一个图像切换到第二个图像。其中使用1秒的时间完成淡入淡出效果  
  28.         TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();  
  29.         drawable.startTransition(1000);  
  30.     }  
  31.   
  32.     public void onClick_LampOff(View view) {  
  33.         // 从第二个图像切换第一个图像。其中使用1秒的时间完成淡入淡出效果  
  34.         TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();  
  35.         drawable.reverseTransition(1000);  
  36.     }  
  37. }  

效果图如下:



嵌入图像资源的使用

/05_KindOfDrawableUse/res/drawable/inset.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <inset xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:drawable="@drawable/logo"  
  4.     android:insetBottom="10dp"  
  5.     android:insetLeft="10dp"  
  6.     android:insetRight="10dp"  
  7.     android:insetTop="10dp" >  
  8.   
  9. </inset>  
  10. <!--  
  11.      android:insetBottom="10dp" 图像距离下边的距离  
  12.     android:insetLeft="10dp" 图像距离左标的距离  
  13.     android:insetRight="10dp" 图像距离右边的距离  
  14.     android:insetTop="10dp" 图像距离上边的距离  
  15. -->  


/05_KindOfDrawableUse/res/layout/inset_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="@drawable/inset" />  
  11.   
  12. </LinearLayout>  

效果图如下:



剪切图像资源的使用

/05_KindOfDrawableUse/res/drawable/clip.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <clip xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:clipOrientation="horizontal"  
  4.     android:drawable="@drawable/progress"  
  5.     android:gravity="left" />  


/05_KindOfDrawableUse/res/layout/clip_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/background"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/image"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/clip" />  
  13.   
  14. </LinearLayout>  

/05_KindOfDrawableUse/src/com/wwj/drawable/ClipDrawableRes.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.wwj.drawable;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.ClipDrawable;  
  5. import android.os.Bundle;  
  6. import android.widget.ImageView;  
  7.   
  8. /** 
  9.  * 剪切图像的使用 
  10.  *  
  11.  * 在res/drawable目录下定义clip资源 
  12.  *  
  13.  * @author wwj 
  14.  *  
  15.  */  
  16. public class ClipDrawableRes extends Activity {  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.clip_res);  
  21.         ImageView imageview = (ImageView) findViewById(R.id.image);  
  22.         ClipDrawable drawable = (ClipDrawable) imageview.getBackground();  
  23.         // 截取30%的图像  
  24.         drawable.setLevel(3000);  
  25.     }  
  26. }  

效果图如下:



比例图像资源的使用

/05_KindOfDrawableUse/res/drawable/scale.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:drawable="@drawable/logo"  
  4.     android:scaleGravity="center_vertical|center_horizontal"  
  5.     android:scaleHeight="80%"  
  6.     android:scaleWidth="80%" >  
  7.   
  8. </scale>  

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

外形图像资源的使用

外形图像是用得比较多,可以实现自己想要的效果,比如一个文本框
/05_KindOfDrawableUse/res/drawable/shape.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle" >  
  4.   
  5.     <gradient  
  6.         android:angle="45"  
  7.         android:endColor="#80FF00FF"  
  8.         android:startColor="#FFFF0000" />  
  9.   
  10.     <padding  
  11.         android:bottom="7dp"  
  12.         android:left="7dp"  
  13.         android:right="7dp"  
  14.         android:top="7dp" />  
  15.   
  16.     <stroke  
  17.         android:width="2dp"  
  18.         android:color="#FFF" />  
  19.   
  20.     <corners android:radius="8dp" />  
  21.   
  22. </shape>  

/05_KindOfDrawableUse/res/layout/shape_res.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_margin="20dp"  
  11.         android:background="@drawable/shape"  
  12.         android:text="Shape Label" />  
  13.   
  14. </LinearLayout>  


效果图如下:



关于Android提供的所有图像资源已经通过两篇博文给大家介绍完了,一些具体的参数没有列出来,也没有详细解释,各位想了解更多的话,可以到官网查看具体参数的使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本火锅店点餐系统采用Java语言和Vue技术,框架采用SSM,搭配Mysql数据库,运行在Idea里,采用小程序模式。本火锅店点餐系统提供管理员、用户两种角色的服务。总的功能包括菜品的查询、菜品的购买、餐桌预定和订单管理。本系统可以帮助管理员更新菜品信息和管理订单信息,帮助用户实现在线的点餐方式,并可以实现餐桌预定。本系统采用成熟技术开发可以完成点餐管理的相关工作。 本系统的功能围绕用户、管理员两种权限设计。根据不同权限的不同需求设计出更符合用户要求的功能。本系统中管理员主要负责审核管理用户,发布分享新的菜品,审核用户的订餐信息和餐桌预定信息等,用户可以对需要的菜品进行购买、预定餐桌等。用户可以管理个人资料、查询菜品、在线点餐和预定餐桌、管理订单等,用户的个人资料是由管理员添加用户资料时产生,用户的订单内容由用户在购买菜品时产生,用户预定信息由用户在预定餐桌操作时产生。 本系统的功能设计为管理员、用户两部分。管理员为菜品管理、菜品分类管理、用户管理、订单管理等,用户的功能为查询菜品,在线点餐、预定餐桌、管理个人信息等。 管理员负责用户信息的删除和管理,用户的姓名和手机号都可以由管理员在此功能里看到。管理员可以对菜品的信息进行管理、审核。本功能可以实现菜品的定时更新和审核管理。本功能包括查询餐桌,也可以发布新的餐桌信息。管理员可以查询已预定的餐桌,并进行审核。管理员可以管理公告和系统的轮播图,可以安排活动。管理员可以对个人的资料进行修改和管理,管理员还可以在本功能里修改密码。管理员可以查询用户的订单,并完成菜品的安排。 当用户登录进系统后可以修改自己的资料,可以使自己信息的保持正确性。还可以修改密码。用户可以浏览所有的菜品,可以查看详细的菜品内容,也可以进行菜品的点餐。在本功能里用户可以进行点餐。用户可以浏览没有预定出去的餐桌,选择合适的餐桌可以进行预定。用户可以管理购物车里的菜品。用户可以管理自己的订单,在订单管理界面里也可以进行查询操作。
Android中,可以使用自定义视图和画布来实现图像裁剪。 首先,创建一个自定义的视图类,并在其中重写onDraw()方法。在onDraw()方法中,使用画布(Canvas)对象绘制需要裁剪的图像。然后,使用画布对象的clipRect()方法来指定需要裁剪的区域。最后,使用画布对象的drawBitmap()方法来绘制裁剪后的图像。 以下是一个简单的示例代码: ```java public class CustomImageView extends View { private Bitmap mBitmap; public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制原始图像 canvas.drawBitmap(mBitmap, 0, 0, null); // 裁剪区域 Rect rect = new Rect(100, 100, 300, 300); canvas.clipRect(rect); // 绘制裁剪后的图像 canvas.drawBitmap(mBitmap, 0, 0, null); } } ``` 在这个示例代码中,我们首先使用BitmapFactory类从资源文件中加载一个图像。然后,在onDraw()方法中,我们使用Canvas对象绘制该图像。接下来,我们创建一个矩形对象,指定需要裁剪的区域。然后,我们使用Canvas对象的clipRect()方法将裁剪区域设置为该矩形。最后,我们再次使用Canvas对象绘制图像,此时只会绘制裁剪区域内的部分。 当然,这只是一个简单的示例。你可以根据自己的需求和想象力来实现更复杂的图像裁剪效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值