如何使用淡化文本制作自定义TextView?

时间:2012-11-29 06:52:20

标签: android textview android-custom-view marquee

我想制作一个带有淡化文字的自定义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);

1 个答案:

答案 0 :(得分:0)

您可以使用xml制作淡入淡出动画,如下所示 (anim / alpha.xml)将其放入 res / anim / alpha.xml

 <alpha xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromAlpha="0.0"
  android:toAlpha="1.0"
  android:duration="3000" />

在java文件中

   private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    TextView l=(TextView) findViewById(R.id.your_textview_id);
    l.clearAnimation();
    l.startAnimation(anim);
   }

有关详细信息,请查看此animated splash screen