gallery的简单使用方法

 

Gallery意思是"画廊",就是一个水平可左右拖动的列表,里面可以放置多个图片,经常和ImageSwitcher一起使用

在使用ImageSwitcher时,需要传入一个ViewFactory对象,并且需要给gallery设置数据适配器;   代码如下

public class MainActivity extends Activity {
    int[] images = new int[] { R.drawable.shuangzi, R.drawable.shuangyu,
            R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
            R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
            R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
            R.drawable.mojie };
    private ImageSwitcher is;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test1);

        is = (ImageSwitcher) findViewById(R.id.is);

        Gallery gallery = (Gallery) findViewById(R.id.gallery);

        is.setFactory(new ViewFactory() {
            @Override
            public View makeView() {
                ImageView iv = new ImageView(MainActivity.this);
                iv.setScaleType(ScaleType.FIT_XY);
                iv.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                return iv;
            }
        });
        gallery.setAdapter(new BaseAdapter() {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView iv = new ImageView(MainActivity.this);
                iv.setImageResource(images[position % images.length]);

                iv.setScaleType(ScaleType.FIT_XY);
                TypedArray ta = obtainStyledAttributes(R.styleable.Gallery);
                iv.setLayoutParams(new Gallery.LayoutParams(75, 100));

                iv.setBackgroundResource(ta.getResourceId(
                        R.styleable.Gallery_android_galleryItemBackground, 0));
                
                
                return iv;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return images[position];
            }

            @Override
            public int getCount() {
                return images.length;
            }
        });
        
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                is.setImageResource(images[position%images.length]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

上面getView方法作用是:显示画廊的水平列表
这里TypedArray作用是引入自定义控件的自定义属性

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/is"
        android:layout_width="320dp"
        android:layout_height="320dp" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:spacing="3dp"
        android:unselectedAlpha="0.5" />

</LinearLayout>

自定义属性attrs.xml文件位于values目录下

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Gallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>

</resources>

关于自定义属性参考文章:http://www.cnblogs.com/carlosk/archive/2012/06/06/2538336.html

                                 http://blog.csdn.net/congqingbin/article/details/7869730

                                http://blog.csdn.net/tinafhx/article/details/5290878

 

 

转载于:https://www.cnblogs.com/android-zcq/p/3284269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值