如何才能在Android手机操作系统中实现图片浏览的功能呢?我们今天就可以通过一段代码示例来对此进行一个充分的了解,以方便我们将来的应用。

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. packagecom.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. publicclassImageSwitcherTestextendsActivityimplements
  16. AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{
  17. privateImageSwitchermSwitcher;
  18. privateInteger[] 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. privateInteger[] 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. /**Calledwhentheactivityisfirstcreated.*/
  28. @Override
  29. publicvoidonCreate(BundlesavedInstanceState){
  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(newImageAdapter(this));
  40. g.setOnItemSelectedListener(this);
  41. }
  42. /*
  43. *overrideforViewSwitcher.ViewFactory#makeView()
  44. */
  45. publicViewmakeView(){
  46. ImageView i = new ImageView(this);
  47. i.setBackgroundColor(0xFF000000);
  48. i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  49. i.setLayoutParams(newImageSwitcher.LayoutParams
    (LayoutParams.FILL_PARENT,
  50. LayoutParams.FILL_PARENT));
  51. returni;
  52. }
  53. /*
  54. *overridefor
  55. *AdapterView.OnItemSelectedListener#onItemSelected()
  56. */
  57. publicvoidonItemSelected(AdapterViewparent,
    Viewv,intposition,longid){
  58. mSwitcher.setImageResource(mImageIds[position]);
  59. }
  60. /*
  61. *overrideforAdapterView.OnItemSelectedListener
    #onNothingSelected()
  62. */
  63. publicvoidonNothingSelected(AdapterView < ?> arg0){
  64. //TODOAuto-generatedmethodstub
  65. }
  66. publicclassImageAdapterextendsBaseAdapter{
  67. publicImageAdapter(Contextc){
  68. mContext = c ;
  69. }
  70. publicintgetCount(){
  71. returnmThumbIds.length;
  72. }
  73. publicObjectgetItem(intposition){
  74. returnposition;
  75. }
  76. publiclonggetItemId(intposition){
  77. returnposition;
  78. }
  79. publicViewgetView(intposition,ViewconvertView,
    ViewGroupparent){
  80. ImageView i = new ImageView(mContext);
  81. i.setImageResource(mThumbIds[position]);
  82. i.setAdjustViewBounds(true);
  83. i.setLayoutParams(newGallery.LayoutParams(
  84. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  85. i.setBackgroundResource(R.drawable.picture_frame);
  86. returni;
  87. }
  88. privateContextmContext;
  89. }
  90. }

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

更多相关文章

  1. android 导入新工程或是编译没了android.jar 导致出错的解决方案
  2. Android:你要的WebView与 JS 交互方式 都在这里了
  3. 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等
  4. 深入探讨 Android(安卓)传感器
  5. Android(安卓)多线程-----AsyncTask详解
  6. Android(安卓)多线程-----AsyncTask详解
  7. Android视图绘制流程完全解析,带你一步步深入了解View(二)
  8. Android版本管理解决方法小议
  9. Android与Js通信之JsBridge再封装

随机推荐

  1. android对大图片的缓存处理
  2. Android Lamda 学习
  3. Android跳转系统相机或相册获取图片
  4. android 显示多选列表对话框
  5. android Settings项目安装
  6. android编程中setLayoutParams方法设置
  7. android html 读写文件
  8. Android学习——Android签名用keytool和j
  9. Android中Intent延时跳转的方法
  10. Could not get unknown property 'ANDROI