Android技术学习,更多知识请访问https://www.itkc8.com   

实现功能:布局文件中有两个控件,分别是ImageSwitcher和Gallery控件,Gallery默认的情况下是不可见的。当进入程序的界面时,如果滑动屏幕,会切换到下一张或者前一张图片,根据你滑动屏幕的方向来判断,当单击屏幕时,会显示Gallery控件,再单击屏幕时,会隐藏Gallery控件,这个问题不是我自己解决的,通过论坛提问,AMinfo帮我解决的,感谢他。

        心得:我解决这个问题的方案是:要实现两个监听器,分别是:OnTouchListener和OnGestureListener,怎么尝试都无法区分单击屏幕和滑动屏幕动作。AMinfo的解决方案是在OnTouchListener的重写onTouch方法中添加如下的代码,如果我在一个人这样做下去,我都不知道什么时候完成这么一个小功能呢?让我明白需要大家帮助的时候,不可一意孤行,不但浪费时间,而且伤身啊,当然,自己也要先想想怎么去解决这个问题喽。

 

[java] view plaincopyprint?

  1. // 数字4和数字0分别表示的是不可见和可见,  
  2. // 也可以使用View.INVISIBLE和View.VISIBLE来代替数字.  
  3. else  
  4. {  
  5.     if (gallery.getVisibility() == 4)  
  6.         gallery.setVisibility(0);  
  7.     else  
  8.         gallery.setVisibility(4);  
  9. }  


        下面,贴上完整的小应用代码:

 

Album.java,项目中要用到的图片自己提供哦

 

[java] view plaincopyprint?

  1. package com.treasure.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.os.Bundle;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.ViewGroup;  
  11. import android.view.animation.AnimationUtils;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemSelectedListener;  
  14. import android.widget.Gallery.LayoutParams;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.Gallery;  
  17. import android.widget.ImageSwitcher;  
  18. import android.widget.ImageView;  
  19. import android.widget.ViewSwitcher.ViewFactory;  
  20.   
  21. /** 
  22.  * 实现相册浏览功能 
  23.  */  
  24. public class Album extends Activity implements OnItemSelectedListener, ViewFactory, OnTouchListener  
  25. {  
  26.     private ImageSwitcher imageSwitcher;  
  27.     private Gallery gallery;  
  28.       
  29.     private int selectedTag = 0;  
  30.     private int downX, upX;  
  31.       
  32.     private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,   
  33.             R.drawable.f, R.drawable.g};  
  34.     private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,   
  35.             R.drawable.f, R.drawable.g};  
  36.       
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState)   
  39.     {  
  40.         super.onCreate(savedInstanceState);  
  41.           
  42.         setContentView(R.layout.main);  
  43.           
  44.         imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);  
  45.         imageSwitcher.setFactory(this);  
  46.         //设置图片切换时的动画效果  
  47.         imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
  48.         imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));  
  49.         imageSwitcher.setOnTouchListener(this);  
  50.           
  51.         gallery = (Gallery)findViewById(R.id.gallery);  
  52.         //自定义ImageAdapter继承于BaseAdapter,是一个内部类  
  53.         gallery.setAdapter(new ImageAdapter(this));  
  54.         gallery.setOnItemSelectedListener(this);  
  55.     }  
  56.     @Override  
  57.     public View makeView()  
  58.     {  
  59.         ImageView image = new ImageView(this);  
  60.         image.setScaleType(ImageView.ScaleType.FIT_XY);  
  61.         image.setLayoutParams(new ImageSwitcher.LayoutParams(  
  62.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  63.         return image;  
  64.     }  
  65.   
  66.     @Override  
  67.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  68.             long arg3)  
  69.     {  
  70.         selectedTag = arg2;  
  71.         imageSwitcher.setImageResource(imagesId[arg2]);  
  72.     }  
  73.   
  74.     @Override  
  75.     public void onNothingSelected(AdapterView<?> arg0)  
  76.     {}  
  77.     public class ImageAdapter extends BaseAdapter  
  78.     {  
  79.         private Context context;  
  80.         int galleryItemBackground;  
  81.         public ImageAdapter (Context c)  
  82.         {  
  83.             context = c;  
  84.             TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery);  
  85.             galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);  
  86.             typeArray.recycle();  
  87.         }  
  88.         @Override  
  89.         public int getCount()   
  90.         {  
  91.             //返回selectId[]的长度  
  92.             return selectId.length;  
  93.         }  
  94.   
  95.         @Override  
  96.         public Object getItem(int position)  
  97.         {  
  98.             return position;  
  99.         }  
  100.   
  101.         @Override  
  102.         public long getItemId(int position)   
  103.         {  
  104.             return position;  
  105.         }  
  106.   
  107.         @Override  
  108.         public View getView(int position, View convertView, ViewGroup parent)   
  109.         {  
  110.             ImageView imageView = new ImageView(context);  
  111.             //设置资源图片  
  112.             imageView.setImageResource(selectId[position]);  
  113.             imageView.setAdjustViewBounds(true);  //允许调整边框  
  114.             //设定底部画廊,自适应大小  
  115.             imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  116.             //设置画廊背景  
  117.             imageView.setBackgroundResource(galleryItemBackground);  
  118.             return imageView;  
  119.         }  
  120.           
  121.     }  
  122.       
  123.     @Override  
  124.     public boolean onTouch(View v, MotionEvent event)  
  125.     {  
  126.           
  127.         if (event.getAction() == MotionEvent.ACTION_DOWN)  
  128.         {  
  129.             downX = (int)event.getX();  // 取得按下时的坐标x  
  130.             return true;  
  131.         }  
  132.         else if (event.getAction() == MotionEvent.ACTION_UP)  
  133.         {  
  134.             upX = (int)event.getX();    // 取得松开时的坐标x;  
  135.             if (upX - downX > 100)  
  136.             {  
  137.                 // 从左拖到右,即看前一张  
  138.                 // 如果是第一,则去到尾部  
  139.                 if (gallery.getSelectedItemPosition() == 0)  
  140.                     selectedTag = gallery.getCount() - 1;  
  141.                 else  
  142.                     selectedTag = gallery.getSelectedItemPosition() - 1;  
  143.                   
  144.                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  145.                         R.anim.push_right_in));  
  146.                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,   
  147.                         R.anim.push_right_out));  
  148.             }  
  149.             else if (downX - upX > 100)  
  150.             {  
  151.                 // 从右拖到左,即看后一张  
  152.                 // 如果是最后,则去到第一  
  153.                 if (gallery.getSelectedItemPosition()   
  154.                         == (gallery.getCount() - 1))  
  155.                     selectedTag = 0;  
  156.                 else  
  157.                     selectedTag = gallery.getSelectedItemPosition() + 1;  
  158.                 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
  159.                         R.anim.push_left_in));  
  160.                 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,   
  161.                         R.anim.push_left_out));  
  162.             }  
  163.               
  164.             else  
  165.             {  
  166.                 if (gallery.getVisibility() == 4)  
  167.                     gallery.setVisibility(0);  
  168.                 else  
  169.                     gallery.setVisibility(4);  
  170.             }  
  171.                   
  172.               
  173.             // 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener  
  174.             gallery.setSelection(selectedTag, true);  
  175.             return true;  
  176.         }  
  177.           
  178.         return false;  
  179.     }  
  180. }  


