Android中3D gallary的实现

来源:互联网 发布:php 仿京东商城源代码 编辑:程序博客网 时间:2024/05/17 23:07

Gallery3DActivity:

[java] view plaincopy
  1. package com.test3dgallary;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.AdapterView.OnItemClickListener;  
  8. import android.widget.ImageView;  
  9.   
  10. public class Gallery3DActivity extends Activity {  
  11.     private ImageView imageView;  
  12.   
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         imageView = (ImageView) findViewById(R.id.main_iv_show);  
  17.         Integer[] images = { R.drawable.test1, R.drawable.test2,  
  18.                 R.drawable.test3, R.drawable.test4, R.drawable.test1,  
  19.                 R.drawable.test2, R.drawable.test3, R.drawable.test4 };  
  20.         ImageAdapter adapter = new ImageAdapter(this, images);  
  21.         adapter.createReflectedImages();// 创建倒影效果  
  22.         GallaryFor3D gallery = (GallaryFor3D) findViewById(R.id.main_gallery);  
  23.         gallery.setFadingEdgeLength(0);  
  24.         gallery.setSpacing(-10); // 图片之间的间距  
  25.         gallery.setAdapter(adapter);  
  26.         gallery.setSelection(4);  
  27.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  28.             public void onItemClick(AdapterView<?> parent, View view,  
  29.                     int position, long id) {  
  30.                 imageView.setImageDrawable(((ImageView) view).getDrawable());  
  31.             }  
  32.         });  
  33.     }  
  34. }  

GallaryFor3D:

[java] view plaincopy
  1. package com.test3dgallary;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Camera;  
  5. import android.graphics.Matrix;  
  6. import android.util.AttributeSet;  
  7. import android.view.View;  
  8. import android.view.animation.Transformation;  
  9. import android.widget.Gallery;  
  10. import android.widget.ImageView;  
  11.   
  12. public class GallaryFor3D extends Gallery {  
  13.     private Camera mCamera = new Camera();// 相机类  
  14.     private int mMaxRotationAngle = 60;// 最大转动角度  
  15.     private int mMaxZoom = -300;// 最大缩放值  
  16.     private int mCoveflowCenter;// 半径值  
  17.   
  18.     public GallaryFor3D(Context context) {  
  19.         super(context);  
  20.         // 支持转换 ,执行getChildStaticTransformation方法  
  21.         this.setStaticTransformationsEnabled(true);  
  22.     }  
  23.   
  24.     public GallaryFor3D(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.         this.setStaticTransformationsEnabled(true);  
  27.     }  
  28.   
  29.     public GallaryFor3D(Context context, AttributeSet attrs, int defStyle) {  
  30.         super(context, attrs, defStyle);  
  31.         this.setStaticTransformationsEnabled(true);  
  32.     }  
  33.   
  34.     public int getMaxRotationAngle() {  
  35.         return mMaxRotationAngle;  
  36.     }  
  37.   
  38.     public void setMaxRotationAngle(int maxRotationAngle) {  
  39.         mMaxRotationAngle = maxRotationAngle;  
  40.     }  
  41.   
  42.     public int getMaxZoom() {  
  43.         return mMaxZoom;  
  44.     }  
  45.   
  46.     public void setMaxZoom(int maxZoom) {  
  47.         mMaxZoom = maxZoom;  
  48.     }  
  49.   
  50.     private int getCenterOfCoverflow() {  
  51.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  52.                 + getPaddingLeft();  
  53.     }  
  54.   
  55.     private static int getCenterOfView(View view) {  
  56.         return view.getLeft() + view.getWidth() / 2;  
  57.     }  
  58.   
  59.     // 控制gallery中每个图片的旋转(重写的gallery中方法)  
  60.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
  61.         // 取得当前子view的半径值  
  62.         final int childCenter = getCenterOfView(child);  
  63.         final int childWidth = child.getWidth();  
  64.         // 旋转角度  
  65.         int rotationAngle = 0;  
  66.         // 重置转换状态  
  67.         t.clear();  
  68.         // 设置转换类型  
  69.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  70.         // 如果图片位于中心位置不需要进行旋转  
  71.         if (childCenter == mCoveflowCenter) {  
  72.             transformImageBitmap((ImageView) child, t, 0);  
  73.         } else {  
  74.             // 根据图片在gallery中的位置来计算图片的旋转角度  
  75.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  76.             // 如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)  
  77.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  78.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle  
  79.                         : mMaxRotationAngle;  
  80.             }  
  81.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  82.         }  
  83.         return true;  
  84.     }  
  85.   
  86.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  87.         mCoveflowCenter = getCenterOfCoverflow();  
  88.         super.onSizeChanged(w, h, oldw, oldh);  
  89.     }  
  90.   
  91.     private void transformImageBitmap(ImageView child, Transformation t,  
  92.             int rotationAngle) {  
  93.         // 对效果进行保存  
  94.         mCamera.save();  
  95.         final Matrix imageMatrix = t.getMatrix();  
  96.         // 图片高度  
  97.         final int imageHeight = child.getLayoutParams().height;  
  98.         // 图片宽度  
  99.         final int imageWidth = child.getLayoutParams().width;  
  100.   
  101.         // 返回旋转角度的绝对值  
  102.         final int rotation = Math.abs(rotationAngle);  
  103.   
  104.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
  105.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
  106.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  107.         // As the angle of the view gets less, zoom in  
  108.         if (rotation < mMaxRotationAngle) {  
  109.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  110.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  111.         }  
  112.         // 在Y轴上旋转,对应图片竖向向里翻转。  
  113.         // 如果在X轴上旋转,则对应图片横向向里翻转。  
  114.         mCamera.rotateY(rotationAngle);  
  115.         mCamera.getMatrix(imageMatrix);  
  116.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  117.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  118.         mCamera.restore();  
  119.     }  
  120. }  

