Android开发3D界面特效

Android开发3D界面特效

分类: Android 1169人阅读 评论(1) 收藏 举报

转自:http://my.oschina.net/u/577276/blog/63400




  1. package com.test.util;  
  2.   
  3. import com.dooya.activity.R;  
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Bitmap;  
  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.Bitmap.Config;  
  14. import android.graphics.PorterDuff.Mode;  
  15. import android.graphics.Shader.TileMode;  
  16. import android.graphics.drawable.BitmapDrawable;  
  17. import android.view.View;  
  18. import android.view.ViewGroup;  
  19. import android.widget.BaseAdapter;  
  20. import android.widget.ImageView;  
  21.   
  22. public class ImageAdapter extends BaseAdapter {  
  23.     int mGalleryItemBackground;  
  24.     private Context mContext;  
  25.     //加载资源图片  
  26.     private Integer[] mImageIds = {   
  27.             R.drawable.favorite,  
  28.             R.drawable.room,   
  29.             R.drawable.scene,  
  30.             R.drawable.security,   
  31.             R.drawable.time,  
  32.             R.drawable.set};  
  33.   
  34.     public ImageAdapter(Context c) {  
  35.         mContext = c;  
  36.     }  
  37.   
  38.     public int getCount() {  
  39.         return mImageIds.length;  
  40.     }  
  41.   
  42.     public Object getItem(int position) {  
  43.         return position;  
  44.     }  
  45.   
  46.     public long getItemId(int position) {  
  47.         return position;  
  48.     }  
  49.   
  50.     public View getView(int position, View convertView, ViewGroup parent) {  
  51.   
  52.         ImageView i = createReflectedImages(mContext,mImageIds[position]);  
  53.           
  54.         i.setLayoutParams(new CoverFlow.LayoutParams(120100));  
  55.         i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);  
  56.           
  57.         // 设置的抗锯齿  
  58.         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();  
  59.         drawable.setAntiAlias(true);  
  60.         return i;  
  61.     }  
  62.   
  63.     public float getScale(boolean focused, int offset) {  
  64.         return Math.max(01.0f / (float) Math.pow(2, Math.abs(offset)));  
  65.     }  
  66.       
  67.     /** 
  68.      * 设置镜像图像 
  69.      * @param mContext 
  70.      * @param imageId 
  71.      * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>  
  72.      */  
  73.     public ImageView createReflectedImages(Context mContext,int imageId) {  
  74.   
  75.         Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);  
  76.           
  77.         final int reflectionGap = 4;  
  78.           
  79.         int width = originalImage.getWidth();  
  80.         int height = originalImage.getHeight();  
  81.   
  82.         Matrix matrix = new Matrix();  
  83.         matrix.preScale(1, -1);  
  84.   
  85.         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,  
  86.                 height / 2, width, height / 2, matrix, false);  
  87.   
  88.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  89.                 (height + height / 2), Config.ARGB_8888);  
  90.   
  91.         Canvas canvas = new Canvas(bitmapWithReflection);  
  92.   
  93.         canvas.drawBitmap(originalImage, 00null);  
  94.   
  95.         Paint deafaultPaint = new Paint();  
  96.         canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);  
  97.   
  98.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  99.   
  100.         Paint paint = new Paint();  
  101.         LinearGradient shader = new LinearGradient(0, originalImage  
  102.                 .getHeight(), 0, bitmapWithReflection.getHeight()  
  103.                 + reflectionGap, 0x70ffffff0x00ffffff, TileMode.MIRROR);  
  104.   
  105.         paint.setShader(shader);  
  106.   
  107.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  108.   
  109.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  110.                 + reflectionGap, paint);  
  111.   
  112.         ImageView imageView = new ImageView(mContext);  
  113.         imageView.setImageBitmap(bitmapWithReflection);  
  114.   
  115.         return imageView;  
  116.     }  
  117.       
  118. }  


  1.  package com.test.util;  
  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 CoverFlow extends Gallery {  
  13.   
  14.     private Camera mCamera = new Camera();  
  15.     private int mMaxRotationAngle = 50;//60;  
  16.     private int mMaxZoom = -380;//-120;  
  17.     private int mCoveflowCenter;  
  18.     private boolean mAlphaMode = true;  
  19.     private boolean mCircleMode = false;  
  20.   
  21.     public CoverFlow(Context context) {  
  22.         super(context);  
  23.         this.setStaticTransformationsEnabled(true);  
  24.     }  
  25.   
  26.     public CoverFlow(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         this.setStaticTransformationsEnabled(true);  
  29.     }  
  30.   
  31.     public CoverFlow(Context context, AttributeSet attrs, int defStyle) {  
  32.         super(context, attrs, defStyle);  
  33.         this.setStaticTransformationsEnabled(true);  
  34.     }  
  35.   
  36.     public int getMaxRotationAngle() {  
  37.         return mMaxRotationAngle;  
  38.     }  
  39.   
  40.     public void setMaxRotationAngle(int maxRotationAngle) {  
  41.         mMaxRotationAngle = maxRotationAngle;  
  42.     }  
  43.   
  44.     public boolean getCircleMode() {  
  45.         return mCircleMode;  
  46.     }  
  47.   
  48.     public void setCircleMode(boolean isCircle) {  
  49.         mCircleMode = isCircle;  
  50.     }  
  51.   
  52.     public boolean getAlphaMode() {  
  53.         return mAlphaMode;  
  54.     }  
  55.   
  56.     public void setAlphaMode(boolean isAlpha) {  
  57.         mAlphaMode = isAlpha;  
  58.     }  
  59.   
  60.     public int getMaxZoom() {  
  61.         return mMaxZoom;  
  62.     }  
  63.   
  64.     public void setMaxZoom(int maxZoom) {  
  65.         mMaxZoom = maxZoom;  
  66.     }  
  67.   
  68.     private int getCenterOfCoverflow() {  
  69.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  70.                 + getPaddingLeft();  
  71.     }  
  72.   
  73.     private static int getCenterOfView(View view) {  
  74.         return view.getLeft() + view.getWidth() / 2;  
  75.     }  
  76.   
  77.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
  78.         final int childCenter = getCenterOfView(child);  
  79.         final int childWidth = child.getWidth();  
  80.         int rotationAngle = 0;  
  81.         t.clear();  
  82.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  83.         if (childCenter == mCoveflowCenter) {  
  84.             transformImageBitmap((ImageView) child, t, 0);  
  85.         } else {  
  86.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  87.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  88.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle  
  89.                         : mMaxRotationAngle;  
  90.             }  
  91.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  92.         }  
  93.         return true;  
  94.     }  
  95.   
  96.     /** 
  97.      *  
  98.      */  
  99.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  100.         mCoveflowCenter = getCenterOfCoverflow();  
  101.         super.onSizeChanged(w, h, oldw, oldh);  
  102.     }  
  103.   
  104.     /** 
  105.      * 把图像位图的角度通过 
  106.      */  
  107.     private void transformImageBitmap(ImageView child, Transformation t,  
  108.             int rotationAngle) {  
  109.         mCamera.save();  
  110.         final Matrix imageMatrix = t.getMatrix();  
  111.         final int imageHeight = child.getLayoutParams().height;  
  112.         final int imageWidth = child.getLayoutParams().width;  
  113.         final int rotation = Math.abs(rotationAngle);  
  114.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  115.   
  116.         // 如视图的角度更少,放大  
  117.         if (rotation <= mMaxRotationAngle) {  
  118.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  119.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  120.             if (mCircleMode) {  
  121.                 if (rotation < 40)  
  122.                     mCamera.translate(0.0f, 1550.0f);  
  123.                 else  
  124.                     mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);  
  125.             }  
  126.             if (mAlphaMode) {  
  127.                 ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));  
  128.             }  
  129.         }  
  130.         mCamera.rotateY(rotationAngle);  
  131.         mCamera.getMatrix(imageMatrix);  
  132.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  133.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  134.         mCamera.restore();  
  135.     }  
  136. }  


  1. package com.test.activity;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.os.Bundle;  
  6.   
  7. import android.view.WindowManager;  
  8.   
  9. import com.dooya.util.CoverFlow;  
  10.   
  11. import com.dooya.util.ImageAdapter;  
  12.   
  13.    
  14.   
  15. public class HomeActivity extends Activity {  
  16.   
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.   
  19.         super.onCreate(savedInstanceState);  
  20.   
  21.         CoverFlow cf = new CoverFlow(this);  
  22.   
  23.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  24.   
  25.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏  
  26.   
  27.         cf.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg));//背景  
  28.   
  29.         cf.setAdapter(new ImageAdapter(this));  
  30.   
  31.         ImageAdapter imageAdapter = new ImageAdapter(this);  
  32.   
  33.         cf.setAdapter(imageAdapter);  
  34.   
  35.         cf.setSelection(2true);  
  36.   
  37.         cf.setAnimationDuration(1000);  
  38.   
  39.         setContentView(cf);  
  40.   
  41.     }  
  42.   

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值