android 拖动图片3D效果

 3D效果的相册

 

  本程序main.xml源码

  1.    
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     android:background="#ffffff">  
  9.   
  10. <com.sx.GalleryFlow   
  11.     android:id="@+id/Gallery01"   
  12.     android:layout_width="fill_parent"   
  13.     android:layout_height="wrap_content"   
  14.     android:layout_centerInParent="true"/>  
  15. </RelativeLayout>  


Activity java源码

  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3.   
  4. public class ActivityMain extends Activity   
  5. {  
  6.  public void onCreate(Bundle savedInstanceState)   
  7.  {  
  8.         super.onCreate(savedInstanceState);  
  9.     
  10.         setContentView(R.layout.layout_gallery);  
  11.           
  12.         Integer[] images = {   
  13.                 R.drawable.img,  
  14.                 R.drawable.img3,  
  15.                 R.drawable.img15,  
  16.                 R.drawable.img21                  
  17.                 };  
  18.           
  19.         ImageAdapter adapter = new ImageAdapter(this, images);  
  20.         adapter.createReflectedImages();  
  21.   
  22.         GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);  
  23.         galleryFlow.setAdapter(adapter);          
  24.  }  
  25. }  

ImageAdapter java源码

  1. import android.content.Context;  
  2. import android.content.res.Resources;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.LinearGradient;  
  7. import android.graphics.Matrix;  
  8. import android.graphics.Paint;  
  9. import android.graphics.PorterDuffXfermode;  
  10. import android.graphics.Bitmap.Config;  
  11. import android.graphics.PorterDuff.Mode;  
  12. import android.graphics.Shader.TileMode;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.ImageView;  
  17. import android.widget.ImageView.ScaleType;  
  18.   
  19. public class ImageAdapter extends BaseAdapter  
  20. {  
  21.   
  22.      int mGalleryItemBackground;  
  23.      private Context    mContext;  
  24.      private Integer[]  mImageIds;  
  25.      private ImageView[] mImages;  
  26.   
  27.      public ImageAdapter(Context c, Integer[] ImageIds)   
  28.      {  
  29.          mContext  = c;  
  30.          mImageIds = ImageIds;  
  31.          mImages   = new ImageView[mImageIds.length];  
  32.      }  
  33.   
  34.      public boolean createReflectedImages()   
  35.      {  
  36.          final int reflectionGap = 4;  
  37.          int index = 0;  
  38.   
  39.          for (int imageId : mImageIds)  
  40.          {  
  41.              Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);  
  42.              int width  = originalImage.getWidth();  
  43.              int height = originalImage.getHeight();  
  44.   
  45.              Matrix matrix = new Matrix();  
  46.              matrix.preScale(1, -1);  
  47.   
  48.              Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false);  
  49.   
  50.              Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);  
  51.   
  52.              Canvas canvas = new Canvas(bitmapWithReflection);  
  53.   
  54.              canvas.drawBitmap(originalImage, 0, 0, null);  
  55.   
  56.              Paint deafaultPaint = new Paint();  
  57.              canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);  
  58.   
  59.              canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  60.   
  61.              Paint paint = new Paint();  
  62.              LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight()  
  63.                                                         + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);  
  64.   
  65.              paint.setShader(shader);  
  66.   
  67.              paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  68.   
  69.              canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);  
  70.   
  71.              ImageView imageView = new ImageView(mContext);  
  72.              imageView.setImageBitmap(bitmapWithReflection);  
  73.              imageView.setLayoutParams(new GalleryFlow.LayoutParams(250, 340));  
  74.              imageView.setScaleType(ScaleType.FIT_XY);  
  75.              mImages[index++] = imageView;  
  76.          }  
  77.          return true;  
  78.      }  
  79.   
  80.      private Resources getResources()   
  81.      {  
  82.          // TODO Auto-generated method stub  
  83.          return null;  
  84.      }  
  85.   
  86.      public int getCount()   
  87.      {  
  88.          return mImageIds.length;  
  89.      }  
  90.   
  91.      public Object getItem(int position)  
  92.      {  
  93.          return position;  
  94.      }  
  95.   
  96.      public long getItemId(int position)  
  97.      {  
  98.          return position;  
  99.      }  
  100.   
  101.      public View getView(int position, View convertView, ViewGroup parent)  
  102.      {  
  103.          return mImages[position];  
  104.      }  
  105.   
  106.      public float getScale(boolean focused, int offset)   
  107.      {  
  108.          return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));  
  109.      }  
  110. }  

