android自定義view仿今日頭條加載文字變色效果
更新時間:2018年07月17日 10:31:38 作者:zhoushenxian
這篇文章主要為大家詳細介紹了android自定義view仿今日頭條加載文字變色效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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();
}
}
}
效果:

如果要達到頭條那么好看,叫你們美工給你們2個顏色值就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android仿今日頭條頂部導航欄效果的實例代碼
- Android仿今日頭條多個fragment懶加載的實現(xiàn)
- Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能
- Android studio導入項目的方法詳解(簡單快速)
- Android 仿今日頭條簡單的刷新效果實例代碼
- Android仿今日頭條APP實現(xiàn)下拉導航選擇菜單效果
- Android應用中仿今日頭條App制作ViewPager指示器
- Android實現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個view中
- Android仿今日頭條滑動頁面導航效果
- Android實現(xiàn)今日頭條訂閱頻道效果
相關文章
MUI進行APP混合開發(fā)實現(xiàn)下拉刷新和上拉加載
給大家分析一下在用MUI進行APP混合開發(fā)的時候,如何用代碼實現(xiàn)下拉刷新和上拉加載這個普遍應用的功能。2017-11-11
Android自定義viewGroup實現(xiàn)點擊動畫效果
這篇文章主要介紹了Android自定義viewGroup實現(xiàn)點擊動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

