2016-07-16 64 views
0

我怎么能实现在TextView的反射效果如下面的一个 enter image description here如何在TextView上实现反射效果?

我尝试以下link1link2,但它的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); 
    } 
} 
+0

为什么人们放弃投票? – Attaullah

回答

1

你可以隐藏你的textView到ImageView并尝试你的东西!

下面是一个方法来做到这一点!

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

谢谢,但对不起@johnrao任何其他方式?我不想创建位图的东西等... – Attaullah