res\layout\main.xml

 

 

[html] view plaincopyprint?

  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.     android:orientation="vertical" >  
  6.   
  7.     <ImageSwitcher android:id="@+id/switcher"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentTop="true"/>  
  12.       
  13.     <Gallery android:id="@+id/gallery"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_height="150dp"  
  17.         android:spacing="10dp"  
  18.         android:layout_alignParentBottom="true"  
  19.         android:gravity="center_vertical"  
  20.         android:visibility="invisible"/>  
  21.       
  22. RelativeLayout>  


res\values\attrs.xml

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     declare-styleable>  
  6. resources>  


res\anim\push_left_in.xml

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.     <translate  
  5.         android:duration="1500"  
  6.         android:fromXDelta="100%p"  
  7.         android:toXDelta="0" />  
  8.   
  9.     <alpha  
  10.         android:duration="1500"  
  11.         android:fromAlpha="0.1"  
  12.         android:toAlpha="1.0" />  
  13. set>  


res\anim\push_left_out.xml

 

 

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.     <translate  
  5.         android:duration="1500"  
  6.         android:fromXDelta="0"  
  7.         android:toXDelta="-100%p" />  
  8.   
  9.     <alpha  
  10.         android:duration="1500"  
  11.         android:fromAlpha="1.0"  
  12.         android:toAlpha="0.1" />  
  13. set>  


res\anim\push_right_in.xml

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.     <translate  
  5.         android:duration="1500"  
  6.         android:fromXDelta="-100%p"  
  7.         android:toXDelta="0" />  
  8.   
  9.     <alpha  
  10.         android:duration="1500"  
  11.         android:fromAlpha="0.1"  
  12.         android:toAlpha="1.0" />  
  13. set>  


res\anim\push_right_out.xml

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.     <translate  
  5.         android:duration="1500"  
  6.         android:fromXDelta="0"  
  7.         android:toXDelta="100%p" />  
  8.   
  9.     <alpha  
  10.         android:duration="1500"  
  11.         android:fromAlpha="1.0"  
  12.         android:toAlpha="0.1" />  
  13. set>  

 

 

 

效果 图:

进入界面没有单击屏幕,默认是隐藏Gallery

进入界面,当单击屏幕,会显示Gallery

向右滑动时,显示下一张图片

向左滑动时,显示上一张图片

 

非常感谢http://blog.csdn.net/lyb2518/article/details/7889169

 

Android技术学习,更多知识请访问https://www.itkc8.com   

更多相关文章

  1. Android中进行图像压缩和缩放
  2. Android(安卓)nomedia 避免图片等资源泄露在系统图库当中
  3. Android内存优化总结&实践
  4. Android横竖屏解析
  5. Android中Gridview和ViewPager显示图片的优化处理(2)
  6. 个推:服务端遇到的一些问题。
  7. Android高手进阶教程(二十八)之---Android(安卓)ViewPager控件的
  8. 小猪浅谈Android屏幕适配
  9. Xamarin android 使用RecyclerView结合SwipeRefreshLayout下拉刷

随机推荐

  1. Android(安卓)- 开发实例(16):ListView新
  2. android利用Intent.ACTION_SEND实现简单
  3. system.img镜像转换为system.new.dat + s
  4. 2011.06.23——— android 事件处理机制
  5. Android实时绘制效果(一)
  6. android eclipse setup
  7. 使用命令行编译Qt Android apps
  8. android 的relativelayout 报错 No resou
  9. Android 用AsyncTask下载网络图片并显示
  10. Android SDK 自带项目GestureBuilderActi