本文实例讲述了AndroID编程实现文字倒影效果的方法。分享给大家供大家参考,具体如下:
我们所有的vIEw都继承自VIEw类,VIEw类里有个方法叫ondraw(). 即,我们看到的界面都是画出来的,所以我们可以重写ondraw()方法。
既然知道了这点就好办了,还有个难点就是,我们的倒影也是画出来的,那我们从哪去取原始图片呢?熟悉VIEw的童鞋都知道Cache这个东西,不错,就是通过Cache我们取到了原始图片。
放源码了。,感谢期待。这个只是个demo,并不完善哈,布局什么的还需要调整下,或者我什么时候有空再进行一次封装,就可以直接从布局文件中任意调整了。
布局文件中增加如下代码
<com.tc.reflect.ReflectTextVIEw androID:layout_margintop="20dp" androID:ID="@+ID/test_reflect" androID:layout_wIDth="fill_parent" androID:layout_height="50dp" androID:textSize="25dp" androID:textcolor="#ff0000" androID:textStyle="bold" androID:gravity="top|center_horizontal" androID:text="不会飞翔的翅膀 XP.C" /> <com.tc.reflect.ReflectTextVIEw androID:layout_margintop="20dp" androID:ID="@+ID/test_reflect" androID:layout_wIDth="fill_parent" androID:layout_height="50dp" androID:textSize="25dp" androID:textcolor="#a0a0a0" androID:textStyle="italic" androID:gravity="top|center_horizontal" androID:text="titanchen2000@yahoo.com.cn" />
类代码如下:
/* * copyright (C) 2011 TC licensed under the Apache license,Version 2.0 (the "license"); * you may not use this file except in compliance with the license. You may obtain a copy of the license at * http://www.apache.org/licenses/liCENSE-2.0 Unless required by applicable law or agreed to in writing,software * distributed under the license is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,* either express or implIEd. See the license for the specific language governing permissions and limitations under the * license. This code is base on the AndroID TextVIEw and was Created by titanchen2000@yahoo.com.cn * * @author TC */package com.tc.reflect;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.Canvas;import androID.graphics.linearGradIEnt;import androID.graphics.Matrix;import androID.graphics.Paint;import androID.graphics.PorterDuffXfermode;import androID.graphics.PorterDuff.Mode;import androID.graphics.Shader.TileMode;import androID.util.AttributeSet;import androID.Widget.TextVIEw;public class ReflectTextVIEw extends TextVIEw { public ReflectTextVIEw(Context context) { super(context); } public ReflectTextVIEw(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); } public ReflectTextVIEw(Context context,AttributeSet attrs) { super(context,attrs); } @OverrIDe protected voID onDraw(Canvas canvas) { //draw the text from layout() super.onDraw(canvas); int height = getHeight(); int wIDth = getWIDth(); //make the shadow reverse of Y Matrix matrix = new Matrix(); matrix.preScale(1,-1); //make sure you can use the cache setDrawingCacheEnabled(true); //create bitmap from cache,this is the most important of this Bitmap originalimage = Bitmap.createBitmap(getDrawingCache()); //create the shadow Bitmap reflectionImage = Bitmap.createBitmap(originalimage,height / 3,wIDth,matrix,false); //draw the shadow canvas.drawBitmap(reflectionImage,8 * height / 12,null); //process shadow bitmap to make it shadow like Paint paint = new Paint(); linearGradIEnt shader = new linearGradIEnt(0,height,0x70ffffff,0x00ffffff,TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0,paint); }}
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现文字倒影效果的方法全部内容,希望文章能够帮你解决Android编程实现文字倒影效果的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)