Android Gallery

 

xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="0.89"
        android:background="@drawable/image"
        android:inAnimation="@android:anim/fade_in"
        android:outAnimation="@android:anim/fade_out" >

    </ImageSwitcher>

"
<Gallery 
    android:id="@+id/gallery"  
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_weight="0.5"
     >
   
    </Gallery>
    
</LinearLayout>

java 

 /**
 * @Title MyGalleryActivity.java
 * @package     com.example.standardview
 * @since  
 * @version     1.0.0 
 * @author      Vic Lee
 * @date        Aug 25, 2016-1:41:53 PM
 */
package com.example.standardview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
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.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MyGalleryActivity extends Activity implements OnItemClickListener, ViewFactory {
    private int mindex=0;
    ImageSwitcher imageSwitcher1;
    Gallery gallery1;
    private int[] mimage= new int[]{
            R.drawable.person1,
            
            R.drawable.person3,
            R.drawable.person4,
            R.drawable.person5,
            R.drawable.person6,
            R.drawable.person7,
            R.drawable.person8,
            R.drawable.person9
            };
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gallery);
        gallery1=(Gallery) findViewById(R.id.gallery);
        gallery1.setAdapter(new ImageAdapter() );
        imageSwitcher1=(ImageSwitcher) findViewById(R.id.imageswitcher);
        imageSwitcher1.setFactory(this);
        imageSwitcher1.setImageResource(mimage[mindex]);
        
        
        gallery1.setOnItemClickListener( this);
    }
    class ImageAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return mimage.length;
        }

        @Override
        public Object getItem(int position) 
        {
            return mimage[position];
        }

        @Override
        public long getItemId(int position) {


            return mimage[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView imageView=new  ImageView(MyGalleryActivity.this);
            imageView.setImageResource(mimage[position]);
            return imageView;
        }
    
    
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{
Toast.makeText(this, ""+arg2, 3000).show();
imageSwitcher1.setImageResource(mimage[arg2]);
    
}

/* (non-Javadoc)
 * @see android.widget.ViewSwitcher.ViewFactory#makeView()
 */
@Override
public View makeView() {
    
    // TODO Auto-generated method stub
    return new ImageView(this);
    
}
}

效果:

 

posted @ 2016-08-25 14:31  每天进步一点点!  阅读(2875)  评论(0编辑  收藏  举报