ImageAdapter:

[java] view plaincopy
  1. package com.test3dgallary;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.Bitmap.Config;  
  7. import android.graphics.BitmapFactory;  
  8. import android.graphics.Canvas;  
  9. import android.graphics.LinearGradient;  
  10. import android.graphics.Matrix;  
  11. import android.graphics.Paint;  
  12. import android.graphics.PorterDuffXfermode;  
  13. import android.graphics.Shader.TileMode;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.ImageView;  
  18.   
  19. public class ImageAdapter extends BaseAdapter {  
  20.     int mGalleryItemBackground;  
  21.     private Context mContext;  
  22.     private Integer[] mImageIds;  
  23.     private ImageView[] mImages;  
  24.   
  25.     public ImageAdapter(Context context, Integer[] ImageIds) {  
  26.         mContext = context;  
  27.         mImageIds = ImageIds;  
  28.         mImages = new ImageView[mImageIds.length];  
  29.     }  
  30.   
  31.     /** 
  32.      * 创建倒影效果 
  33.      *  
  34.      * @return 
  35.      */  
  36.     public boolean createReflectedImages() {  
  37.         // 倒影图和原图之间的距离  
  38.         final int reflectionGap = 4;  
  39.         int index = 0;  
  40.         for (int imageId : mImageIds) {  
  41.             // 返回原图解码之后的bitmap对象  
  42.             Bitmap originalImage = BitmapFactory.decodeResource(  
  43.                     mContext.getResources(), imageId);  
  44.             int width = originalImage.getWidth();  
  45.             int height = originalImage.getHeight();  
  46.             // 创建矩阵对象  
  47.             Matrix matrix = new Matrix();  
  48.   
  49.             // 指定一个角度以0,0为坐标进行旋转  
  50.             // matrix.setRotate(30);  
  51.   
  52.             // 指定矩阵(x轴不变,y轴相反)  
  53.             matrix.preScale(1, -1);  
  54.   
  55.             // 将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图  
  56.             Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,  
  57.                     height / 2, width, height / 2, matrix, false);  
  58.   
  59.             // 创建一个宽度不变,高度为原图+倒影图高度的位图  
  60.             Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  61.                     (height + height / 2), Config.ARGB_8888);  
  62.   
  63.             // 将上面创建的位图初始化到画布  
  64.             Canvas canvas = new Canvas(bitmapWithReflection);  
  65.             canvas.drawBitmap(originalImage, 00null);  
  66.   
  67.             Paint deafaultPaint = new Paint();  
  68.             deafaultPaint.setAntiAlias(false);  
  69.             // canvas.drawRect(0, height, width, height +  
  70.             // reflectionGap,deafaultPaint);  
  71.             canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  72.             Paint paint = new Paint();  
  73.             paint.setAntiAlias(false);  
  74.   
  75.             /** 
  76.              * 参数一:为渐变起初点坐标x位置, 参数二:为y轴位置, 参数三和四:分辨对应渐变终点, 最后参数为平铺方式, 
  77.              * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变 
  78.              */  
  79.             LinearGradient shader = new LinearGradient(0,  
  80.                     originalImage.getHeight(), 0,  
  81.                     bitmapWithReflection.getHeight() + reflectionGap,  
  82.                     0x70ffffff0x00ffffff, TileMode.MIRROR);  
  83.             // 设置阴影  
  84.             paint.setShader(shader);  
  85.             paint.setXfermode(new PorterDuffXfermode(  
  86.                     android.graphics.PorterDuff.Mode.DST_IN));  
  87.             // 用已经定义好的画笔构建一个矩形阴影渐变效果  
  88.             canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  89.                     + reflectionGap, paint);  
  90.   
  91.             // 创建一个ImageView用来显示已经画好的bitmapWithReflection  
  92.             ImageView imageView = new ImageView(mContext);  
  93.             imageView.setImageBitmap(bitmapWithReflection);  
  94.             // 设置imageView大小 ,也就是最终显示的图片大小  
  95.             imageView.setLayoutParams(new GallaryFor3D.LayoutParams(300400));  
  96.             // imageView.setScaleType(ScaleType.MATRIX);  
  97.             mImages[index++] = imageView;  
  98.         }  
  99.         return true;  
  100.     }  
  101.   
  102.     @SuppressWarnings("unused")  
  103.     private Resources getResources() {  
  104.         return null;  
  105.     }  
  106.   
  107.     public int getCount() {  
  108.         return mImageIds.length;  
  109.     }  
  110.   
  111.     public Object getItem(int position) {  
  112.         return position;  
  113.     }  
  114.   
  115.     public long getItemId(int position) {  
  116.         return position;  
  117.     }  
  118.   
  119.     public View getView(int position, View convertView, ViewGroup parent) {  
  120.         return mImages[position];  
  121.     }  
  122.   
  123.     public float getScale(boolean focused, int offset) {  
  124.         return Math.max(01.0f / (float) Math.pow(2, Math.abs(offset)));  
  125.     }  
  126. }  

main.xml:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <com.test3dgallary.GallaryFor3D  
  7.         android:id="@+id/main_gallery"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#000"  
  11.         android:spacing="20dp" />  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/main_iv_show"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="match_parent" />  
  17.   
  18. </LinearLayout> 
原创粉丝点击