一个2D仿3D效果的Gallery
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 extends Gallery {
/** 摄像机 */
private Camera mCamera = new Camera();
/**最大转动角度*/
private int mMaxRotationAngle = 60;
/**最大缩放*/
private int mMaxZoom = -120;
/**半径值*/
private int mCoveflowCenter;
public GalleryFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}
public GalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
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();
}
}
在layout布局中引用自己写的Gallery
<com.tarena.GalleryFlow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/galTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:spacing="10dp"/>
Activity中的代码
import com.tarena.biz.AppBiz;
import com.tarena.day0703.adapter.AppAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Gallery;
public class Day07_04_GalleryActivity extends Activity {
private Gallery galApps;
private AppBiz biz;
private AppAdapter adapter;
private void setupView() {
galApps = (Gallery) findViewById(R.id.galTest);
adapter = new AppAdapter(this, biz.getAppInfos());
galApps.setAdapter(adapter);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
biz = new AppBiz();
setupView();
}
}
然后我写了一个业务类和实体类
业务类
import java.util.ArrayList;
import com.tarena.day0703.R;
import com.tarena.entity.AppInfo;
public class AppBiz {
public ArrayList<AppInfo> getAppInfos() {
ArrayList<AppInfo> apps = new ArrayList<AppInfo>();
for (int i = 1; i <= 30; i++) {
AppInfo app = new AppInfo(R.drawable.guozi1, "app" + i);
apps.add(app);
}
return apps;
}
}
实体类
public class AppInfo {
public AppInfo() {
}
public AppInfo(int iconRes, String title) {
super();
this.iconRes = iconRes;
this.title = title;
}
private int iconRes;
private String title;
public int getIconRes() {
return iconRes;
}
public void setIconRes(int iconRes) {
this.iconRes = iconRes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
最后我自己写了一个Adapter
public class AppInfo {
public AppInfo() {
}
public AppInfo(int iconRes, String title) {
super();
this.iconRes = iconRes;
this.title = title;
}
private int iconRes;
private String title;
public int getIconRes() {
return iconRes;
}
public void setIconRes(int iconRes) {
this.iconRes = iconRes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
以上代码就可以实现一个3D效果的相册了
不过要注意的是,你的图片范围大小一定要在指定的Gallery大小之类如果超出这Gallery的范围就会报一个图片超出(OutOfMemoryError)的错误,提示是:bitmap size exceed VM budget (图片大小超出了范围)
祝大家早日成为高手 android
补充:Java , Java SE