3D相册(Gallery 3D + 倒影效果) GalleryFlow
GalleryFlow.java
package com.image.galleryFlow;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryFlow extendsGallery {
privateCamera mCamera = newCamera();
privateint mMaxRotationAngle =60;// 最大旋转角度
privateint mMaxZoom = -430;// 最大缩放值
privateint mCoverflowCenter;
publicGalleryFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}
publicGalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
publicGalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
publicint getMaxRotationAngle() {
returnmMaxRotationAngle;
}
publicvoid setMaxRotationAngle(intmaxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
publicint getMaxZoom() {
returnmMaxZoom;
}
publicvoid setMaxZoom(intmaxZoom) {
mMaxZoom = maxZoom;
}
privateint getCenterOfCoverflow() {
return(getWidth() - getPaddingLeft() - getPaddingRight()) / 2
+ getPaddingLeft();
}
privatestatic int getCenterOfView(View view) {
returnview.getLeft() + view.getWidth() / 2;
}
@Override
protectedboolean getChildStaticTransformation(View child, Transformation t) {
// TODO Auto-generated method stub
finalint childCenter = getCenterOfView(child);
finalint childWidth = child.getWidth();
introtationAngle = 0;
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
if(childCenter == mCoverflowCenter) {
transformImageBitmap((ImageView) child, t,0);
}else {
rotationAngle = (int) (((float) (mCoverflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if(Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle <0) ? -mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap((ImageView) child, t, rotationAngle);
}
returntrue;
}
privatevoid transformImageBitmap(ImageView child, Transformation t,
introtationAngle) {
mCamera.save();
finalMatrix imageMatrix = t.getMatrix();
finalint imageHeight = child.getLayoutParams().height;
finalint imageWidth = child.getLayoutParams().width;
finalint rotation = Math.abs(rotationAngle);
mCamera.translate(0.0f,0.0f, 100.0f);
if(rotation < mMaxRotationAngle) {
floatzoomAmount = (float) (mMaxZoom + (rotation *1.5));
mCamera.translate(0.0f,0.0f, zoomAmount);
}
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth /2), -(imageHeight /2));
imageMatrix.postTranslate((imageWidth /2), (imageHeight /2));
mCamera.restore();
}
protectedvoid onSizeChanged(intw, int h, int oldw, int oldh) {
mCoverflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
}
ImageAdapter.java
|
layout_gallery.xml
|
Gallery循环播放图片
package com.example.testandroid;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/** * gallery 图片循环播放 * * @author maqf * */
@SuppressWarnings("deprecation")
public class CycleImageActivity extendsActivity {
privateint[] picture = { R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5,
R.drawable.gallery6, };
privateGallery pictureGallery = null;
privateint index = 0;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tiem);
this.pictureGallery = (Gallery) findViewById(R.id.gallery);
ImageAdapter adapter =new ImageAdapter(this);
this.pictureGallery.setAdapter(adapter);
Timer timer =new Timer();
timer.schedule(task,2000, 2000);
}
/** * 定时器 */
privateTimerTask task = newTimerTask() {
@Override
publicvoid run() {
Message message =new Message();
message.what =2;
index = pictureGallery.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
};
/** * Handler */
privateHandler handler = newHandler() {
@Override
publicvoid handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what) {
case2:
pictureGallery.setSelection(index);
break;
default:
break;
}
}
};
/** * 自定义一个图片显示适配器 * * @author TanRuixiang * */
classImageAdapter extendsBaseAdapter {
privateContext context;
publicImageAdapter(Context context) {
this.context = context;
}
publicint getCount() {
returnInteger.MAX_VALUE;
}
publicObject getItem(intposition) {
returnposition;
}
publiclong getItemId(intposition) {
returnposition;
}
publicView getView(intposition, View convertView, ViewGroup parent) {
ImageView imageView =new ImageView(context);
imageView.setImageResource(picture[position % picture.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(newGallery.LayoutParams(
Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
returnimageView;
}
}
}