概述
本文实例为大家分享了android自定义view加载文字变色效果的具体代码,供大家参考,具体内容如下
不分析了,很简单,直接贴代码:
package com.loading; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Handler; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; /** * Created by zhouguizhijxhz on 2018/5/25. */ public class LoadingView extends View{ private Paint loadPaint; private Paint paint; private String text = "今日头条"; private float percent; private Handler handler = new Handler(); public LoadingView(Context context) { this(context,null); } public LoadingView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); setMeasuredDimension(bounds.width(),bounds.height()); } private void initPaint() { paint = new Paint(); paint.setColor(Color.parseColor("#999999")); paint.setTextSize(60); loadPaint = new Paint(); loadPaint.setStyle(Paint.Style.FILL); loadPaint.setColor(0x70ffffff); } @Override protected void onDraw(Canvas canvas) { drawText(canvas); drawLine(canvas); } private void drawLine(Canvas canvas) { if(null==canvas){ return; } canvas.save(); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); Rect rect = new Rect(0, 0, (int) (bounds.width()*percent), bounds.height()); canvas.clipRect(rect); canvas.drawRect(rect,loadPaint); canvas.restore(); handler.postDelayed(new Runnable() { @Override public void run() { if(percent>=1.0){ percent=0; }else{ percent+=0.05f; } postInvalidate(); } },200); } private void drawText(Canvas canvas) { if(null==canvas){ return; } Paint.FontMetricsInt fm = paint.getFontMetricsInt(); canvas.drawText(text, getWidth() / 2 - paint.measureText(text) / 2, getHeight() / 2 - (fm.bottom + fm.top) / 2, paint); } @Override protected void onWindowVisibilityChanged(int visibility) { if(visibility==View.VISIBLE){ percent+=0.05f; invalidate(); } } }
效果:
如果要达到头条那么好看,叫你们美工给你们2个颜色值就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是飞快冥王星为你收集整理的android自定义view仿今日头条加载文字变色效果的全部内容,希望文章能够帮你解决android自定义view仿今日头条加载文字变色效果所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复