//安卓开发之画廊布局(使用系统自带的ImageSwitcher与Gallery实现)
public class MainActivity extends Activity implements ViewFactory {private Gallery gallery = null;
private ImageSwitcher imageSwitcher = null;
int[] imageIDs = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
R.drawable.p8 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// 设置ImageSwitcher组件的工厂对象
imageSwitcher.setFactory(this);
// // 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
gallery = (Gallery) findViewById(R.id.gallery);
ImageViewAdapter adapter = new ImageViewAdapter(MainActivity.this,
imageIDs);
Log.i("MainActivity", "123.....0");
gallery.setAdapter(adapter);
Log.i("MainActivity", "123.....1");
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
imageSwitcher.setImageResource(imageIDs[position
% imageIDs.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("ljq", "parent=" + parent.getClass().getName()); // Gallery
Log.i("ljq", "view=" + view.getClass().getName()); // ImageView
Log.i("ljq", "position=" + position); // 1
Log.i("ljq", "id=" + id);// 1
Gallery gl = (Gallery) parent;
ImageView iv = (ImageView) view;
}
});
}
//
// @Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Log.i("MainActivity", "123.....2");
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Log.i("MainActivity", "123.....3");
return imageView;
}
}
//ImageViewAdapter类代码如下:
package com.example.agallerylayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ImageViewAdapter extends BaseAdapter {
private int[] imageIDs=null;
private Context context=null;
public ImageViewAdapter(Context context, int[] imageIDs) {
this.context=context;
this.imageIDs=imageIDs;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imageIDs[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
//优化二,通过取余来循环取得imageIDs数组中的图像资源ID,取余可以大大较少资源的浪费
iv.setImageResource(imageIDs[position%imageIDs.length]);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// Log.i("MainActivity", "123.....4");
// iv.setLayoutParams(new LinearLayout.LayoutParams(100,100));//把图片缩小原来的60%
// Log.i("MainActivity", "123.....5");
// iv.setBackgroundResource(_mGalleryItemBackground);
return iv;
}
}
//布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:unselectedAlpha: 设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。 -->
<Gallery android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="10dip"
android:background="@drawable/android_layout_bg"
android:unselectedAlpha="1.2"
android:id="@+id/gallery"
android:layout_marginTop="30dp"/>
<ImageSwitcher android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>