Android Gallery3D效果 代码

  贴上代码:
  1.   1.扩展Gallery:
  2.   public class GalleryFlow extends Gallery {
  3.   private Camera mCamera = new Camera();//相机类
  4.   private int mMaxRotationAngle = 60;//最大转动角度
  5.   private int mMaxZoom = -300;////最大缩放值
  6.   private int mCoveflowCenter;//半径值
  7.   public GalleryFlow(Context context) {
  8.   super(context);
  9.   //支持转换 ,执行getChildStaticTransformation方法
  10.   this.setStaticTransformationsEnabled(true);
  11.   }
  12.   public GalleryFlow(Context context, AttributeSet attrs) {
  13.   super(context, attrs);
  14.   this.setStaticTransformationsEnabled(true);
  15.   }
  16.   public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
  17.   super(context, attrs, defStyle);
  18.   this.setStaticTransformationsEnabled(true);
  19.   }
  20.   public int getMaxRotationAngle() {
  21.   return mMaxRotationAngle;
  22.   }
  23.   public void setMaxRotationAngle(int maxRotationAngle) {
  24.   mMaxRotationAngle = maxRotationAngle;
  25.   }
  26.   public int getMaxZoom() {
  27.   return mMaxZoom;
  28.   }
  29.   public void setMaxZoom(int maxZoom) {
  30.   mMaxZoom = maxZoom;
  31.   }
  32.   private int getCenterOfCoverflow() {
  33.   return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
  34.   + getPaddingLeft();
  35.   }
  36.   private static int getCenterOfView(View view) {
  37.   System.out.println("view left :"+view.getLeft());
  38.   System.out.println("view width :"+view.getWidth());
  39.   return view.getLeft() + view.getWidth() / 2;
  40.   }
  41.   //控制gallery中每个图片的旋转(重写的gallery中方法)
  42.   protected boolean getChildStaticTransformation(View child, Transformation t) {
  43.   //取得当前子view的半径值
  44.   final int childCenter = getCenterOfView(child);
  45.   System.out.println("childCenter:"+childCenter);
  46.   final int childWidth = child.getWidth();
  47.   //旋转角度
  48.   int rotationAngle = 0;
  49.   //重置转换状态
  50.   t.clear();
  51.   //设置转换类型
  52.   t.setTransformationType(Transformation.TYPE_MATRIX);
  53.   //如果图片位于中心位置不需要进行旋转
  54.   if (childCenter == mCoveflowCenter) {
  55.   transformImageBitmap((ImageView) child, t, 0);
  56.   } else {
  57.   //根据图片在gallery中的位置来计算图片的旋转角度
  58.   rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
  59.   System.out.println("rotationAngle:" +rotationAngle);
  60.   //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)
  61.   if (Math.abs(rotationAngle) > mMaxRotationAngle) {
  62.   rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
  63.   }
  64.   transformImageBitmap((ImageView) child, t, rotationAngle);
  65.   }
  66.   return true;
  67.   }
  68.   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  69.   mCoveflowCenter = getCenterOfCoverflow();
  70.   super.onSizeChanged(w, h, oldw, oldh);
  71.   }
  72.   private void transformImageBitmap(ImageView child, Transformation t,
  73.   int rotationAngle) {
  74.   //对效果进行保存
  75.   mCamera.save();
  76.   final Matrix imageMatrix = t.getMatrix();
  77.   //图片高度
  78.   final int imageHeight = child.getLayoutParams().height;
  79.   //图片宽度
  80.   final int imageWidth = child.getLayoutParams().width;
  81.   //返回旋转角度的绝对值
  82.   final int rotation = Math.abs(rotationAngle);
  83.   // 在Z轴上正向移动camera的视角,实际效果为放大图片.
  84.   // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动.
  85.   mCamera.translate(0.0f, 0.0f, 100.0f);
  86.   // As the angle of the view gets less, zoom in
  87.   if (rotation < mMaxRotationAngle) {
  88.   float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
  89.   mCamera.translate(0.0f, 0.0f, zoomAmount);
  90.   }
  91.   // 在Y轴上旋转,对应图片竖向向里翻转.
  92.   // 如果在X轴上旋转,则对应图片横向向里翻转.
  93.   mCamera.rotateY(rotationAngle);
  94.   mCamera.getMatrix(imageMatrix);
  95.   imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
  96.   imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
  97.   mCamera.restore();
  98.   }
  99.   }
