• 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

  • 相关阅读:
    时间单位转化
    快速排序算法
    用virtualenv建立Python独立开发环境
    Shell正则表达式之grep、sed、awk实操笔记
    Objective-C 30分钟入门教程
    base64加密后字符串长度
    error: synthesized property 'name' must either be named the same as a compatible instance variable or must explicitly name an instance variable问题解决
    Ubuntu系统下通过Clang编译器编写Objective-C
    MongoDB 聚合Group(二)
    MongoDB聚合(单一用途的聚合方法)
  • 原文地址:https://www.cnblogs.com/android-zcq/p/3284269.html
Copyright © 2020-2023  润新知