android Gallery镜像倒影特效

 

本文档将介绍在android上如何实现一个倒影效果的Gallery。代码如下:

 

MirrorView.java是我自己实现的一个类,继承自Gallery类。重写了getChildStaticTransformation() 方法.

 

 

package com.demo;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;

public class MirrorView extends Gallery {

    private Camera mCamera = new Camera();     // 是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等
    private int mMaxRotationAngle = 60;     // 是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度
    private int mMaxZoom = -380;             // 是图片在z轴平移的距离,视觉上看起来就是放大缩小的效果.
    private int mCoveflowCenter;
    private boolean mAlphaMode = true;
    private boolean mCircleMode = false;

    public MirrorView(Context context) {
        super(context);
        this.setStaticTransformationsEnabled(true);
    }

    public MirrorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setStaticTransformationsEnabled(true);
    }

    public MirrorView(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 boolean getCircleMode() {
        return mCircleMode;
    }

    public void setCircleMode(boolean isCircle) {
        mCircleMode = isCircle;
    }

    public boolean getAlphaMode() {
        return mAlphaMode;
    }

    public void setAlphaMode(boolean isAlpha) {
        mAlphaMode = isAlpha;
    }

    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;
    }

    protected boolean getChildStaticTransformation(View child, Transformation t) {
        final int childCenter = getCenterOfView(child);
        final int childWidth = child.getWidth();
        int rotationAngle = 0;
        t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);
        if (childCenter == mCoveflowCenter) {
            transformImageBitmap((ImageView) child, t, 0);
        } else {
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                rotationAngle = (rotationAngle < 0) ? - mMaxRotationAngle : mMaxRotationAngle;
            }
            transformImageBitmap((ImageView) child, t, rotationAngle);
        }
        return true;
    }

    /**
     * This is called during layout when the size of this view has changed. If
     * you were just added to the view hierarchy, you're called with the old
     * values of 0.
     *
     * @param w
     *            Current width of this view.
     * @param h
     *            Current height of this view.
     * @param oldw
     *            Old width of this view.
     * @param oldh
     *            Old height of this view.
     */
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCoveflowCenter = getCenterOfCoverflow();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     * Transform the Image Bitmap by the Angle passed
     *
     * @param imageView
     *            ImageView the ImageView whose bitmap we want to rotate
     * @param t
     *            transformation
     * @param rotationAngle
     *            the Angle by which to rotate the Bitmap
     */
    private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) {
        mCamera.save();
        final Matrix imageMatrix = t.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);
        // As the angle of the view gets less, zoom in
        if (rotation <= mMaxRotationAngle) {
            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
            mCamera.translate(0.0f, 0.0f, zoomAmount);
            if (mCircleMode) {
                if (rotation < 40)
                    mCamera.translate(0.0f, 155, 0.0f);
                else
                    mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);
            }
            if (mAlphaMode) {
                ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));
            }
        }
        mCamera.rotateY(rotationAngle);
        mCamera.getMatrix(imageMatrix);
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        mCamera.restore();
    }
}

 

--------------------------------------------------------------------------

 

自己实现的一个ReflectionImage类,用来实现图片的反转以及蒙板效果。

 

package com.demo;
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.LinearGradient; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Bitmap.Config; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.Shader.TileMode; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.widget.ImageView;

public class ReflectionImage extends ImageView { 
    //是否为Reflection模式 
    private boolean mReflectionMode = true; 
    public ReflectionImage(Context context) { 
        super(context); 
    } 
    public ReflectionImage(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        //取得原始图片的bitmap并重画 
        Bitmap originalImage = ((BitmapDrawable)this.getDrawable()).getBitmap(); 
        DoReflection(originalImage); 
    } 
    public ReflectionImage(Context context, AttributeSet attrs, 
            int defStyle) { 
        super(context, attrs, defStyle); 
        Bitmap originalImage = ((BitmapDrawable)this.getDrawable()).getBitmap(); 
        DoReflection(originalImage); 
    } 
    public void setReflectionMode(boolean isRef) { 
        mReflectionMode = isRef; 
    } 
    public boolean getReflectionMode() { 
        return mReflectionMode; 
    } 
    //偷懒了,只重写了setImageResource,和构造函数里面干了同样的事情 
    @Override 
    public void setImageResource(int resId) { 
        Bitmap originalImage = BitmapFactory.decodeResource( 
                getResources(), resId); 
        DoReflection(originalImage); 
        //super.setImageResource(resId); 
    } 
    private void DoReflection(Bitmap originalImage) { 
        final int reflectionGap = 4;                            //原始图片和反射图片中间的间距 
        int width = originalImage.getWidth(); 
        int height = originalImage.getHeight(); 
         
        //反转 
        Matrix matrix = new Matrix(); 
        matrix.preScale(1, -1); 
        //reflectionImage就是下面透明的那部分,可以设置它的高度为原始的3/4,这样效果会更好些 
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, false); 
        //创建一个新的bitmap,高度为原来的两倍 
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height), Config.ARGB_8888); 
        Canvas canvasRef = new Canvas(bitmapWithReflection); 
         
        //先画原始的图片 
        canvasRef.drawBitmap(originalImage, 0, 0, null); 
        //画间距 
        Paint deafaultPaint = new Paint(); 
        canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint); 
         
        //画被反转以后的图片 
        canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
        // 创建一个渐变的蒙版放在下面被反转的图片上面 
        Paint paint = new Paint(); 
        LinearGradient shader = new LinearGradient(0, 
                originalImage.getHeight(), 0, bitmapWithReflection.getHeight()+ reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP); 
        // Set the paint to use this shader (linear gradient) 
        paint.setShader(shader); 
        // Set the Transfer mode to be porter duff and destination in 
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
        // Draw a rectangle using the paint with our linear gradient 
        canvasRef.drawRect(0, height, width, bitmapWithReflection.getHeight() 
                + reflectionGap, paint); 
      //调用ImageView中的setImageBitmap 
        this.setImageBitmap(bitmapWithReflection); 
    } 

 

-----------------------------------------------------------------------------------

为MirrorView类实现的adapter。

 

package com.demo;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class AddImgAdp extends BaseAdapter {
     int mGalleryItemBackground; 
     private Context mContext; 
    private Integer[] Imgid = { R.drawable.gallery_photo_1,
            R.drawable.gallery_photo_2, R.drawable.gallery_photo_3,
            R.drawable.gallery_photo_4, R.drawable.gallery_photo_5,
            R.drawable.gallery_photo_6, R.drawable.gallery_photo_7 };

    public AddImgAdp(Context c) { 
        mContext = c; 
    } 
    public int getCount() { 
        return Imgid.length; 
    } 
    public Object getItem(int position) { 
        return position;  
    } 
    public long getItemId(int position) { 
        return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
        ReflectionImage i = new ReflectionImage(mContext); 
         
        i.setImageResource(Imgid[position]); 
        i.setLayoutParams(new MirrorView.LayoutParams(100, 200)); 
        i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
        // Make sure we set anti-aliasing otherwise we get jaggies 
        BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); 
        drawable.setAntiAlias(true); 
        return i; 
    } 

    public float getScale(boolean focused, int offset) { 
        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); 
    } 
}

 

-----------------------------------------------------------

xml布局文件简单的很,代码如下:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center">

    <com.demo.MirrorView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/examplegallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

 

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值