1、paint.setShader(shader);/ Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN);/ Draw a rectangle using the paint with our linear gradientcanvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);return bitmap
2、WithReflection;android Gallery镜像倒影特效2011-06-25 14:5888人阅读评论(0)收藏举报本文档将介绍在android上如何实现一个倒影效果的Gallery。代码如下:MirrorView.java是我自己实现的一个类,继承自Gallery类。重写了getChildStaticTransformation() 方法.package com.demo;import android.content.Context;import android.graphics.Camera;import android.graphics.Matrix;import and
3、roid.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 = 6
4、0; / 是图片绕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
5、MirrorView(Context context, AttributeSet attrs) super(context, attrs); public MirrorView(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle); public int getMaxRotationAngle() return mMaxRotationAngle; public void setMaxRotationAngle(int maxRotationAngle) mMaxRotationAn
6、gle = 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; pub
7、lic 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 getChildSta
8、ticTransformation(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,
9、 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 tru
10、e; /* This is called during layout when the size of this view has changed. If* you were just added to the view hierarchy, youre 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 t
11、his 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*
12、 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.getLayou
13、tParams().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) 4
14、0) 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)
15、; imageMatrix.postTranslate(imageWidth / 2), (imageHeight / 2); mCamera.restore();-自己实现的一个ReflectionImage类,用来实现图片的反转以及蒙板效果。import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Paint;import a
16、ndroid.graphics.PorterDuffXfermode;import android.graphics.Bitmap.Config;import android.graphics.PorterDuff.Mode;import android.graphics.Shader.TileMode;import android.graphics.drawable.BitmapDrawable;public class ReflectionImage extends ImageView /是否为Reflection模式 private boolean mReflectionMode = t
17、rue; public ReflectionImage(Context context) public ReflectionImage(Context context, AttributeSet attrs) /取得原始图片的bitmap并重画 Bitmap originalImage = (BitmapDrawable)this.getDrawable().getBitmap(); DoReflection(originalImage); public ReflectionImage(Context context, AttributeSet attrs, int defStyle) pub
18、lic void setReflectionMode(boolean isRef) mReflectionMode = isRef; public boolean getReflectionMode() return mReflectionMode; /偷懒了,只重写了setImageResource,和构造函数里面干了同样的事情 Override public void setImageResource(int resId) Bitmap originalImage = BitmapFactory.decodeResource( getResources(), resId); /super.
19、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,这样效
20、果会更好些 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.drawBit
21、map(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);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1