zoukankan      html  css  js  c++  java
  • Android控件开发之Gallery3D效果

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


    ImageAdapter.Java

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


     

    GalleryFlow.java

    1. package xiaosi.GalleryFlow;  
    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 GalleryFlow extends Gallery {  
    13.   
    14.     private Camera mCamera = new Camera();  
    15.     private int mMaxRotationAngle = 60;  
    16.     private int mMaxZoom = -120;  
    17.     private int mCoveflowCenter;  
    18.   
    19.     public GalleryFlow(Context context) {  
    20.             super(context);  
    21.             this.setStaticTransformationsEnabled(true);  
    22.     }  
    23.   
    24.     public GalleryFlow(Context context, AttributeSet attrs) {  
    25.             super(context, attrs);  
    26.             this.setStaticTransformationsEnabled(true);  
    27.     }  
    28.   
    29.     public GalleryFlow(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.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
    60.   
    61.             final int childCenter = getCenterOfView(child);  
    62.             final int childWidth = child.getWidth();  
    63.             int rotationAngle = 0;  
    64.   
    65.             t.clear();  
    66.             t.setTransformationType(Transformation.TYPE_MATRIX);  
    67.   
    68.             if (childCenter == mCoveflowCenter) {  
    69.                     transformImageBitmap((ImageView) child, t, 0);  
    70.             } else {  
    71.                     rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
    72.                     if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
    73.                             rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle  
    74.                                             : mMaxRotationAngle;  
    75.                     }  
    76.                     transformImageBitmap((ImageView) child, t, rotationAngle);  
    77.             }  
    78.   
    79.             return true;  
    80.     }  
    81.   
    82.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
    83.             mCoveflowCenter = getCenterOfCoverflow();  
    84.             super.onSizeChanged(w, h, oldw, oldh);  
    85.     }  
    86.   
    87.     private void transformImageBitmap(ImageView child, Transformation t,  
    88.                     int rotationAngle) {  
    89.             mCamera.save();  
    90.             final Matrix imageMatrix = t.getMatrix();  
    91.             final int imageHeight = child.getLayoutParams().height;  
    92.             final int imageWidth = child.getLayoutParams().width;  
    93.             final int rotation = Math.abs(rotationAngle);  
    94.   
    95.             // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
    96.             // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
    97.             mCamera.translate(0.0f, 0.0f, 100.0f);  
    98.   
    99.             // As the angle of the view gets less, zoom in  
    100.             if (rotation < mMaxRotationAngle) {  
    101.                     float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
    102.                     mCamera.translate(0.0f, 0.0f, zoomAmount);  
    103.             }  
    104.   
    105.             // 在Y轴上旋转,对应图片竖向向里翻转。  
    106.             // 如果在X轴上旋转,则对应图片横向向里翻转。  
    107.             mCamera.rotateY(rotationAngle);  
    108.             mCamera.getMatrix(imageMatrix);  
    109.             imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
    110.             imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
    111.             mCamera.restore();  
    112.     }  
    113. }  


     

    源代码:点击打开链接

  • 相关阅读:
    CodeForces 681D Gifts by the List (树上DFS)
    UVa 12342 Tax Calculator (水题,纳税)
    CodeForces 681C Heap Operations (模拟题,优先队列)
    CodeForces 682C Alyona and the Tree (树上DFS)
    CodeForces 682B Alyona and Mex (题意水题)
    CodeForces 682A Alyona and Numbers (水题,数学)
    Virtualizing memory type
    页面跳转
    PHP Misc. 函数
    PHP 5 Math 函数
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6721788.html
Copyright © 2011-2022 走看看