--------------------.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;
}
@Override
public View getView(int arg0,
View arg1,
ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(
context);
imageView
.setImageResource(imgResId[arg0
% imgResId.length]);
imageView
.setScaleType(ImageView.ScaleType.FIT_XY);
imageView
.setLayoutParams(new DetialGallery.LayoutParams(
175, 200));
return imageView;
}
}
}
-----------------------.java DetialGallery
package gongzibai.co.cc;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
public class DetialGallery extends
Gallery {
private Camera mCamera = new Camera();
private int mMaxRotationAngle = 45;
private int mMaxZoom = -120;
private int mCoveflowCenter;
public DetialGallery(
Context context,
AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public int getMaxRotationAngle() {
return mMaxRotationAngle;
}
public void setMaxRotationAngle(
int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
public int getMaxZoom() {
return mMaxZoom;
}
public void setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}
private int getCenterOfCoverflow() {
return (getWidth()
- getPaddingLeft() - getPaddingRight())
/ 2 + getPaddingLeft();
}
private static int getCenterOfView(
View view) {
return view.getLeft()
+ view.getWidth() / 2;
}
protected boolean getChildStaticTransformation(
View child, Transformation t) {
final int childCenter = getCenterOfView(child);
final int childWidth = child
.getWidth();
int rotationAngle = 0;
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
if (childCenter == mCoveflowCenter) {
transformImageBitmap(
(ImageView) child,
t, 0);
} else {
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap(
(ImageView) child,
t, rotationAngle);
}
return true;
}
protected void onSizeChanged(int w,
int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw,
oldh);
}
private void transformImageBitmap(
ImageView child,
Transformation t,
int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = t
.getMatrix();
final int imageHeight = child
.getLayoutParams().height;
final int imageWidth = child
.getLayoutParams().width;
final int rotation = Math
.abs(rotationAngle);
// 在Z軸上正向移動camera的視角,實際效果為放大圖片。
// 如果在Y軸上移動,則圖片上下移動;X軸上對應圖片左右移動。
mCamera.translate(0.0f, 0.0f,
100.0f);
// As the angle of the view gets less, zoom in
if (rotation < mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
mCamera.translate(0.0f,
0.0f, zoomAmount);
}
// 在Y軸上旋轉,對應圖片豎向向裡翻轉。
// 如果在X軸上旋轉,則對應圖片橫向向裡翻轉。
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(
-(imageWidth / 2),
-(imageHeight / 2));
imageMatrix.postTranslate(
(imageWidth / 2),
(imageHeight / 2));
mCamera.restore();
}
//
// private boolean isScrollingLeft(
// MotionEvent e1,
// MotionEvent e2) {
// return e2.getX() > e1.getX();
// }
@Override
public boolean onFling(
MotionEvent e1,
MotionEvent e2,
float velocityX,
float velocityY) {
// int keyCode;
// if (isScrollingLeft(e1, e2)) {
// keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
// } else {
// keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
// }
// onKeyDown(keyCode, null);
return false;
}
}
-------------------.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<gongzibai.co.cc.DetialGallery
android:id="@+id/widget1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:spacing="10dp"
android:unselectedAlpha="0.5" />
</LinearLayout>