http://1002878825-qq-com.iteye.com/blog/1194788
Java代码
- 主页面:Gallery3DActivity
- package
- import
- import
- import
- import
- import
- import
- public class Gallery3DActivity extends
- @Override
- protected void
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Integer[] images = { R.drawable.aa, R.drawable.bb,
- R.drawable.cc, R.drawable.dd, R.drawable.ee,
- /* R.drawable.gg,R.drawable.hh ,
- R.drawable.jj, R.drawable.kk, R.drawable.ll*/};
- new ImageAdapter(this, images);
- //创建倒影效果
- this.findViewById(R.id.Gallery01);
- 0);
- 50); //图片之间的间距
- galleryFlow.setAdapter(adapter);
- new
- public void
- int position, long
- Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
- }
- });
- 0);
- }
- }
Java代码
- GalleryFlow类:作用是控制Gallery
- package
- import
- import
- import
- import
- import
- import
- import
- import
- public class GalleryFlow extends
- private Camera mCamera = new Camera();//相机类
- private int mMaxRotationAngle = 60;//最大转动角度
- private int mMaxZoom = -300;最大缩放值
- private int mCoveflowCenter;//半径值
- public
- super(context);
- //支持转换 ,执行getChildStaticTransformation方法
- this.setStaticTransformationsEnabled(true);
- }
- public
- super(context, attrs);
- this.setStaticTransformationsEnabled(true);
- }
- public GalleryFlow(Context context, AttributeSet attrs, int
- super(context, attrs, defStyle);
- this.setStaticTransformationsEnabled(true);
- }
- public int
- return
- }
- public void setMaxRotationAngle(int
- mMaxRotationAngle = maxRotationAngle;
- }
- public int
- return
- }
- public void setMaxZoom(int
- mMaxZoom = maxZoom;
- }
- private int
- return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
- + getPaddingLeft();
- }
- private static int
- "view left :"+view.getLeft());
- "view width :"+view.getWidth());
- return view.getLeft() + view.getWidth() / 2;
- }
- //控制gallery中每个图片的旋转(重写的gallery中方法)
- @Override
- protected boolean
- //取得当前子view的半径值
- final int
- "childCenter:"+childCenter);
- final int
- //旋转角度
- int rotationAngle = 0;
- //重置转换状态
- t.clear();
- //设置转换类型
- t.setTransformationType(Transformation.TYPE_MATRIX);
- //如果图片位于中心位置不需要进行旋转
- if
- 0);
- else
- //根据图片在gallery中的位置来计算图片的旋转角度
- int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
- "rotationAngle:"
- //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)
- if
- 0) ? -mMaxRotationAngle : mMaxRotationAngle;
- }
- transformImageBitmap((ImageView) child, t, rotationAngle);
- }
- return true;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int
- mCoveflowCenter = getCenterOfCoverflow();
- super.onSizeChanged(w, h, oldw, oldh);
- }
- private void
- int
- //对效果进行保存
- mCamera.save();
- final
- //图片高度
- final int
- //图片宽度
- final int
- //返回旋转角度的绝对值
- final int
- // 在Z轴上正向移动camera的视角,实际效果为放大图片。
- // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
- 0.0f, 10.0f, 300.0f);// 后面的值越高图片越缩小
- // As the angle of the view gets less, zoom in
- if
- float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
- 0.0f, 0.0f, zoomAmount);
- }
- // 在Y轴上旋转,对应图片竖向向里翻转。
- // 如果在X轴上旋转,则对应图片横向向里翻转。
- mCamera.rotateY(rotationAngle);
- mCamera.getMatrix(imageMatrix);
- 2), -(imageHeight / 2));
- 2), (imageHeight / 2));
- mCamera.restore();
- }
- }
Java代码
- 最后是把图片设置到Gallery中
- ImageAdapter类:
- package
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- public class ImageAdapter extends
- int
- private
- private
- private
- public
- mContext = c;
- mImageIds = ImageIds;
- new
- }
- /**
- * 创建倒影效果
- * @return
- */
- public boolean
- //倒影图和原图之间的距离
- final int reflectionGap = 4;
- int index = 0;
- for (int
- //返回原图解码之后的bitmap对象
- Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
- int
- int
- //创建矩阵对象
- new
- //指定一个角度以0,0为坐标进行旋转
- // matrix.setRotate(30);
- //指定矩阵(x轴不变,y轴相反)
- 1, -1);
- //将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
- 0,
- 2, width, height/2, matrix, false);
- //创建一个宽度不变,高度为原图+倒影图高度的位图
- Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
- 2), Config.ARGB_8888);
- //将上面创建的位图初始化到画布
- new
- 0, 0, null);
- new
- false);
- // canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);
- 0, height + reflectionGap, null);
- new
- false);
- /**
- * 参数一:为渐变起初点坐标x位置,
- * 参数二:为y轴位置,
- * 参数三和四:分辨对应渐变终点,
- * 最后参数为平铺方式,
- * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变
- */
- new LinearGradient(0,originalImage.getHeight(), 0,
- 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
- //设置阴影
- paint.setShader(shader);
- new
- //用已经定义好的画笔构建一个矩形阴影渐变效果
- 0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
- //创建一个ImageView用来显示已经画好的bitmapWithReflection
- new
- imageView.setImageBitmap(bitmapWithReflection);
- //设置imageView大小 ,也就是最终显示的图片大小
- new GalleryFlow.LayoutParams(220, 400));
- // imageView.setScaleType(ScaleType.MATRIX);
- mImages[index++] = imageView;
- }
- return true;
- }
- @SuppressWarnings("unused")
- private
- return null;
- }
- public int
- return
- }
- public Object getItem(int
- return
- }
- public long getItemId(int
- return
- }
- public View getView(int
- return
- }
- public float getScale(boolean focused, int
- return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
- }
- }
Java代码
- main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- "vertical"
- "fill_parent"
- "fill_parent"
- <com.xiawenquan.GalleryFlow
- "@+id/Gallery01"
- "20dip"
- "fill_parent"
- "wrap_content"
- />
- </LinearLayout>
- TextBitmap.rar (2.2 MB)
- 下载次数: 266