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