android的viewpager 指示文字过渡效果

有android客户端随着viewpager的滑动,它上方的文字出现两种颜色,左边一半是一种,右边一半是一种,其实这是绘制两层文字,上边一层clip的效果,见如下代码:

public class TransitTextView extends TextView {
    public TransitTextView(Context context) {
        this(context,null,0);
    }

    public TransitTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TransitTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        textPaint = getPaint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setAntiAlias(true);
        textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        str = getText().toString();
        if(attrs != null){
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TransitTextView, 0, 0);
            try {
                normalColor = a.getColor(R.styleable.TransitTextView_normalColor,0);
                selectColor = a.getColor(R.styleable.TransitTextView_selectColor, 0);
                endProgress = a.getInteger(R.styleable.TransitTextView_endProgress,1);
            } finally {
                a.recycle();
            }
        }
    }


    public void setStr(String str){
        this.str = str;
        setText(str);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (textPaint != null && !TextUtils.isEmpty(str)) {
            textPaint.setColor(normalColor);
            float start = startProgress > 0 ? 0.0f : endProgress;
            float end = startProgress > 0 ? startProgress : 1.0f;
            canvas.save();
            canvas.clipRect(getWidth() * start, 0, getWidth() * end, getHeight());
            canvas.drawText(str, getWidth() / 2, getHeight() - 6, textPaint);
            canvas.restore();

            textPaint.setColor(selectColor);
            clipRect.set(getWidth() * startProgress, 0, getWidth() * endProgress, getHeight());
            canvas.save();
            canvas.clipRect(clipRect);
            canvas.drawText(str, getWidth() / 2, getHeight()-6, textPaint);
            canvas.restore();
        }
    }


    public void setTransitProgress(float startProgress,float endProgress) {
        this.startProgress = startProgress;
        this.endProgress = endProgress;
        postInvalidate();
    }


    private int normalColor;
    private int selectColor;
    private float startProgress,endProgress = 0.0f;
    private String str;
    private RectF clipRect = new RectF();
    private TextPaint textPaint = null;
    private Context context;
}

在viewpager的pager监听中代码:

private List<TransitTextView> textviews = new ArrayList<TransitTextView>();
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                float f = position + positionOffset;
                //textviews为所有指示文字的集合
                textviews[position].setTransitProgress(f, 1.0f);
                textviews[(position+1)%textviews.length].setTransitProgress(0.0f, f);
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值