Android Gallery 3D效果(有图有真相)

--------------------.Java

package gongzibai.co.cc;

import java.util.ArrayList;

import Android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
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.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class Gallery1Activity extends
  Activity {
 public static final int CHANGE_BANNER = 1;
 private int cur_index = 0;

/** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

final DetialGallery gallery = (DetialGallery) findViewById(R.id.widget1);
  ImageAdapter imageAdapter = new ImageAdapter(
    Gallery1Activity.this);

gallery.setAdapter(imageAdapter);

final Handler handler = new Handler() {
   public void handleMessage(
     Message msg) {

switch (msg.what) {

case CHANGE_BANNER:

gallery.onKeyDown(
       KeyEvent.KEYCODE_DPAD_RIGHT,
       null);

break;

default:

break;

}

};
  };
  // new Thread(new Runnable() {
  // int flag = 1;
  //
  // public void run() {
  //
  // while (true) {
  //
  // handler.sendEmptyMessage(CHANGE_BANNER);
  //
  // try {
  //
  // Thread.sleep(1000);
  //
  // } catch (InterruptedException e) {
  //
  // e.printStackTrace();
  //
  // }
  //
  // }
  //
  // }
  //
  // }
  //
  // ).start();

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
   public void onItemSelected(
     AdapterView<?> arg0,
     View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(
      Gallery1Activity.this,
      "这是第" + arg2
        + "张图片",
      2).show();
   }

@Override
   public void onNothingSelected(
     AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
  });

}

class ImageAdapter extends
   BaseAdapter {
  // private ArrayList<byte[]> dishImages = new ArrayList<byte[]>();
  // private ImageView[] mImages;

Context context;
  int[] imgResId = new int[] {
    R.drawable.icon,
    R.drawable.icon2,
    R.drawable.icon3,
    R.drawable.icon,
    R.drawable.icon2,
    R.drawable.icon3,

};

public ImageAdapter(
    Gallery1Activity gallery1Activity) {
   // TODO Auto-generated constructor stub
   super();
   this.context = gallery1Activity;
  }

//  public boolean createReflectedImages() {
//   final int reflectionGap = 4;
//   int index = 0;
//   System.out
//     .println("dishImages size "
//       + dishImages
//         .size());
//   for (int i = 0; i < dishImages
//     .size(); ++i) {
//    System.out
//      .println("dishImage --- "
//        + dishImages
//          .get(i));
//    Bitmap originalImage = BitmapFactory
//      .decodeByteArray(
//        dishImages
//          .get(i),
//        0,
//        dishImages
//          .get(i).length);
//    int width = originalImage
//      .getWidth();
//    int height = originalImage
//      .getHeight();
//    Matrix matrix = new Matrix();
//    matrix.preScale(1, -1);
//    Bitmap reflectionImage = Bitmap
//      .createBitmap(
//        originalImage,
//        0,
//        height / 2,
//        width,
//        height / 2,
//        matrix,
//        false);
//
//    Bitmap bitmapWithReflection = Bitmap
//      .createBitmap(
//        width,
//        (height + height / 2),
//        Config.ARGB_8888);
//
//    Canvas canvas = new Canvas(
//      bitmapWithReflection);
//    canvas.drawBitmap(
//      originalImage,
//      0, 0, null);
//    Paint deafaultPaint = new Paint();
//    canvas.drawRect(
//      0,
//      height,
//      width,
//      height
//        + reflectionGap,
//      deafaultPaint);
//    canvas.drawBitmap(
//      reflectionImage,
//      0,
//      height
//        + reflectionGap,
//      null);
//    Paint paint = new Paint();
//    LinearGradient shader = new LinearGradient(
//      0,
//      originalImage
//        .getHeight(),
//      0,
//      bitmapWithReflection
//        .getHeight()
//        + reflectionGap,
//      0x70ffffff,
//      0x00ffffff,
//      TileMode.CLAMP);
//    paint.setShader(shader);
//    paint.setXfermode(new PorterDuffXfermode(
//      Mode.DST_IN));
//    canvas.drawRect(
//      0,
//      height,
//      width,
//      bitmapWithReflection
//        .getHeight()
//        + reflectionGap,
//      paint);
//    ImageView imageView = new ImageView(
//      context);
//    imageView
//      .setImageBitmap(bitmapWithReflection);
//    // imageView.setLayoutParams(new GalleryFlow.LayoutParams(180,
//    // 240));
//    imageView
//      .setLayoutParams(new DetialGallery.LayoutParams(
//        170,
//        200));
//    imageView.setScaleType(ScaleType.FIT_XY);
//    mImages[index++] = imageView;
//   }
//   return true;
//  }

@Override
  public int getCount() {
   // TODO Auto-generated method stub
   // return Integer.MAX_VALUE;
   return Integer.MAX_VALUE;

}

@Override
  public Object getItem(
    int position) {
   // TODO Auto-generated method stub

return position;
   //
   // return position;
   // return position;

}

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

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

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