欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android自定義view仿今日頭條加載文字變色效果

 更新時(shí)間:2018年07月17日 10:31:38   作者:zhoushenxian  
這篇文章主要為大家詳細(xì)介紹了android自定義view仿今日頭條加載文字變色效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android自定義view加載文字變色效果的具體代碼,供大家參考,具體內(nèi)容如下

不分析了,很簡(jiǎn)單,直接貼代碼:

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

效果:

如果要達(dá)到頭條那么好看,叫你們美工給你們2個(gè)顏色值就可以了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論