閱讀758 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Gallery 模仿Flash廣告欄~!附源碼

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


先上個效果圖~

https://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="https://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("https://www.baidu.com/img/baidu_sylogo1.gif");  
  28.         _adv_imgURL.add("https://www.iteye.com/images/logo.gif?1308833136");  
  29.         _adv_imgURL.add("https://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>  


https://androiddada.iteye.com/

由於代碼比較多,放上源碼,希望大家能用到~!

 


最後更新:2017-04-02 17:51:24

  上一篇:go ViewPager實現左右兩個屏幕的切換
  下一篇:go 關閉IBM HTTP Server插件自動生成服務,以避免部署大量應用時導致Dmgr內存溢出