Gallery和IamgeSwitcher协作幻灯片效果
Gallery和IamgeSwitcher协作幻灯片效果
今天学习了Android的Gallery,根据自己的喜好做了一个NBA球星的幻灯片,也算是一边学习,一边自娱自乐吧。。。
package com.test.gallery;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
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 TestGallleryActivity extends Activity {
int [] imgIds = new int []{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,
R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,
R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.switcher);
switcher.setInAnimation(AnimationUtils.loadAnimation( this , android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation( this , android.R.anim.fade_out));
switcher.setFactory( new ViewFactory() {
public View makeView() {
ImageView img1 = new ImageView(TestGallleryActivity. this );
img1.setBackgroundColor( 0xff0000 );
img1.setScaleType(ImageView.ScaleType.FIT_CENTER);
img1.setLayoutParams( new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return img1;
}
});
BaseAdapter adapter = new BaseAdapter(){
public int getCount() {
// TODO Auto-generated method stub
return imgIds.length;
}
public Object getItem( int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId( int position) {
// TODO Auto-generated method stub
return position;
}
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView img2 = new ImageView(TestGallleryActivity. this );
img2.setImageResource(imgIds[position]);
img2.setScaleType(ImageView.ScaleType.FIT_XY);
img2.setLayoutParams( new Gallery.LayoutParams( 75 , 100 ));
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
img2.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0 ));
return img2;
}};
Gallery gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(adapter);
gallery.setOnItemClickListener( new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
switcher.setImageResource(imgIds[arg2]);
}});
}
}
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于Gallery和IamgeSwitcher协作幻灯片效果的详细内容...