2

我想定制一个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);
4

1 回答 1

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);
   }

有关更多详细信息,您可以查看此动画启动画面

于 2012-11-29T07:03:23.083 回答