Android图片浏览源码解读
创始人
2024-06-22 01:30:18
0

Android手机操作系统的应用方式灵活,简单,深受广大编程爱好者的喜爱。尤其是它的开源代码,使得我们能够方便的得到自己想要的功能需求。今天我们就为大家带来了有关Android图片浏览的相关方法。

首先是Android图片浏览中layout xml:

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < RelativeLayout xmlns:android="http://schemas.Android.com/apk/res/Android"   
  3. Android:layout_width="fill_parent"   
  4. Android:layout_height="fill_parent">   
  5. < ImageSwitcher Android:id="@+id/switcher" 
  6. Android:layout_width="fill_parent" 
  7. Android:layout_height="fill_parent" 
  8. Android:layout_alignParentTop="true" 
  9. Android:layout_alignParentLeft="true" 
  10. /> 
  11. < Gallery Android:id="@+id/gallery" 
  12. Android:background="#55000000" 
  13. Android:layout_width="fill_parent" 
  14. Android:layout_height="60dp" 
  15. Android:layout_alignParentBottom="true" 
  16. Android:layout_alignParentLeft="true" 
  17. Android:gravity="center_vertical" 
  18. Android:spacing="16dp" 
  19. /> 
  20. < /RelativeLayout> 

layout里面用到了前面所说的两个控件,ImageSwitcher用啦显示全图,Gallery用来显示缩略图。着重看看ImageSwitcher,在ImageSwitcher1中需要实现ViewSwitcher.ViewFactory这个接口,这个接口里有个方法makeView,这样就产生了用来显示图片的view. ImageSwitcher调用过程是这样的,首先要有一个Factory为它提供一个View,然后ImageSwitcher就可以初始化各种资源了。注意在使用一个ImageSwitcher之前,一定要调用setFactory方法,要不setImageResource这个方法会报空指针异常。

下面是Android图片浏览代码:

  1. package com.zx.imageswitcher;  
  2. import Android.app.Activity;  
  3. import Android.content.Context;  
  4. import Android.os.Bundle;  
  5. import Android.view.View;  
  6. import Android.view.ViewGroup;  
  7. import Android.view.animation.AnimationUtils;  
  8. import Android.widget.AdapterView;  
  9. import Android.widget.BaseAdapter;  
  10. import Android.widget.Gallery;  
  11. import Android.widget.ImageSwitcher;  
  12. import Android.widget.ImageView;  
  13. import Android.widget.ViewSwitcher;  
  14. import Android.widget.Gallery.LayoutParams;  
  15. public class ImageSwitcherTest extends Activity implements  
  16. AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{  
  17. private ImageSwitcher mSwitcher;  
  18. private Integer[] mThumbIds = {  
  19. R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,  
  20. R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,  
  21. R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,  
  22. R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};  
  23. private Integer[] mImageIds = {  
  24. R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,  
  25. R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,  
  26. R.drawable.sample_6, R.drawable.sample_7};  
  27. /** Called when the activity is first created. */  
  28. @Override  
  29. public void onCreate(Bundle savedInstanceState) {  
  30. super.onCreate(savedInstanceState);  
  31. setContentView(R.layout.main);  
  32. mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);  
  33. mSwitcher.setFactory(this);  
  34. mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  35. Android.R.anim.fade_in));  
  36. mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
  37. Android.R.anim.fade_out));  
  38. Gallery g = (Gallery) findViewById(R.id.gallery);  
  39. g.setAdapter(new ImageAdapter(this));  
  40. g.setOnItemSelectedListener(this);  
  41. }  
  42. /*  
  43. * override for ViewSwitcher.ViewFactory#makeView()  
  44. */  
  45. public View makeView() {  
  46. ImageView i = new ImageView(this);  
  47. i.setBackgroundColor(0xFF000000);  
  48. i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  49. i.setLayoutParams(new ImageSwitcher.LayoutParams
    (LayoutParams.FILL_PARENT,  
  50. LayoutParams.FILL_PARENT));  
  51. return i;  
  52. }  
  53. /*  
  54. * override for   
  55. * AdapterView.OnItemSelectedListener#onItemSelected()  
  56. */  
  57. public void onItemSelected(AdapterView parent, 
    View v, int position, long id) {  
  58. mSwitcher.setImageResource(mImageIds[position]);  
  59. }  
  60. /*  
  61. * override for AdapterView.OnItemSelectedListener
    #onNothingSelected()  
  62. */  
  63. public void onNothingSelected(AdapterView< ?> arg0) {  
  64. // TODO Auto-generated method stub  
  65. }  
  66. public class ImageAdapter extends BaseAdapter {  
  67. public ImageAdapter(Context c) {  
  68. mContext = c;  
  69. }  
  70. public int getCount() {  
  71. return mThumbIds.length;  
  72. }  
  73. public Object getItem(int position) {  
  74. return position;  
  75. }  
  76. public long getItemId(int position) {  
  77. return position;  
  78. }  
  79. public View getView(int position, View convertView, 
    ViewGroup parent) {  
  80. ImageView i = new ImageView(mContext);  
  81. i.setImageResource(mThumbIds[position]);  
  82. i.setAdjustViewBounds(true);  
  83. i.setLayoutParams(new Gallery.LayoutParams(  
  84. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  85. i.setBackgroundResource(R.drawable.picture_frame);  
  86. return i;  
  87. }  
  88. private Context mContext;  
  89. }  

从Android图片浏览的代码中看到还实现了AdapterView.OnItemSelectedListener,这样就需要重写onItemSelected()方法,然后在该方法中:mSwitcher.setImageResource(mImageIds[position]);这样就实现了图片在ImageSwitcher中的切换。

【编辑推荐】

  1. Android菜单构造技巧 
  2. Android adb中命令的运行 
  3. Android虚拟设备适用你的部署目标 
  4. Android屏幕元素相关概念详解 
  5. Android代码结构深入剖析 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...