Android编程实现文字倒影效果的方法
本文实例讲述了Android编程实现文字倒影效果的方法。分享给大家供大家参考,具体如下:
我们所有的view都继承自View类,View类里有个方法叫ondraw().即,我们看到的界面都是画出来的,所以我们可以重写ondraw()方法。
既然知道了这点就好办了,还有个难点就是,我们的倒影也是画出来的,那我们从哪去取原始图片呢?熟悉View的童鞋都知道Cache这个东西,不错,就是通过Cache我们取到了原始图片。
放源码了。,感谢期待。这个只是个demo,并不完善哈,布局什么的还需要调整下,或者我什么时候有空再进行一次封装,就可以直接从布局文件中任意调整了。
布局文件中增加如下代码
类代码如下:
/*
*Copyright(C)2011TCLicensedundertheApacheLicense,Version2.0(the"License");
*youmaynotusethisfileexceptincompliancewiththeLicense.YoumayobtainacopyoftheLicenseat
*http://www.apache.org/licenses/LICENSE-2.0Unlessrequiredbyapplicablelaworagreedtoinwriting,software
*distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANYKIND,
*eitherexpressorimplied.SeetheLicenseforthespecificlanguagegoverningpermissionsandlimitationsunderthe
*License.ThiscodeisbaseontheAndroidTextViewandwasCreatedbytitanchen2000@yahoo.com.cn
*
*@authorTC
*/
packagecom.tc.reflect;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.LinearGradient;
importandroid.graphics.Matrix;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.PorterDuff.Mode;
importandroid.graphics.Shader.TileMode;
importandroid.util.AttributeSet;
importandroid.widget.TextView;
publicclassReflectTextViewextendsTextView{
publicReflectTextView(Contextcontext){
super(context);
}
publicReflectTextView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
}
publicReflectTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
}
@Override
protectedvoidonDraw(Canvascanvas){
//drawthetextfromlayout()
super.onDraw(canvas);
intheight=getHeight();
intwidth=getWidth();
//maketheshadowreverseofY
Matrixmatrix=newMatrix();
matrix.preScale(1,-1);
//makesureyoucanusethecache
setDrawingCacheEnabled(true);
//createbitmapfromcache,thisisthemostimportantofthis
BitmaporiginalImage=Bitmap.createBitmap(getDrawingCache());
//createtheshadow
BitmapreflectionImage=Bitmap.createBitmap(originalImage,0,
height/3,width,height/3,matrix,false);
//drawtheshadow
canvas.drawBitmap(reflectionImage,0,8*height/12,null);
//processshadowbitmaptomakeitshadowlike
Paintpaint=newPaint();
LinearGradientshader=newLinearGradient(0,8*height/12,0,
height,0x70ffffff,0x00ffffff,TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0,8*height/12,width,height,paint);
}
}
希望本文所述对大家Android程序设计有所帮助。