现在的位置: 首页 > 综合 > 正文

Android作业:一个3D相册源码

2013年10月04日 ⁄ 综合 ⁄ 共 3950字 ⁄ 字号 评论关闭

 

我们专业课有Android的学习,最后老师让做一个简单的Android应用程序.我在网上找些资料,加上自己改造一下做了一个3D相册.

程序仿照Android的相册功能,调用Gallery类对相片进行浏览.实现3D效果.其中不足的地方就是里面的图片都是写死的.这一点可以改进.程序中使用Png格式图片,注意图片不要太大,500*500一下最好.

image

首先:

GalleryFlow.java 类 用来实现图片放映查看效果.

 

   1: package com.android.CustomGallery;

   2:  

   3: import android.content.Context;

   4: import android.graphics.Camera;

   5: import android.graphics.Matrix;

   6: import android.util.AttributeSet;

   7: import android.util.Log;

   8: import android.view.View;

   9: import android.view.animation.Transformation;

  10: import android.widget.Gallery;

  11: import android.widget.ImageView;

  12:  

  13: public class GalleryFlow extends Gallery {

  14:  

  15:         /**

  16:          * Graphics Camera used for transforming the matrix of ImageViews

  17:          */

  18:         private Camera mCamera = new Camera();

  19:  

  20:         /**

  21:          * The maximum angle the Child ImageView will be rotated by

  22:          */

  23:         private int mMaxRotationAngle = 60;

  24:  

  25:         /**

  26:          * The maximum zoom on the centre Child

  27:          */

  28:         private int mMaxZoom = -120;

  29:  

  30:         /**

  31:          * The Centre of the Coverflow

  32:          */

  33:         private int mCoveflowCenter;

  34:  

  35:         public GalleryFlow(Context context) {

  36:                 super(context);

  37:                 this.setStaticTransformationsEnabled(true);

  38:         }

  39:  

  40:         public GalleryFlow(Context context, AttributeSet attrs) {

  41:                 super(context, attrs);

  42:                 this.setStaticTransformationsEnabled(true);

  43:         }

  44:  

  45:         public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {

  46:                 super(context, attrs, defStyle);

  47:                 this.setStaticTransformationsEnabled(true);

  48:         }

  49:  

  50:         /**

  51:          * Get the max rotational angle of the image

  52:          * 

  53:          * @return the mMaxRotationAngle

  54:          */

  55:         public int getMaxRotationAngle() {

  56:                 return mMaxRotationAngle;

  57:         }

  58:  

  59:         /**

  60:          * Set the max rotational angle of each image

  61:          * 

  62:          * @param maxRotationAngle

  63:          *            the mMaxRotationAngle to set

  64:          */

  65:         public void setMaxRotationAngle(int maxRotationAngle) {

  66:                 mMaxRotationAngle = maxRotationAngle;

  67:         }

  68:  

  69:         /**

  70:          * Get the Max zoom of the centre image

  71:          * 

  72:          * @return the mMaxZoom

  73:          */

  74:         public int getMaxZoom() {

  75:                 return mMaxZoom;

  76:         }

  77:  

  78:         /**

  79:          * Set the max zoom of the centre image

  80:          * 

  81:          * @param maxZoom

  82:          *            the mMaxZoom to set

  83:          */

  84:         public void setMaxZoom(int maxZoom) {

  85:                 mMaxZoom = maxZoom;

  86:         }

  87:  

  88:         /**

  89:          * Get the Centre of the Coverflow

  90:          * 

  91:          * @return The centre of this Coverflow.

  92:          */

  93:         private int getCenterOfCoverflow() {

  94:                 //Log.e("CoverFlow Width+Height", getWidth() + "*" + getHeight());

  95:                 return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2

  96:                                 + getPaddingLeft();

  97:         }

  98:  

  99:         /**

 100:          * Get the Centre of the View

 101:          * 

 102:          * @return The centre of the given view.

 103:          */

 104:         private static int getCenterOfView(View view) {

 105:                 /*Log.e("ChildView Width+Height", view.getWidth() + "*"

 106:                                 + view.getHeight());*/

 107:                 return view.getLeft() + view.getWidth() / 2;

 108:         }

 109:  

 110:         /**

 111:          * {@inheritDoc}

 112:          * 

 113:          * @see #setStaticTransformationsEnabled(boolean)

 114:          */

 115:         protected boolean getChildStaticTransformation(View child, Transformation t) {

 116:  

 117:                 final int childCenter = getCenterOfView(child);

 118:                 final int childWidth = child.getWidth();

 119:                 int rotationAngle = 0;

 120:  

 121:                 t.clear();

 122:                 t.setTransformationType(Transformation.TYPE_MATRIX);

 123:  

 124:                 if (childCenter == mCoveflowCenter) {

 125:                         transformImageBitmap((ImageView) child, t, 0);

 126:                 } else {

 127:                         rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);

 128:                         if (Math.abs(rotationAngle) > mMaxRotationAngle) {

 129:                                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle

 130:                                                 : mMaxRotationAngle;

 131:                         }

 132:                         transformImageBitmap((ImageView) child, t, rotationAngle);

 133:                 }

 134:  

 135:                 return true;

 136:         }

 137:  

 138:         /**

 139:          * This is called during layout when the size of this view has changed. If

 140:          * you were just added to the view hierarchy, you're called with the old

 141:          * values of 0.

 142:          * 

 143:          * @param w

 144:          *            Current width of this view.

 145:          * @param h

 146:          *            Current height of this view.

 147:          * @param oldw

 148:          *            Old width of this view.

 149:          * @param oldh

 150:          *            Old height of this view.

 151:          */

 152:         protected void onSizeChanged(int w, int h, int oldw, int oldh) {

 153:                 mCoveflowCenter = getCenterOfCoverflow();

 154:                 super.onSizeChanged(w, h, oldw, oldh);

 155:         }

 156:  

 157:         /**

 158:          * Transform the Image Bitmap by the Angle passed

 159:          * 

 160:          * @param imageView

 161:          *            ImageView the ImageView whose bitmap we want to rotate

 162:          * @param t

 163:          *            transformation

 164:          * @param rotationAngle

 165:          *            the Angle by which to rotate the Bitmap

抱歉!评论已关闭.