天天看点

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

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

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

先上个效果图~

http://androiddada.iteye.com/

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

首先这个是个左右循环的gallery(其实是integer.max_value

= 2147483647 这么多的个啦,接近无限了)。

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

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

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

下面是重要代码:

布局:

xml代码  

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

<span style="font-size: small;"><?xml version="1.0" encoding="utf-8"?>  

<linearlayout xmlns:android="http://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_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拖拽滑动过快:

java代码  

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

<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);  

    @override  

    public boolean onfling(motionevent e1, motionevent e2, float velocityx,  

            float velocityy) {  

            //返回false 解决gallery拖拽滑动过快  

        return false;  

    public void setunselectedalpha(float unselectedalpha) {  

        // todo auto-generated method stub  

        unselectedalpha = 1.0f;  

        super.setunselectedalpha(unselectedalpha);  

    </span>  

adapter中的 getview方法:

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

<span style="font-size: small;">@override  

    public view getview(int position, view convertview, viewgroup parent) {  

            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:

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

<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  

            public void onnothingselected(adapterview<?> arg0) {  

        });   

        //图片地址  

        _adv_imgurl.add("http://www.baidu.com/img/baidu_sylogo1.gif");  

        _adv_imgurl.add("http://www.iteye.com/images/logo.gif?1308833136");  

        _adv_imgurl.add("http://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);  

        }  

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

adv_gallery.rar (73.7 kb)

下载次数: 221

继续阅读