我想定制一个TextView
带有渐变文本的自定义,比如选框的渐变效果。这是我的代码:它不起作用。
public class FadeTextView extends TextView {
private Paint mShaderPaint;
private Matrix mMatrix;
private LinearGradient mShader;
private RectF mFadingRect;
public FadeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public FadeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FadeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mShaderPaint = new Paint();
mShader = new LinearGradient(getLeft(), getTop(), getRight(), getBottom(), 0x7FFFFFFF, 0x00FFFFFF,
TileMode.CLAMP);
mShaderPaint.setShader(mShader);
mShaderPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(getLeft(), getTop(), getRight(), getBottom(), mShaderPaint);
}
}
即使我试图只画一个文字,它也失败了:
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(16);
canvas.drawText("test", getLeft(), getTop(), paint);