欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现文字倒影效果的方法

程序员文章站 2023-12-15 14:08:40
本文实例讲述了android编程实现文字倒影效果的方法。分享给大家供大家参考,具体如下: 我们所有的view都继承自view类,view类里有个方法叫ondraw()....

本文实例讲述了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, 0,
        height / 3, width, height / 3, matrix, false);
    //draw the shadow
    canvas.drawbitmap(reflectionimage, 0, 8 * height / 12, null);
    //process shadow bitmap to make it shadow like
    paint paint = new paint();
    lineargradient shader = new lineargradient(0, 8 * height / 12, 0,
        height, 0x70ffffff, 0x00ffffff, tilemode.clamp);
    paint.setshader(shader);
    paint.setxfermode(new porterduffxfermode(mode.dst_in));
    canvas.drawrect(0, 8 * height / 12, width, height, paint);
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: