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