歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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~

Copyright © Linux教程網 All Rights Reserved