android中画廊视图Gallery和ImageSwitcher组件的使用

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

Gallery能够水平方向显示其内容,一般用来浏览图片,被选中的选项位于中间,并且可以响应事件显示信息,下面结合ImageSwitcher组件来实现一个通过缩略图来浏览照片的程序,代码如下:

Activity:

package com.lovo;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;public class TestGalleryActivity extends Activity {// 声明ImageSwitcherprivate ImageSwitcher mSwitcher;// 图片数组IDprivate int[] m = { R.drawable.image1, R.drawable.image2,R.drawable.image3, R.drawable.image4, R.drawable.image5,R.drawable.image6, R.drawable.image7 };@Overrideprotected void onCreate(Bundle savedInstanceState) {// 设置窗口特征无标题// requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获得ImageSwitcher对象mSwitcher = (ImageSwitcher) findViewById(R.id.swither);// 为ImageSwitcher设置工厂mSwitcher.setFactory(new ViewFactory() {@Overridepublic View makeView() {// 创建imageView对象ImageView imageView = new ImageView(TestGalleryActivity.this);// 设置背景颜色imageView.setBackgroundColor(0xFF000000);// 设置精度类型imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);// 设置布局参数imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return imageView;}});// 设置出现和离开的动画效果mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));// 获得Gallery对象Gallery gallery = (Gallery) findViewById(R.id.gallery);gallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {// 设置图片资源mSwitcher.setImageResource(m[arg2]);}});// 为Gallery添加适配器gallery.setAdapter(new ImageAdapter(this));}class ImageAdapter extends BaseAdapter {private Context mContext;public ImageAdapter(Context context) {mContext = context;}@Overridepublic int getCount() {return m.length;}@Overridepublic Object getItem(int arg0) {return arg0;}@Overridepublic long getItemId(int arg0) {return arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// 实例化ImageView对象ImageView imageView = new ImageView(mContext);// 设置图片资源imageView.setImageResource(m[arg0]);// 设置边界对齐imageView.setAdjustViewBounds(true);// 设置布局参数imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// 设置背景资源imageView.setBackgroundResource(R.drawable.about_background_land);return imageView;}}
}


布局XML:



 附上图片效果:


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部