Gallery效果
MainActivity
1 package gallerydemo.example.administrator.gallerydemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.animation.Animation; 7 import android.view.animation.AnimationUtils; 8 import android.widget.AdapterView; 9 import android.widget.Gallery; 10 import android.widget.ImageSwitcher; 11 import android.widget.ImageView; 12 import android.widget.ViewSwitcher; 13 14 public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{ 15 int []res={R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,R.drawable.item9}; 16 private Gallery gallery; 17 private ImageAdapter imageAdapter; 18 private ImageSwitcher imageSwitcher; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 gallery= (Gallery) findViewById(R.id.gallery); 24 imageAdapter=new ImageAdapter(res,this); 25 gallery.setAdapter(imageAdapter); 26 gallery.setOnItemSelectedListener(this); 27 imageSwitcher= (ImageSwitcher) findViewById(R.id.image); 28 imageSwitcher.setFactory(this); 29 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));/*Android自定义淡入动画*/ 30 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out)); 31 } 32 33 @Override 34 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 35 36 imageSwitcher.setBackgroundResource(res[position%res.length]); 37 } 38 39 @Override 40 public void onNothingSelected(AdapterView<?> parent) { 41 42 } 43 44 @Override 45 public View makeView() { 46 ImageView imageView=new ImageView(this); 47 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);/*图片按等比例拉伸*/ 48 return imageView; 49 } 50 }
ImageAdapter
1 package gallerydemo.example.administrator.gallerydemo; 2 3 import android.content.Context; 4 import android.util.Log; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.Gallery; 9 import android.widget.ImageView; 10 11 /** 12 * Created by Administrator on 2016/5/9. 13 */ 14 public class ImageAdapter extends BaseAdapter { 15 private int[] res; 16 private Context context; 17 public ImageAdapter(int[] res,Context context){ 18 this.context=context; 19 this.res=res; 20 } 21 @Override 22 public int getCount() { 23 return Integer.MAX_VALUE; 24 } 25 26 @Override 27 public Object getItem(int position) { 28 return res[position]; 29 } 30 31 @Override 32 public long getItemId(int position) { 33 return position; 34 } 35 36 @Override 37 public View getView(int position, View convertView, ViewGroup parent) { 38 ImageView imageView=new ImageView(context); 39 Log.i("Main","position"+position+","+"position%res.length"+position%res.length); 40 imageView.setBackgroundResource(res[position%res.length]);/*设置图片轮流播放*/ 41 imageView.setLayoutParams(new Gallery.LayoutParams(400,300));/*设置imageView的缩放,长400,宽300*/ 42 43 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 44 return imageView; 45 } 46 }
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <Gallery 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:id="@+id/gallery"></Gallery> 9 <ImageSwitcher 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:id="@+id/image"></ImageSwitcher> 13 </LinearLayout>