Android之图片滑动与显示

先在Transitions中插入ImageSwitcher

package com.example.Galleryphotoshow;

import com.example.Galleryphotoshow.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;



public   class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory
{

    private int[] resIds = new int[]
    { 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, R.drawable.item10,
      R.drawable.item11, R.drawable.item12, R.drawable.item13,
      R.drawable.item14, R.drawable.item15 };

    private Gallery gallery;  
    private ImageSwitcher is;  
    public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.activity_main);  
       gallery = (Gallery) findViewById(R.id.gallery1);  
       is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);  
         
       gallery.setAdapter(new ImageAdapter(this));  
     //让选定的图片在中心显示  
       gallery.setSelection(resIds.length/2);
       //为Gallery绑定监听器;  
       gallery.setOnItemSelectedListener(new OnItemSelectedListener() {  
           public void onItemSelected(AdapterView<?> parent, View view,  
                   int position, long id) {  
               //当在Gallery中选定一张图片是 ImageSwitcher同步显示同一张  
               //position%images.length  为了让图片循环显示  
               is.setImageResource(resIds[position%resIds.length]);  
                 
           }  
           public void onNothingSelected(AdapterView<?> parent) {  
                 
           }  
       });  
         
         
       is.setFactory(new ImageFactory(this));  
     
   }  
     
   private class ImageAdapter extends BaseAdapter{  
       private Context context;  
       public ImageAdapter(Context context) {  
           this.context = context;  
       }  
       //可以return images.lenght(),在这里返回Integer.MAX_VALUE  
       //是为了使图片循环显示  
       public int getCount() {  
           return Integer.MAX_VALUE;  
       }  
       public Object getItem(int position) {  
           return null;  
       }  
       public long getItemId(int position) {  
           return 0;  
       }  
       public View getView(int position, View convertView, ViewGroup parent) {  
           ImageView iv = new ImageView(context);  
           iv.setImageResource(resIds[position%resIds.length]);  
           iv.setLayoutParams(new Gallery.LayoutParams(90,90));  
           iv.setAdjustViewBounds(true);  
           return iv;  
       }  
   }  
     
   private class ImageFactory implements ViewFactory{  
       private Context context;  
       public ImageFactory(Context context){  
           this.context = context;  
       }  
       public View makeView() {  
           ImageView iv = new ImageView(context);  
           iv.setLayoutParams(new ImageSwitcher.LayoutParams(200,200));  
           return iv;  
       }  
   }

@Override
public View makeView() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    
}  
}  
不努力,还要青春干什么?
原文地址:https://www.cnblogs.com/caidupingblogs/p/4952357.html