textview 文字倒影

package publicclass;

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.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * Created by ty-deng on 17/5/6.
 */

public class ReflectionTextView extends TextView {

    private Matrix mMatrix;
    private Paint mPaint;

    public ReflectionTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mMatrix = new Matrix();
        mMatrix.preScale(1, -1);
        //这句是关闭硬件加速,启用软件加速,如果报相关错误可以尝试注释这句代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(),
                (int) (getMeasuredHeight() * 1.67));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        setDrawingCacheEnabled(true);
        Bitmap originalImage = Bitmap.createBitmap(getDrawingCache());
        int cutHeight = height/4;
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/6, width, height/4, mMatrix, false);
        canvas.drawBitmap(reflectionImage, 0, height/2f+10, null);
        if(mPaint == null)  {
            mPaint = new Paint();
            LinearGradient shader = new LinearGradient(0, height/2f, 0,
                    height - cutHeight, 0x7fffffff, 0x0fffffff, TileMode.CLAMP);
            mPaint.setShader(shader);
            mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        }
        canvas.drawRect(0, height/2f-2, width, height, mPaint);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start,
                                 int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        buildDrawingCache();
        postInvalidate();
        //每次更新TextView后遗留上次的残影,所以在这里每次刷新TextView后清楚DrawingCache
        destroyDrawingCache();
    }
}
/*
* 主要功能在onDraw方法里面,先调用setDrawingCacheEnabled(true);
* 让cache可用,然后通过cache创建一个和原图片一样的图像,通过mMatrix.preScale(1, -1);
* 使图片倒过来,调用Bitmap.createBitmap(originalImage, 0, height/5, width, height/2, mMatrix, false);
* 创建一个倒过来的图像,调用canvas.drawBitmap(reflectionImage, 0, height/3f, null);
* 把倒过来的图像画到画布上。通过调用LinearGradient shader = new LinearGradient(0, height/2, 0,
height, 0x7fffffff, 0x0fffffff, TileMode.CLAMP);
mPaint.setShader(shader);
mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));使倒影的图像的颜色渐变,由灰色变为黑色。
时间走动时调用buildDrawingCache();
postInvalidate();
让倒影从新绘制。
调用setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredHeight()*1.67));设置图像的宽度和高度。
* */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值