Android Gallery運用

package bizhi.co.cc;

import java.io.InputStream;

import net.miidi.credit.MiidiCredit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class BizhiActivity extends
  Activity {
 protected static InputStream iStream;
 private ImageAdapter mImageAdapter;
 ImageView mImageView;
 private int[] image = {
   R.drawable.shuzi1,
   R.drawable.shuzi18,
   R.drawable.shuzi2,
   R.drawable.shuzi3,
   R.drawable.shuzi4,
   R.drawable.shuzi5,
   R.drawable.shuzi6,
   R.drawable.shuzi7,

   R.drawable.shuzi9,
   R.drawable.shuzi10,
   R.drawable.shuzi11,
   R.drawable.shuzi12,
   R.drawable.shuzi13,
   R.drawable.shuzi14,
   R.drawable.shuzi15,
   R.drawable.shuzi16,
   R.drawable.shuzi17,

   R.drawable.shuzi19,
   R.drawable.shuzi20,

 };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow()
    .setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
  MiidiCredit.init(BizhiActivity.this, "5677",
          "iuwruztprel0txtd", false);
  setContentView(R.layout.main);
  mImageAdapter = new ImageAdapter(
    BizhiActivity.this,
    image);

  Gallery mGallery = (Gallery) findViewById(R.id.gallery1);
  mImageView = (ImageView) findViewById(R.id.imageView1);

  // int mGalleryItemBackground;
  // TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
  // mGalleryItemBackground = a.getResourceId(
  // R.styleable.Gallery_android_galleryItemBackground, 0);
  // a.recycle();
  // mImageView.setBackgroundResource(mGalleryItemBackground);

  mGallery.setAdapter(mImageAdapter);
  mGallery.setOnItemClickListener(new Gallery.OnItemClickListener() {

   @Override
   public void onItemClick(
     AdapterView<?> arg0,
     View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    mImageView
      .setImageResource(mImageAdapter.image[arg2]);
    Resources resources = getBaseContext()
      .getResources();
    iStream = resources
      .openRawResource(mImageAdapter.image[arg2]);

   }
  });

  mImageView
    .setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(
       View v) {
      // TODO Auto-generated method stub
      new AlertDialog.Builder(
        BizhiActivity.this)
        .setTitle(
          "正確答案:")
        .setMessage(
          R.string.daan)
        .setPositiveButton(
          "確定",
          new DialogInterface.OnClickListener() {

           @Override
           public void onClick(
             DialogInterface dialog,
             int which) {
            // TODO Auto-generated method
            // stub

           }
          })

        .create()
        .show();

     }
    });

 }

 public class ImageAdapter extends
   BaseAdapter {

  private Context context;
  private int[] image;
  int mGalleryItemBackground;

  public ImageAdapter(Context c,
    int[] image2) {
   this.context = c;
   this.image = image2;

   TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
   mGalleryItemBackground = a
     .getResourceId(
       R.styleable.Gallery_android_galleryItemBackground,
       0);
   a.recycle();
  }

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return image.length;
  }

  @Override
  public Object getItem(int arg0) {
   // TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int arg0) {
   // TODO Auto-generated method stub
   return arg0;
  }

  @Override
  public View getView(int arg0,
    View arg1,
    ViewGroup arg2) {
   // TODO Auto-generated method stub
   /* 產生ImageView對象 */
   ImageView i = new ImageView(
     context);
   /* 設定圖片給imageView對象 */
   i.setImageResource(image[arg0]);
   /* 重新設定圖片的寬高 */
   i.setScaleType(ImageView.ScaleType.FIT_XY);
   /* 重新設定Layout的寬高 */
   i.setLayoutParams(new Gallery.LayoutParams(
     210, 190));    www.aiwalls.com

   /* 設定Gallery背景圖 */
   i.setBackgroundResource(mGalleryItemBackground);
   /* 傳回imageView對象 */
   return i;
  }

 }

 // public void setWallpaper(InputStream data) throws IOException {
 // // TODO Auto-generated method stub
 // super.setWallpaper(data);
 // }

 

作者:gongzibai

發佈留言