Android中的GalleryView实例演示

先看Android中的GalleryView实例演示效果:

滑动小图点击后上面的大图就和小图一致了

Android中的GalleryView实例演示

Android中的GalleryView实例演示

Android中的GalleryView实例演示

activity代码:

package com.tmacsky;
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 GalleryActivity extends Activity implements ViewFactory {
 int index = 0;
 ImageSwitcher imageSwitcher;
 int [] image = {R.drawable.ha,R.drawable.hb,R.drawable.hc,R.drawable.hd,R.drawable.he,R.drawable.hf,R.drawable.hg,R.drawable.hh};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gallery);
  //设置imageswitch
  imageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);
  //设置一个容器
  imageSwitcher.setFactory(this);
  //给一个初始的值
  imageSwitcher.setImageResource(image[index]);
  //定义gallery
  Gallery gallery = (Gallery)findViewById(R.id.gallery);
  //设置adapter
  gallery.setAdapter(new ImageAdapter());
  gallery.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    // TODO Auto-generated method stub
    //设置滑动后弹出切换图片的数字
    Toast.makeText(GalleryActivity.this, " " + arg2, 3000).show();
    imageSwitcher.setImageResource(image[arg2]);
   }
  });
 }
 class ImageAdapter extends BaseAdapter{
  public int getCount() {
   // TODO Auto-generated method stub
   return image.length;
  }
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return image[position];
  }
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return image[position];
  }
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   ImageView imageView = new ImageView(GalleryActivity.this);
   imageView.setPadding(10, 50, 10, 5);
   imageView.setImageResource(image[position]);
   return imageView;
  }
 }
 //设置工厂的时候,系统需要返回一个新的imageview,此处直接是默认的就可以了
 public View makeView() {
  // TODO Auto-generated method stub
  return new ImageView(this);
 }
}

gallery.xml的文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<ImageSwitcher
    android:id="@+id/imageswitcher"
    android:background="@drawable/hc"
    android:inAnimation="@android:anim/fade_in"
    android:outAnimation="@android:anim/fade_out"
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ></ImageSwitcher>
<Gallery
    android:layout_width="200px"
    android:layout_height="wrap_content"
    android:id="@+id/gallery"
    android:layout_gravity="center_horizontal"
    >
</Gallery>
</LinearLayout>

那啥图片资源什么的自己可以找自己喜欢的图,OK~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/838af3d8a88a21962b3e351df5f1154e.html