我怎样才能在TextView上实现反射效果?

时间:2016-07-16 10:59:07

标签: android android-canvas textview

如何在TextView上实现反射效果,如下所示 enter image description here

我尝试关注 link1 link2 ,但它适用于ImageView如何申请TextView。

public class ReflectedImageView extends ImageView {
    private Paint mPaint;

    public ReflectedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Setup the paint.
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStrokeWidth(0.0f);

        // Destination (DST) is drawn by the parent, and should be retained.
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        // It's recommended not to create a Shader in the basic layout calls.
        // Linear gradient from Transparent to Black, from top to bottom.
        // Note: We rotated the image using a transformation.
        // Hence the colors will be opposite.
        LinearGradient gradient = new LinearGradient(0, 0, 0, bottom - top, 
                                                     0x0, 0xFF000000,
                                                     Shader.TileMode.CLAMP);

        // Set the gradient as the shader.
        mPaint.setShader(gradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Save the canvas. All PorterDuff operations should be done in a offscreen bitmap.
        int count = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null,
                                     Canvas.MATRIX_SAVE_FLAG |
                                     Canvas.CLIP_SAVE_FLAG |
                                     Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                                     Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                                     Canvas.CLIP_TO_LAYER_SAVE_FLAG);



        // Do a default draw.
        super.onDraw(canvas);

        // Draw the paint (that has a shader set), on top of the image
        // drawn by the parent (ImageView).
        // Note: This works only on ICS. For pre-ICS phones, create a bitmap and
        // draw on it, like mentioned in CanvasDelegate linked below.
        canvas.drawPaint(mPaint);

        // Restore the canvas.
        canvas.restoreToCount(count);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将textView转换为ImageView并尝试使用它们!

这是一种方法!

TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());