您的位置:

Android Gallery详解

Android Gallery是一个可以展示图片或者视频等多媒体内容的组件。它也是一个支持手势操作的滑动控件,用户可以通过滑动屏幕来切换图片等多媒体内容。在本文中,我们将从以下几个方面来对Android Gallery进行详细的阐述:

一、Gallery的基本使用

public class MainActivity extends Activity {

    private Gallery mGallery;

    private int[] mImageIds = new int[] {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };

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

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(240, 180));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    }
}

以上代码展示了Gallery的基本使用方法,需要注意的是,我们需要为Gallery设置一个Adapter来为其提供数据,同时,我们也需要自定义Adapter来实现展示图片的效果。在这个例子中,我们创建了一个ImageAdapter类,重写了Adapter中的方法来实现图片展示,同时我们也为ImageView设置了LayoutParams和ScaleType。

二、Gallery的滑动特效

Android的Gallery控件具有多种滑动特效,包括平移、渐变、缩放等等。我们可以通过定义我们自己的Animation来实现Gallery的滑动特效。

public class MainActivity extends Activity {

    private Gallery mGallery;

    private int[] mImageIds = new int[] {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };

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

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setAnimationDuration(1000);
        mGallery.setAnimation(new TranslateAnimation(0, 200, 0, 0));
    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(240, 180));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    }
}

在以上代码中,我们定义了一个TranslateAnimation对象,用来指定Gallery的滑动方向和距离。同时我们也通过setAnimationDuration方法来设置Gallery的滑动时间。

三、Gallery的点击事件

我们也可以为Gallery设置点击事件,当用户点击图片时可以进行特定操作。例如,在这个例子中,当用户点击图片时,我们将展示该图片的名称。

public class MainActivity extends Activity {

    private Gallery mGallery;

    private int[] mImageIds = new int[] {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };

    private String[] mImageNames = new String[] {
            "图片1",
            "图片2",
            "图片3",
            "图片4",
            "图片5"
    };

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

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, mImageNames[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(240, 180));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    }
}

在以上代码中,我们重写了Gallery的OnItemClickListener方法,当用户点击某个图片时,我们通过Toast来展示该图片的名称。

四、Gallery的自定义布局

我们也可以为Gallery设置自定义布局,例如,在这个例子中,我们将为Gallery中每个图片添加一个TextView显示图片的名称。

public class MainActivity extends Activity {

    private Gallery mGallery;

    private int[] mImageIds = new int[] {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };

    private String[] mImageNames = new String[] {
            "图片1",
            "图片2",
            "图片3",
            "图片4",
            "图片5"
    };

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

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, mImageNames[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_gallery, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
            TextView textView = (TextView) view.findViewById(R.id.tv_name);
            imageView.setImageResource(mImageIds[position]);
            textView.setText(mImageNames[position]);
            return view;
        }
    }
}

以上代码中,我们通过调用LayoutInflater的方法来加载自定义布局,并且在布局中添加了一个TextView来展示图片名称。

五、Gallery的性能优化

对于一些超大的图片或者视屏数据,在Gallery中进行展示可能会影响到应用的性能。在这种情况下,我们可以通过异步加载来进行性能优化。

public class MainActivity extends Activity {

    private Gallery mGallery;

    private int[] mImageIds = new int[] {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };

    private String[] mImageNames = new String[] {
            "图片1",
            "图片2",
            "图片3",
            "图片4",
            "图片5"
    };

    private ImageLoader mImageLoader;

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

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, mImageNames[position], Toast.LENGTH_SHORT).show();
            }
        });

        mImageLoader = new ImageLoader();
    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setTag(mImageIds[position]);
            mImageLoader.loadImage(mImageIds[position], imageView);
            imageView.setLayoutParams(new Gallery.LayoutParams(240, 180));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    }

    private class ImageLoader {

        private SparseArray mBitmaps = new SparseArray<>();

        public void loadImage(int resId, ImageView target) {
            Bitmap bitmap = mBitmaps.get(resId);
            if (bitmap != null) {
                target.setImageBitmap(bitmap);
            } else {
                new LoadTask(resId, target).execute();
            }
        }

        private class LoadTask extends AsyncTask
    {

            private int resId;
            private ImageView target;

            public LoadTask(int resId, ImageView target) {
                this.resId = resId;
                this.target = target;
            }

            @Override
            protected Bitmap doInBackground(Void... params) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(getResources(), resId, options);
                int width = options.outWidth;
                int height = options.outHeight;
                options.inSampleSize = Math.max(width / 240, height / 180);
                options.inJustDecodeBounds = false;
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId, options);
                mBitmaps.put(resId, bitmap);
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                Object tag = target.getTag();
                if (tag != null && tag instanceof Integer && (int) tag == resId) {
                    target.setImageBitmap(bitmap);
                }
            }
        }
    }
}

   
  

以上代码中,我们通过自定义ImageLoader类和LoadTask类来异步加载Gallery中的图片,同时也添加了一个缓存机制来提高性能。