2016-07-16 61 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