Gallery 模仿Flash廣告欄~!附源碼
https://androiddada.iteye.com/blog/1316926
先上個效果圖~
https://androiddada.iteye.com/
思路是這樣的,功能方麵:
首先這個是個左右循環的Gallery(其實是Integer.MAX_VALUE = 2147483647 這麼多的個啦,接近無限了)。
這個網上有很多,不再贅述。代碼裏麵也有,可以直接下載~
然後就是Gallery的樣式,我這裏 設置成無陰影的,間距 android:spacing="0dip"。
最後就是下麵的指示條了,我使用FrameLayout布局,裏麵的指示點 radiobuttion.(因為隻要一個是點亮的,用於指示當前位置,所以在一個group中)
下麵是重要代碼:
布局:
- <span style="font-size: small;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="150dip" >
- <com.test.AdvGallery
- android:fadingEdge="none"
- android:id="@+id/home_advs_gallery"
- android:spacing="0dip"
- android:layout_width="fill_parent"
- android:layout_height="150dip" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="20dip"
- android:layout_gravity="bottom"
- android:background="#55999999"
- android:gravity="center"
- android:orientation="horizontal" >
- <RadioGroup
- android:gravity="center"
- android:id="@+id/home_advs_gallery_mark"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- </RadioGroup>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout></span>
自定義Gallery,為了解決Gallery拖拽滑動過快:
- <span style="font-size: small;">public class AdvGallery extends Gallery {
- public AdvGallery(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public AdvGallery(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- //返回false 解決Gallery拖拽滑動過快
- return false;
- }
- @Override
- public void setUnselectedAlpha(float unselectedAlpha) {
- // TODO Auto-generated method stub
- unselectedAlpha = 1.0f;
- super.setUnselectedAlpha(unselectedAlpha);
- }
- </span>
adapter中的 getview方法:
- <span style="font-size: small;">@Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ImageView imageView = new ImageView(context);
- String curr_URL = imgURL.get(position%imgURL.size());
- imageView.setTag(curr_URL);
- Drawable cachedImage = asyncImageLoader.loadDrawable(context,curr_URL,new ImageCallback1() {
- @Override
- public void imageLoaded(Drawable imageDrawable, String imageUrl) {
- ImageView imageViewByTag = (ImageView) gallery.findViewWithTag(imageUrl);
- if (imageViewByTag != null && imageDrawable != null ) {
- imageViewByTag.setImageDrawable(imageDrawable);
- notifyDataSetChanged();
- }
- }
- });
- if (cachedImage != null) {
- imageView.setImageDrawable(cachedImage);
- }else{
- imageView.setImageResource(R.drawable.ic_launcher);
- }
- // 設置邊界對齊
- imageView.setAdjustViewBounds(true);
- imageView.setLayoutParams(new Gallery.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- //設置比例類型
- // imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- return imageView;
- }</span>
main中的oncreate:
- <span style="font-size: small;"> @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- _radioGroup = (RadioGroup) findViewById(R.id.home_advs_gallery_mark);
- _adv_Gallery = (Gallery) findViewById(R.id.home_advs_gallery);
- _advGalleryAdapter = new AdvGalleryAdapter(ADV_GalleryActivity.this,_adv_imgURL,_adv_Gallery);
- _adv_Gallery.setAdapter(_advGalleryAdapter);
- _adv_Gallery.setSelection(Integer.MAX_VALUE >> 1);
- _adv_Gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- _radioGroup.check(arg2%_adv_imgURL.size()); //Gallery焦點圖片改變時 更改RadioGroup
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- //圖片地址
- _adv_imgURL.add("https://www.baidu.com/img/baidu_sylogo1.gif");
- _adv_imgURL.add("https://www.iteye.com/images/logo.gif?1308833136");
- _adv_imgURL.add("https://csdnimg.cn/www/images/csdnindex_logo.gif");
- for(int i=0;i<_adv_imgURL.size();i++){
- RadioButton rb = new RadioButton(ADV_GalleryActivity.this);
- rb.setId(i);
- rb.setButtonDrawable(R.drawable.adv_gallery_mark_selector);
- rb.setClickable(false);
- _radioGroup.addView(rb);
- }
- }</span>
https://androiddada.iteye.com/
由於代碼比較多,放上源碼,希望大家能用到~!
- ADV_Gallery.rar (73.7 KB)
- 下載次數: 221
最後更新:2017-04-02 17:51:24