画廊视图Gallery

幻灯片图片浏览器

1.布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".AndroidGalleryActivity" >
 7 
 8     <ImageSwitcher
 9         android:id="@+id/imgswi"
10         android:layout_width="320dp"
11         android:layout_height="320dp" />
12 
13     <Gallery
14         android:id="@+id/gallery"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:layout_marginTop="25dp"
18         android:unselectedAlpha="0.6"
19         android:spacing="3pt"
20          />
21 
22 </LinearLayout>

2.逻辑控制

  1 package com.example.androidgallery;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.content.res.TypedArray;
  6 import android.support.v4.view.ViewPager.LayoutParams;
  7 import android.view.Menu;
  8 import android.view.View;
  9 import android.view.ViewGroup;
 10 import android.view.animation.AnimationUtils;
 11 import android.widget.AdapterView;
 12 import android.widget.AdapterView.OnItemSelectedListener;
 13 import android.widget.BaseAdapter;
 14 import android.widget.Gallery;
 15 import android.widget.ImageSwitcher;
 16 import android.widget.ImageView;
 17 import android.widget.ViewSwitcher.ViewFactory;
 18 
 19 public class AndroidGalleryActivity extends Activity {
 20 
 21     int[] imageIds=new int[]{
 22     R.drawable.mm,
 23     R.drawable .mm2,
 24     R.drawable.mm3,
 25     R.drawable.mm4
 26     } ;
 27     
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_android_gallery);
 32         final Gallery gallery=(Gallery)this.findViewById(R.id.gallery);
 33         final ImageSwitcher imgswi=(ImageSwitcher)this.findViewById(R.id.imgswi);
 34         //设置ViewFactory对象
 35         imgswi.setFactory(new ViewFactory() {
 36             
 37             @Override
 38             public View makeView() {
 39                 ImageView imageView=new ImageView(AndroidGalleryActivity.this);
 40                 imageView.setBackgroundColor(0xff0000);
 41                 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
 42                 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 43                 return imageView;
 44             }
 45         });
 46         //设置图片更换动画效果
 47         imgswi.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
 48         imgswi.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
 49         //创建一个BaseAdapter对象,负责提供Gallery显示每张图片
 50         BaseAdapter adapter=new BaseAdapter() {
 51             
 52             @Override
 53             public View getView(int position, View convertview, ViewGroup parent) {
 54                 //创建一个ImageView
 55                 ImageView imageView=new ImageView(AndroidGalleryActivity.this);
 56                 imageView.setImageResource(imageIds[position%imageIds.length]);
 57                 //设置ImageView缩放类型
 58                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
 59                 imageView.setLayoutParams(new Gallery.LayoutParams(75,100));
 60                 /*TypedArray typeArray=obtainStyledAttributes(R.styleable.Gallery);
 61                 imageView.setBackgroundResource(TypedArray)*/
 62                 return imageView;
 63             }
 64             
 65             @Override
 66             public long getItemId(int arg0) {
 67                 // TODO Auto-generated method stub
 68                 return arg0;
 69             }
 70             
 71             @Override
 72             public Object getItem(int arg0) {
 73                 // TODO Auto-generated method stub
 74                 return arg0;
 75             }
 76             
 77             @Override
 78             public int getCount() {
 79                 // TODO Auto-generated method stub
 80                 return imageIds.length;
 81             }
 82         };
 83         
 84         gallery.setAdapter(adapter);
 85         gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
 86 
 87             @Override
 88             public void onItemSelected(AdapterView<?> parent, View view,
 89                     int position, long id) {
 90                 // TODO Auto-generated method stub
 91                 imgswi.setImageResource(imageIds[position%imageIds.length]);
 92             }
 93 
 94             @Override
 95             public void onNothingSelected(AdapterView<?> parent) {
 96                 // TODO Auto-generated method stub
 97                 
 98             }
 99             
100         });
101     }
102 
103     @Override
104     public boolean onCreateOptionsMenu(Menu menu) {
105         // Inflate the menu; this adds items to the action bar if it is present.
106         getMenuInflater().inflate(R.menu.activity_android_gallery, menu);
107         return true;
108     }
109 
110 }

posted @ 2013-03-02 12:14  欢醉  阅读(1261)  评论(0编辑  收藏