Android图片镜像倒影特效Word格式文档下载.docx
《Android图片镜像倒影特效Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Android图片镜像倒影特效Word格式文档下载.docx(13页珍藏版)》请在冰豆网上搜索。
paint.setShader(shader);
//SettheTransfermodetobeporterduffanddestinationin
paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
//Drawarectangleusingthepaintwithourlineargradient
canvas.drawRect(0,height,width,bitmapWithReflection.getHeight()
+reflectionGap,paint);
returnbitmapWithReflection;
}
androidGallery镜像倒影特效
2011-06-2514:
58
88人阅读
评论(0)
收藏
举报
本文档将介绍在android上如何实现一个倒影效果的Gallery。
代码如下:
MirrorView.java是我自己实现的一个类,继承自Gallery类。
重写了getChildStaticTransformation()方法.
packagecom.demo;
importandroid.content.Context;
importandroid.graphics.Camera;
importandroid.graphics.Matrix;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.view.animation.Transformation;
importandroid.widget.Gallery;
importandroid.widget.ImageView;
publicclassMirrorViewextendsGallery{
privateCameramCamera=newCamera();
//是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等
privateintmMaxRotationAngle=60;
//是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度
privateintmMaxZoom=-380;
//是图片在z轴平移的距离,视觉上看起来就是放大缩小的效果.
privateintmCoveflowCenter;
privatebooleanmAlphaMode=true;
privatebooleanmCircleMode=false;
publicMirrorView(Contextcontext){
super(context);
this.setStaticTransformationsEnabled(true);
}
publicMirrorView(Contextcontext,AttributeSetattrs){
super(context,attrs);
publicMirrorView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
publicintgetMaxRotationAngle(){
returnmMaxRotationAngle;
publicvoidsetMaxRotationAngle(intmaxRotationAngle){
mMaxRotationAngle=maxRotationAngle;
publicbooleangetCircleMode(){
returnmCircleMode;
publicvoidsetCircleMode(booleanisCircle){
mCircleMode=isCircle;
publicbooleangetAlphaMode(){
returnmAlphaMode;
publicvoidsetAlphaMode(booleanisAlpha){
mAlphaMode=isAlpha;
publicintgetMaxZoom(){
returnmMaxZoom;
publicvoidsetMaxZoom(intmaxZoom){
mMaxZoom=maxZoom;
privateintgetCenterOfCoverflow(){
return(getWidth()-getPaddingLeft()-getPaddingRight())/2+getPaddingLeft();
//得到子对象的中线
privatestaticintgetCenterOfView(Viewview){
returnview.getLeft()+view.getWidth()/2;
protectedbooleangetChildStaticTransformation(Viewchild,Transformationt){
finalintchildCenter=getCenterOfView(child);
finalintchildWidth=child.getWidth();
introtationAngle=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);
returntrue;
/**
*Thisiscalledduringlayoutwhenthesizeofthisviewhaschanged.If
*youwerejustaddedtotheviewhierarchy,you'
recalledwiththeold
*valuesof0.
*
*@paramw
Currentwidthofthisview.
*@paramh
Currentheightofthisview.
*@paramoldw
Oldwidthofthisview.
*@paramoldh
Oldheightofthisview.
*/
protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
mCoveflowCenter=getCenterOfCoverflow();
super.onSizeChanged(w,h,oldw,oldh);
*TransformtheImageBitmapbytheAnglepassed
*@paramimageView
ImageViewtheImageViewwhosebitmapwewanttorotate
*@paramt
transformation
*@paramrotationAngle
theAnglebywhichtorotatetheBitmap
privatevoidtransformImageBitmap(ImageViewchild,Transformationt,introtationAngle){
mCamera.save();
finalMatriximageMatrix=t.getMatrix();
finalintimageHeight=child.getLayoutParams().height;
finalintimageWidth=child.getLayoutParams().width;
finalintrotation=Math.abs(rotationAngle);
mCamera.translate(0.0f,0.0f,100.0f);
//Astheangleoftheviewgetsless,zoomin
if(rotation<
=mMaxRotationAngle){
floatzoomAmount=(float)(mMaxZoom+(rotation*1.5));
mCamera.translate(0.0f,0.0f,zoomAmount);
if(mCircleMode){
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类,用来实现图片的反转以及蒙板效果。
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.LinearGradient;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Bitmap.Config;
importandroid.graphics.PorterDuff.Mode;
importandroid.graphics.Shader.TileMode;
importandroid.graphics.drawable.BitmapDrawable;
publicclassReflectionImageextendsImageView{
//是否为Reflection模式
privatebooleanmReflectionMode=true;
publicReflectionImage(Contextcontext){
}
publicReflectionImage(Contextcontext,AttributeSetattrs){
//取得原始图片的bitmap并重画
BitmaporiginalImage=((BitmapDrawable)this.getDrawable()).getBitmap();
DoReflection(originalImage);
publicReflectionImage(Contextcontext,AttributeSetattrs,
intdefStyle){
publicvoidsetReflectionMode(booleanisRef){
mReflectionMode=isRef;
publicbooleangetReflectionMode(){
returnmReflectionMode;
//偷懒了,只重写了setImageResource,和构造函数里面干了同样的事情
@Override
publicvoidsetImageResource(intresId){
BitmaporiginalImage=BitmapFactory.decodeResource(
getResources(),resId);
//super.setImageResource(resId);
privatevoidDoReflection(BitmaporiginalImage){
finalintreflectionGap=4;
//原始图片和反射图片中间的间距
intwidth=originalImage.getWidth();
intheight=originalImage.getHeight();
//反转
Matrixmatrix=newMatrix();
matrix.preScale(1,-1);
//reflectionImage就是下面透明的那部分,可以设置它的高度为原始的3/4,这样效果会更好些
BitmapreflectionImage=Bitmap.createBitmap(originalImage,0,0,width,height,matrix,false);
//创建一个新的bitmap,高度为原来的两倍
BitmapbitmapWithReflection=Bitmap.createBitmap(width,(height+height),Config.ARGB_8888);
CanvascanvasRef=newCanvas(bitmapWithReflection);
//先画原始的图片
canvasRef.drawBitmap(originalImage,0,0,null);
//画间距
PaintdeafaultPaint=newPaint();
canvasRef.drawRect(0,height,width,height+reflectionGap,deafaultPaint);
//画被反转以后的图片
canvasRef.drawBitmap(reflectionImage,0,height+reflectionGap,null);
//创建一个渐变的蒙版放在下面被反转的图片上面
Paintpaint=newPaint();
LinearGradientshader=newLinearGradient(0,
originalImage.getHeight(),0,bitmapWithReflection.getHeight()+reflectionGap,0x80ffffff,0x00ffffff,TileMode.CLAMP);