现在的位置: 首页 > 综合 > 正文

Gallery 模仿Flash广告栏~!附源码

2013年02月05日 ⁄ 综合 ⁄ 共 5899字 ⁄ 字号 评论关闭

http://androiddada.iteye.com/blog/1316926

先上个效果图~

http://androiddada.iteye.com/


思路是这样的,功能方面:

首先这个是个左右循环的Gallery(其实是Integer.MAX_VALUE
2147483647 这么多的个啦,接近无限了)。

这个网上有很多,不再赘述。代码里面也有,可以直接下载~

然后就是Gallery的样式,我这里 设置成无阴影的,间距 android:spacing="0dip"。

最后就是下面的指示条了,我使用FrameLayout布局,里面的指示点 radiobuttion.(因为只要一个是点亮的,用于指示当前位置,所以在一个group中)


下面是重要代码:


布局:



Xml代码  收藏代码
  1. <span style="font-size: small;"><?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.   <FrameLayout  
  7.             android:layout_width="fill_parent"  
  8.             android:layout_height="150dip" >  
  9.   
  10.             <com.test.AdvGallery  
  11.                  android:fadingEdge="none"   
  12.                 android:id="@+id/home_advs_gallery"   
  13.                 android:spacing="0dip"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="150dip" />  
  16.   
  17.             <LinearLayout  
  18.                 android:layout_width="fill_parent"  
  19.                 android:layout_height="20dip"  
  20.                 android:layout_gravity="bottom"  
  21.                 android:background="#55999999"  
  22.                 android:gravity="center"  
  23.                 android:orientation="horizontal" >  
  24.   
  25.                 <RadioGroup  
  26.                     android:gravity="center"  
  27.                     android:id="@+id/home_advs_gallery_mark"  
  28.                     android:orientation="horizontal"  
  29.                     android:layout_width="fill_parent"  
  30.                     android:layout_height="wrap_content" >  
  31.            
  32.                 </RadioGroup>  
  33.             </LinearLayout>  
  34.         </FrameLayout>  
  35.   
  36. </LinearLayout></span>  

 


自定义Gallery,为了解决Gallery拖拽滑动过快:



Java代码  收藏代码
  1. <span style="font-size: small;">public class AdvGallery extends Gallery {  
  2.     public AdvGallery(Context context) {  
  3.         super(context);  
  4.         // TODO Auto-generated constructor stub  
  5.     }  
  6.       
  7.   
  8.     public AdvGallery(Context context, AttributeSet attrs) {  
  9.         super(context, attrs);  
  10.         // TODO Auto-generated constructor stub  
  11.     }  
  12.   
  13.     @Override  
  14.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  15.             float velocityY) {  
  16.             //返回false 解决Gallery拖拽滑动过快  
  17.         return false;  
  18.     }  
  19.   
  20.     @Override  
  21.     public void setUnselectedAlpha(float unselectedAlpha) {  
  22.         // TODO Auto-generated method stub  
  23.         unselectedAlpha = 1.0f;  
  24.         super.setUnselectedAlpha(unselectedAlpha);  
  25.     }  
  26.       
  27.     </span>  



adapter中的 getview方法:



Java代码  收藏代码
  1. <span style="font-size: small;">@Override  
  2.     public View getView(int position, View convertView, ViewGroup parent) {  
  3.         // TODO Auto-generated method stub  
  4.             ImageView imageView = new ImageView(context);    
  5.             String curr_URL = imgURL.get(position%imgURL.size());  
  6.             imageView.setTag(curr_URL);  
  7.              Drawable cachedImage = asyncImageLoader.loadDrawable(context,curr_URL,new ImageCallback1() {  
  8.                     @Override  
  9.                     public void imageLoaded(Drawable imageDrawable, String imageUrl) {  
  10.                         ImageView imageViewByTag = (ImageView) gallery.findViewWithTag(imageUrl);  
  11.                         if (imageViewByTag != null && imageDrawable != null ) {   
  12.                             imageViewByTag.setImageDrawable(imageDrawable);  
  13.                             notifyDataSetChanged();  
  14.                         }  
  15.                     }  
  16.                 });  
  17.              if (cachedImage != null) {  
  18.                   imageView.setImageDrawable(cachedImage);  
  19.             }else{  
  20.                 imageView.setImageResource(R.drawable.ic_launcher);  
  21.             }  
  22.             // 设置边界对齐  
  23.              imageView.setAdjustViewBounds(true);  
  24.              imageView.setLayoutParams(new Gallery.LayoutParams(  
  25.                     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  26.             //设置比例类型    
  27. //           imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  28.         return imageView;  
  29.     }</span>  



main中的oncreate:



Java代码  收藏代码
  1. <span style="font-size: small;">  @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.main);  
  5.           
  6.         _radioGroup = (RadioGroup) findViewById(R.id.home_advs_gallery_mark);  
  7.         _adv_Gallery = (Gallery) findViewById(R.id.home_advs_gallery);  
  8.         _advGalleryAdapter = new AdvGalleryAdapter(ADV_GalleryActivity.this,_adv_imgURL,_adv_Gallery);  
  9.           
  10.         _adv_Gallery.setAdapter(_advGalleryAdapter);  
  11.         _adv_Gallery.setSelection(Integer.MAX_VALUE >> 1);  
  12.         _adv_Gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  13.             @Override  
  14.             public void onItemSelected(AdapterView<?> arg0, View arg1,  
  15.                     int arg2, long arg3) {  
  16.                 // TODO Auto-generated method stub  
  17.                 _radioGroup.check(arg2%_adv_imgURL.size()); //Gallery焦点图片改变时 更改RadioGroup  
  18.             }  
  19.   
  20.             @Override  
  21.             public void onNothingSelected(AdapterView<?> arg0) {  
  22.                 // TODO Auto-generated method stub  
  23.             }  
  24.   
  25.         });   
  26.         //图片地址  
  27.         _adv_imgURL.add("http://www.baidu.com/img/baidu_sylogo1.gif");  
  28.         _adv_imgURL.add("http://www.iteye.com/images/logo.gif?1308833136");  
  29.         _adv_imgURL.add("http://csdnimg.cn/www/images/csdnindex_logo.gif");  
  30.           
  31.         for(int i=0;i<_adv_imgURL.size();i++){  
  32.             RadioButton rb = new RadioButton(ADV_GalleryActivity.this);  
  33.             rb.setId(i);  
  34.             rb.setButtonDrawable(R.drawable.adv_gallery_mark_selector);  
  35.             rb.setClickable(false);  
  36.             _radioGroup.addView(rb);  
  37.         }  
  38.           
  39.     }</span>  


http://androiddada.iteye.com/

由于代码比较多,放上源码,希望大家能用到~!

 

抱歉!评论已关闭.