复制代码   2.填充图片容器(BaseAdapter):
  1.   public class ImageAdapter extends BaseAdapter {
  2.   int mGalleryItemBackground;
  3.   private Context mContext;
  4.   private Integer[] mImageIds;
  5.   private ImageView[] mImages;
  6.   public ImageAdapter(Context c, Integer[] ImageIds) {
  7.   mContext = c;
  8.   mImageIds = ImageIds;
  9.   mImages = new ImageView[mImageIds.length];
  10.   }
  11.   /**
  12.   * 创建倒影效果
  13.   * @return
  14.   */
  15.   public boolean createReflectedImages() {
  16.   //倒影图和原图之间的距离
  17.   final int reflectionGap = 4;
  18.   int index = 0;
  19.   for (int imageId : mImageIds) {
  20.   //返回原图解码之后的bitmap对象
  21.   Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
  22.   int width = originalImage.getWidth();
  23.   int height = originalImage.getHeight();
  24.   //创建矩阵对象
  25.   Matrix matrix = new Matrix();
  26.   //指定一个角度以0,0为坐标进行旋转
  27.   // matrix.setRotate(30);
  28.   //指定矩阵(x轴不变,y轴相反)
  29.   matrix.preScale(1, -1);
  30.   //将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
  31.   Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
  32.   height/2, width, height/2, matrix, false);
  33.   //创建一个宽度不变,高度为原图+倒影图高度的位图
  34.   Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
  35.   (height + height / 2), Config.ARGB_8888);
  36.   //将上面创建的位图初始化到画布
  37.   Canvas canvas = new Canvas(bitmapWithReflection);
  38.   canvas.drawBitmap(originalImage, 0, 0, null);
  39.   Paint deafaultPaint = new Paint();
  40.   deafaultPaint.setAntiAlias(false);
  41.   // canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);
  42.   canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  43.   Paint paint = new Paint();
  44.   paint.setAntiAlias(false);
  45.   /**
  46.   * 参数一:为渐变起初点坐标x位置,
  47.   * 参数二:为y轴位置,
  48.   * 参数三和四:分辨对应渐变终点,
  49.   * 最后参数为平铺方式,
  50.   * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变
  51.   */
  52.   LinearGradient shader = new LinearGradient(0,originalImage.getHeight(), 0,
  53.   bitmapWithReflection.getHeight() + reflectionGap,0x70ffffff, 0x00ffffff, TileMode.MIRROR);
  54.   //设置阴影
  55.   paint.setShader(shader);
  56.   paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
  57.   //用已经定义好的画笔构建一个矩形阴影渐变效果
  58.   canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
  59.   //创建一个ImageView用来显示已经画好的bitmapWithReflection
  60.   ImageView imageView = new ImageView(mContext);
  61.   imageView.setImageBitmap(bitmapWithReflection);
  62.   //设置imageView大小 ,也就是最终显示的图片大小
  63.   imageView.setLayoutParams(new GalleryFlow.LayoutParams(300, 400));
  64.   //imageView.setScaleType(ScaleType.MATRIX);
  65.   mImages[index++] = imageView;
  66.   }
  67.   return true;
  68.   }
  69.   @SuppressWarnings("unused")
  70.   private Resources getResources() {
  71.   return null;
  72.   }
  73.   public int getCount() {
  74.   return mImageIds.length;
  75.   }
  76.   public Object getItem(int position) {
  77.   return position;
  78.   }
  79.   public long getItemId(int position) {
  80.   return position;
  81.   }
  82.   public View getView(int position, View convertView, ViewGroup parent) {
  83.   return mImages[position];
  84.   }
  85.   public float getScale(boolean focused, int offset) {
  86.   return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  87.   }
  88.   }
复制代码   3.创建Activity:
  1.   public class Gallery3DActivity extends Activity {
  2.   public void onCreate(Bundle savedInstanceState) {
  3.   super.onCreate(savedInstanceState);
  4.   setContentView(R.layout.layout_gallery);
  5.   Integer[] images = { R.drawable.img0001, R.drawable.img0030,
  6.   R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
  7.   R.drawable.img0230, R.drawable.img0330,R.drawable.img0354 };
  8.   ImageAdapter adapter = new ImageAdapter(this, images);
  9.   adapter.createReflectedImages();//创建倒影效果
  10.   GalleryFlow galleryFlow = (GalleryFlow) this.findViewById(R.id.Gallery01);
  11.   galleryFlow.setFadingEdgeLength(0);
  12.   galleryFlow.setSpacing(-100); //图片之间的间距
  13.   galleryFlow.setAdapter(adapter);
  14.   galleryFlow.setOnItemClickListener(new OnItemClickListener() {
  15.   public void onItemClick(AdapterView<?> parent, View view,
  16.   int position, long id) {
  17.   Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
  18.   }
  19.   });
  20.   galleryFlow.setSelection(4);
  21.   }
  22.   }
复制代码   以上实现代码里面我都做了注释相信大家完全可以看懂.稍微解释下,在BaseAdapter中主要做了图片的倒影效果以及创建了对原始图片和倒影的显示区域.GalleryFlow中主要做了对图片的旋转和缩放操作,根据图片的屏幕中的位置对其进行旋转缩放操作

更多相关文章

  1. 【Android】通过软引用实现图片缓存,防止内存溢出
  2. Android(安卓)图片平铺效果
  3. Android(安卓)Launcher 之 图标加框 优化显示效果
  4. android 如何给图片添加水印
  5. Android各种花式酷炫自定义控件开源库集合(1)。
  6. Android控件开发之Gallery
  7. Android—将Bitmap图片保存到SD卡目录下或者指定目录
  8. [置顶] android中加载图片
  9. Android(安卓)textView文字添加图片 imageSpan使用

随机推荐

  1. Android(安卓)SQLiteStatement 编译、执
  2. Android(安卓)APK反编译详解(附图)
  3. Android(安卓)代码名字-API级别-版本号-N
  4. android的编译和运行过程深入分析
  5. android权限机制,你真的了解么
  6. Android的 linux内核
  7. 图解 Android View动画中 android:pivotX
  8. 你的手机到底安不安全?看看Android和iOS是
  9. 简单实现RecyclerView版本的竖向翻页效果
  10. webrtc 之android与PC互通