您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“android如何自定義view仿今日頭條加載文字變色效果”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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(); } } }
效果:
以上是“android如何自定義view仿今日頭條加載文字變色效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。