随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(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 {
// 声明ImageSwitcher
private ImageSwitcher mSwitcher;
// 图片数组ID
private int[] m = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5,
R.drawable.image6, R.drawable.image7 };
@Override
protected 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() {
@Override
public 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() {
@Override
public 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;
}
@Override
public int getCount() {
return m.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public 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:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageSwitcher
android:id="@+id/swither"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ImageSwitcher>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#55000000"
android:gravity="center_vertical"
android:spacing="16dp" >
</Gallery>
</RelativeLayout>
附上图片效果: