上篇介绍了使用Animation实现3D动画旋转翻页效果,现在介绍图片倒影实现,先看效果图

本示例主要通过自定义Gallery和ImageAdapter(继承自BaseAdapter)实现
1、倒影绘制
ImageAdapter继承自BaseAdapter,详细实现可见 Android 滑动效果入门篇(二)—— Gallery 这里重点介绍倒影原理及实现
倒影原理:
倒影效果是主要由原图+间距+倒影三部分组成,高度大约为原图的3/2(原图为1、倒影为1/2)
原图,就是我们看到了最开始的图片
间距,是原图与倒影之间的间隙,如:reflectionGap = 4;
倒影,是原图下半部分1/2高度,通过矩阵变换matrix.preScale(1, -1); 获取倒立图片,然后再加上线性遮罩和阴影实现
倒影实现:
[java] view plain copy print ?
-
- public boolean createReflectedImages() {
- final int reflectionGap = 4;
- int index = 0;
- for (Map<String, Object> map : list) {
- Integer id = (Integer) map.get("image");
- Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), id);
- int width = originalImage.getWidth();
- int height = originalImage.getHeight();
-
- Matrix matrix = new Matrix();
- matrix.preScale(1, -1);
- Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
- Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
-
- Canvas canvas = new Canvas(bitmapWithReflection);
- canvas.drawBitmap(originalImage, 0, 0, null);
- Paint paint = new Paint();
- canvas.drawRect(0, height, width, height + reflectionGap, paint);
- canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
-
- paint = new Paint();
- LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
- paint.setShader(shader);
- paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
- canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
-
- ImageView imageView = new ImageView(mContext);
- imageView.setImageBitmap(bitmapWithReflection);
- imageView.setLayoutParams(new myGallery.LayoutParams(180, 240));
- imageView.setScaleType(ScaleType.MATRIX);
- mImages[index++] = imageView;
- }
- return true;
- }
2、myGallery
自定义Gallery来实现倒影图片的浏览与选择
[java] view plain copy print ?
- public class myGallery extends Gallery {
-
- private Camera mCamera = new Camera();
- private int mMaxRotationAngle = 60;
- private int mMaxZoom = -120;
- private int mCoveflowCenter;
-
- public myGallery(Context context) {
- super(context);
- this.setStaticTransformationsEnabled(true);
- }
-
- public myGallery(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.setStaticTransformationsEnabled(true);
- }
-
- public myGallery(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- this.setStaticTransformationsEnabled(true);
- }
-
- public int getMaxRotationAngle() {
- return mMaxRotationAngle;
- }
-
- public void setMaxRotationAngle(int maxRotationAngle) {
- mMaxRotationAngle = maxRotationAngle;
- }
-
- public int getMaxZoom() {
- return mMaxZoom;
- }
-
- public void setMaxZoom(int maxZoom) {
- mMaxZoom = maxZoom;
- }
-
-
- private int getCenterOfCoverflow() {
- return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
- }
-
-
- private static int getCenterOfView(View view) {
- return view.getLeft() + view.getWidth() / 2;
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- mCoveflowCenter = getCenterOfCoverflow();
- super.onSizeChanged(w, h, oldw, oldh);
- }
-
- @Override
- protected boolean getChildStaticTransformation(View child, Transformation trans) {
- final int childCenter = getCenterOfView(child);
- final int childWidth = child.getWidth();
- int rotationAngle = 0;
-
- trans.clear();
- trans.setTransformationType(Transformation.TYPE_BOTH);
-
- if (childCenter == mCoveflowCenter) {
- transformImageBitmap((ImageView) child, trans, 0);
- } else {
- rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );
- if (Math.abs(rotationAngle) > mMaxRotationAngle) {
- rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
- }
- transformImageBitmap((ImageView) child, trans, rotationAngle);
- }
-
- return true;
- }
-
- private void transformImageBitmap(ImageView child, Transformation trans, int rotationAngle) {
- mCamera.save();
-
- final Matrix imageMatrix = trans.getMatrix();
- final int imageHeight = child.getLayoutParams().height;
- final int imageWidth = child.getLayoutParams().width;
- final int rotation = Math.abs(rotationAngle);
-
-
- mCamera.translate(0.0f, 0.0f, 100.0f);
-
-
- if (rotation < mMaxRotationAngle) {
- float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
- mCamera.translate(0.0f, 0.0f, zoomAmount);
- }
-
- mCamera.rotateY(rotationAngle);
-
- mCamera.getMatrix(imageMatrix);
- imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
- imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
-
- mCamera.restore();
- }
- }
3、Activity
Activity中,主要实现自定义Gallery的图片填充ImageAdapter、myGallery选择事件监听、点击事件监听
[java] view plain copy print ?
- private void initRes(){
- tvTitle = (TextView) findViewById(R.id.tvTitle);
- gallery = (myGallery) findViewById(R.id.mygallery);
-
- adapter = new ImageAdapter(this);
- adapter.createReflectedImages();
- gallery.setAdapter(adapter);
-
- gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- tvTitle.setText(adapter.titles[position]);
- }
-
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
-
- gallery.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(Main.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();
- }
- });
- }
main.xml布局文件中,通过实现自定义的myGallery,来显示图片集合
[html] view plain copy print ?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:textSize="16sp" />
-
- <com.homer.reflect.myGallery
- android:id="@+id/mygallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/tvTitle"
- android:layout_marginTop="10dip" />
- </RelativeLayout>