2:3:4:5:6:7:" />

2010.06.05日志:Android Gallery左右循环旋转方

系统 1799 0

如图:

2

先在attr.xml里写个Gallery的样式

      
        1:  
      
      
        <?
      
      
        xml 
      
      
        version
      
      =
      
        "1.0" 
      
      
        encoding
      
      =
      
        "utf-8"
      
      
        ?>
      
    
      
        2:  
      
      
        <
      
      
        resources
      
      
        >
      
    
      
        3:  
      
      
            <
      
      
        declare-styleable 
      
      
        name
      
      =
      
        "Gallery"
      
      
        >
      
    
      
        4:  
      
      
                <
      
      
        attr 
      
      
        name
      
      =
      
        "android:galleryItemBackground" 
      
      
        />
      
    
      
        5:  
      
      
            </
      
      
        declare-styleable
      
      
        >
      
    
      
        6:  
      
      
      
      
        <!-- 定义Layout外部resources 的xml文件,用来改变Layout的背景图 -->
      
    
      
        7:  
      
      
      
      
        </
      
      
        resources
      
      
        >
      
    
      
        8:  
      
      
      
    

然后在mainActivity里的OnCreate里:

      
        1:  
      
      setContentView(R.layout.
      
        main
      
      );
    
      
        2:  
      
    
      
        3:  
      
      
        imageAdapter 
      
      = 
      
        new 
      
      myImageAdapter(
      
        this
      
      );
    
      
        4:  
      
    
      
        5:  
      
      
        gallery 
      
      = (Gallery) findViewById(R.id.
      
        Gallery_preView
      
      );
    
      
        6:  
      
      
        imageView 
      
      = (ImageView) findViewById(R.id.
      
        ImageView_photo
      
      );
    
      
        7:  
      
    
      
        8:  
      
      
        gallery
      
      .setAdapter(
      
        imageAdapter
      
      );
    
      
        9:  
      
      
        gallery
      
      .setSelection(200);
      
        //设置Gallery的起始位置
      
    

下面是imageAdapter的类,它继承BaseAdapter

图片资源:

      
        1:  
      
      
        protected int
      
      [] 
      
        myImageIds 
      
      = { R.drawable.
      
        vista_1
      
      , R.drawable.
      
        vista_2
      
      ,
    
      
        2:  
      
                  R.drawable.
      
        vista_3
      
      , R.drawable.
      
        vista_4
      
      , R.drawable.
      
        vista_5
      
      ,
    
      
        3:  
      
                  R.drawable.
      
        vista_6
      
      , };
    

构造里:

      
         1:  
      
      
        public 
      
      myImageAdapter(Context context) {
    
      
         2:  
      
      
        mContext 
      
      = context;
    
      
         3:  
      
      
        /* 使用res/values/attr.xml中的<declare-styleable>定义的Gallery属性 */
      
    
      
         4:  
      
      
      
      TypedArray typed_array = context
    
      
         5:  
      
                      .obtainStyledAttributes(R.styleable.
      
        Gallery
      
      );
    
      
         6:  
      
      
        mGalleryItemBackground 
      
      = typed_array.getResourceId(
    
      
         7:  
      
                      R.styleable.
      
        Gallery_android_galleryItemBackground
      
      , 0);
    
      
         8:  
      
      
        /* 让对象的styleable 属性能够反复使用 */
      
    
      
         9:  
      
      
      
      typed_array.recycle();
    
      
        10:  
      
          }
    
      
        11:  
      
    

getCount()方法:

      
        1:  
      
      
        @Override
      
    
      
        2:  
      
      
      
      
        public int 
      
      getCount() {
    
      
        3:  
      
      
        // 
      
      
        TODO 
      
      
        Auto-generated method stub
      
    
      
        4:  
      
      
                // return myImageIds.length;
      
    
      
        5:  
      
      
      
      
        return 
      
      Integer.
      
        MAX_VALUE
      
      ;
    
      
        6:  
      
          }
    

getView()方法:

      
         1:  
      
      
        @
      
      
        Override
      
    
      
         2:  
      
      
      
      
        public 
      
      
        View getView(
      
      
        int 
      
      
        position, View convertView, ViewGroup parent) {
      
    
      
         3:  
      
      
                ImageView i = 
      
      
        new 
      
      
        ImageView(
      
      
        mContext
      
      
        );
      
    
      
         4:  
      
      
      
    
      
         5:  
      
      
      
      
        if 
      
      
        (position < 0) {
      
    
      
         6:  
      
      
                    position = position + 
      
      
        myImageIds
      
      
        .
      
      
        length
      
      
        ;
      
    
      
         7:  
      
      
                }
      
    
      
         8:  
      
      
                i.setImageResource(
      
      
        myImageIds
      
      
        [position % 
      
      
        myImageIds
      
      
        .
      
      
        length
      
      
        ]);
      
    
      
         9:  
      
      
                i.setScaleType(ImageView.ScaleType.
      
      
        FIT_XY
      
      
        );
      
    
      
        10:  
      
      
                i.setLayoutParams(
      
      
        new 
      
      
        Gallery.LayoutParams(128, 128));
      
    
      
        11:  
      
      
                i.setBackgroundResource(
      
      
        mGalleryItemBackground
      
      
        );
      
    
      
        12:  
      
      
      
      
        return 
      
      
        i;
      
    
      
        13:  
      
      
            }
      
    

2010.06.05日志:Android Gallery左右循环旋转方法


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论