GalleryFlow java源码

  1. import android.content.Context;  
  2. import android.graphics.Camera;  
  3. import android.graphics.Matrix;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.view.animation.Transformation;  
  7. import android.widget.Gallery;  
  8. import android.widget.ImageView;  
  9.   
  10. public class GalleryFlow extends Gallery   
  11. {  
  12.     private Camera mCamera = new Camera();  
  13.     private int mMaxRotationAngle = 60;  
  14.     private int mMaxZoom = -120;  
  15.     private int mCoveflowCenter;  
  16.   
  17.     public GalleryFlow(Context context)  
  18.     {  
  19.         super(context);  
  20.         this.setStaticTransformationsEnabled(true);  
  21.     }  
  22.   
  23.     public GalleryFlow(Context context, AttributeSet attrs)  
  24.     {  
  25.         super(context, attrs);  
  26.         this.setStaticTransformationsEnabled(true);  
  27.     }  
  28.   
  29.     public GalleryFlow(Context context, AttributeSet attrs, int defStyle)   
  30.     {  
  31.         super(context, attrs, defStyle);  
  32.         this.setStaticTransformationsEnabled(true);  
  33.     }  
  34.   
  35.     public int getMaxRotationAngle()   
  36.     {  
  37.         return mMaxRotationAngle;  
  38.     }  
  39.   
  40.     public void setMaxRotationAngle(int maxRotationAngle)   
  41.     {  
  42.         mMaxRotationAngle = maxRotationAngle;  
  43.     }  
  44.   
  45.     public int getMaxZoom()   
  46.     {  
  47.         return mMaxZoom;  
  48.     }  
  49.   
  50.     public void setMaxZoom(int maxZoom)   
  51.     {  
  52.         mMaxZoom = maxZoom;  
  53.     }  
  54.   
  55.     private int getCenterOfCoverflow()   
  56.     {  
  57.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();  
  58.     }  
  59.   
  60.     private static int getCenterOfView(View view)   
  61.     {  
  62.         return view.getLeft() + view.getWidth() / 2;  
  63.     }  
  64.   
  65.     protected boolean getChildStaticTransformation(View child, Transformation t)  
  66.     {  
  67.         final int childCenter = getCenterOfView(child);  
  68.         final int childWidth = child.getWidth();  
  69.         int rotationAngle = 0;  
  70.   
  71.         t.clear();  
  72.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  73.   
  74.         if (childCenter == mCoveflowCenter)   
  75.         {  
  76.             transformImageBitmap((ImageView) child, t, 0);  
  77.         }   
  78.         else  
  79.         {  
  80.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  81.             if (Math.abs(rotationAngle) > mMaxRotationAngle)  
  82.             {  
  83.              rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;  
  84.             }  
  85.               
  86.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  87.         }  
  88.   
  89.         return true;  
  90.     }  
  91.   
  92.     protected void onSizeChanged(int w, int h, int oldw, int oldh)  
  93.     {  
  94.         mCoveflowCenter = getCenterOfCoverflow();  
  95.         super.onSizeChanged(w, h, oldw, oldh);  
  96.     }  
  97.   
  98.     private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle)  
  99.     {  
  100.         mCamera.save();  
  101.         final Matrix imageMatrix = t.getMatrix();  
  102.         final int imageHeight = child.getLayoutParams().height;  
  103.         final int imageWidth = child.getLayoutParams().width;  
  104.         final int rotation = Math.abs(rotationAngle);  
  105.   
  106.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
  107.          // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
  108.          mCamera.translate(0.0f, 0.0f, 100.0f);  
  109.   
  110.         // As the angle of the view gets less, zoom in  
  111.         if (rotation < mMaxRotationAngle)  
  112.         {  
  113.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  114.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  115.         }  
  116.   
  117.         // 在Y轴上旋转,对应图片竖向向里翻转。  
  118.          // 如果在X轴上旋转,则对应图片横向向里翻转。  
  119.          mCamera.rotateY(rotationAngle);  
  120.         mCamera.getMatrix(imageMatrix);  
  121.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  122.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  123.         mCamera.restore();  
  124.     }  
  125. }  

 

http://www.linuxidc.com/Linux/2011-08/41159p3.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值