├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── dictionaries │ └── tongfu.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── screenshots │ ├── 123.gif │ ├── 1234.gif │ ├── color.png │ ├── color2.png │ ├── haha.gif │ ├── hehe.gif │ ├── view1.png │ ├── view10.png │ ├── view2.png │ ├── view3.png │ ├── view4.png │ ├── view5.png │ ├── view6.png │ ├── view7.png │ ├── view8.png │ └── view9.png └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── kk │ │ └── tongfu │ │ └── douyinloading │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── kk │ │ │ └── tongfu │ │ │ └── douyinloading │ │ │ ├── MainActivity.java │ │ │ └── view │ │ │ └── LoadingView.java │ └── res │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── kk │ └── tongfu │ └── douyinloading │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/dictionaries/tongfu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 21 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 仿抖音加载进度条 2 | 3 | 最近在一个android学习交流群中看到有同学问仿抖音进度条加载怎么实现,刚好自己在学习自定义view,就尝试实现了一下,效果如下图,在此记录一下实现过程,如有不足,欢迎指正交流。 4 | 5 | 6 |
7 | 8 |
9 | 10 | ### 效果分析 11 | 首先我们来分析这个进度条的效果,可以看到刚开始进度条是固定长度的深灰色的,然后慢慢向两端延伸并且颜色渐变成透明色,到达屏幕两边时完全透明,如此循环,在这个过程中有两个要点: 12 | 13 | >* 1 长度向外延伸,由中间向两侧延伸,至屏幕边界 14 | >* 2 颜色渐变,深灰色>透明色 15 | 16 | 首先我们来分析长度向外延伸至屏幕边界,首先我们可以获取到整个view的高度,我们可以在view中心位置上画一个固定初始宽度和高度的灰色的进度条(其实就是一条灰色的线),然后定时向两边增加固定的宽度,然后再次重新绘制这个view,直到进度条的宽度大于view的宽度时,我们再次将进度条的宽度设置为初始宽度。接着执行此步骤 17 | 18 | 19 | 接着我们来分析颜色渐变,项目中我们常用的颜色表示方式有两种,一种是不带透明度,一种是带透明度,如下所示 20 | 21 | 22 | 23 | 如果我们直接改变颜色的值,由于颜色值不易控制,改变颜色值的过程中很容易出现我们不需要的颜色,所以我们可以换一种思路,通过改变颜色的透明度来达到我们的效果,我们将最开始的深灰色,通过不断加大它的透明度,最后使其变为透明色不可见,颜色的透明度由两位16进制数来表示,为ff时表明不透明,为00时表示全透明,如下图 24 | 25 | 26 | 27 | 我们知道两位16进制数表示的范围0-255,在进度条为初始宽度时设置透明度为255,随着进度条宽度逐渐增大,我们将透明度由255逐渐减小,最后变为接近全透明0,好了,废话不多说,开始上代码。 28 | 29 | --- 30 | 31 | 首先我们创建一个类LoadingView继承View类,并重写它的构造方法,最后一个构造方法我们一般用不到,删除就行了,让前两个构造方法调用第三个构造方法,以保证每次都会调用到第三个构造方法,如下图 32 | 33 | 34 | 35 | 接下来我们给view设置一个默认最小宽度和高度,以及进度条的初始宽度,初始颜色 并创建画笔 36 | 37 | 38 | 39 | 然后就是自定义view三部曲了,首先我们重写onMeasure方法来测量view的宽高,具体实现如下图,(每一步的说明已经写的很详细不再叙述,如果对MeasureSpec不是很了解的同学可以参考此文[MeasureSpec详解](https://segmentfault.com/a/1190000007948959)) 40 | 41 | 42 | 43 | onSizeChanged方法会在view的大小发生改变时调用,所以获取完view的宽高后我们重写onSizeChanged方法将宽高赋值给全局变量,并设置画笔高度为view的高度,如下图 44 | 45 | 46 | 47 | 接着我们重写onDraw方法开始view的绘制,具体过程如下,首先保证进度条宽度不能大于view的宽度,然后根据进度条宽度占比来计算颜色的透明度,最后给画笔设置为此颜色,然后用canvas来画一条从view的中心点开始向左右画一条宽度为进度条二分之一的线,(关于canvas的使用方法不了解的同学可以参考此文[Canvas使用详解](http://www.gcssloop.com/customview/Canvas_BasicGraphics)) 如下图 48 | 49 | 50 | 51 | 最后调用 invalidate()方法来不断刷新重新绘制,由于在重新绘制的过程中进度条宽度不断改变,这样就形成了进度条不断延伸的效果,效果如下 52 | 53 |
54 | 55 |
56 | 57 | 至此基本效果已经实现了,可能有的同学会说你这个最小宽度每次都要去修改,我想修改个颜色,修改个宽度,每次还要到源码中去修改,太麻烦,那么接下来我们就通过自定义属性,将一些经常更改的东西通过属性来提供(关于自定义属性,可以参考[自定义属性](https://blog.csdn.net/liujian8654562/article/details/80389077)) 58 | 59 | --- 60 | 61 | 首先我们在values文件夹下创建attrs.xml文件,然后在其中定义相应的属性 62 | 63 | 64 | 65 | 然后我们在view的构造函数中获取自定义属性值,并赋值给相关常量,并且根据正则表达式来判断值传入的是否正确, 66 | 67 | 68 | 69 | 接下来我们在xml布局中添加这些属性 70 | 71 | 72 | 73 | 然后运行项目,发现我们更改的属性已经生效 74 | 75 |
76 | 77 |
78 | 79 | 接下来我们来更改动画的执行时间,动画执行时间我们使用handler 来定时发送消息,并在接收到消息后再次发送,以达到循环执行的目的,然后对外提供方法更改重绘周期 80 | 81 | 82 | 83 | 我们通过findviewbyid找到此view,并设置间隔时间为100毫秒测试一下效果 84 | 85 |
86 | 87 |
88 | 89 | 至此仿抖音loadingview基本就完成了,android小白,初次写博客,如有不足的地方望多多指教 本人邮箱suntongf@126.com 项目地址[Demo地址](https://github.com/tongfuzz/douyinloading) 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.kk.tongfu.douyinloading" 7 | minSdkVersion 19 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/screenshots/123.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/123.gif -------------------------------------------------------------------------------- /app/screenshots/1234.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/1234.gif -------------------------------------------------------------------------------- /app/screenshots/color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/color.png -------------------------------------------------------------------------------- /app/screenshots/color2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/color2.png -------------------------------------------------------------------------------- /app/screenshots/haha.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/haha.gif -------------------------------------------------------------------------------- /app/screenshots/hehe.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/hehe.gif -------------------------------------------------------------------------------- /app/screenshots/view1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view1.png -------------------------------------------------------------------------------- /app/screenshots/view10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view10.png -------------------------------------------------------------------------------- /app/screenshots/view2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view2.png -------------------------------------------------------------------------------- /app/screenshots/view3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view3.png -------------------------------------------------------------------------------- /app/screenshots/view4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view4.png -------------------------------------------------------------------------------- /app/screenshots/view5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view5.png -------------------------------------------------------------------------------- /app/screenshots/view6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view6.png -------------------------------------------------------------------------------- /app/screenshots/view7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view7.png -------------------------------------------------------------------------------- /app/screenshots/view8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view8.png -------------------------------------------------------------------------------- /app/screenshots/view9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/screenshots/view9.png -------------------------------------------------------------------------------- /app/src/androidTest/java/com/kk/tongfu/douyinloading/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.kk.tongfu.douyinloading; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.kk.tongfu.douyinloading", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/kk/tongfu/douyinloading/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.kk.tongfu.douyinloading; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.kk.tongfu.douyinloading.view.LoadingView; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | private LoadingView mLoadingView; 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | mLoadingView=findViewById(R.id.loadingView); 17 | mLoadingView.setTimePeriod(100); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/kk/tongfu/douyinloading/view/LoadingView.java: -------------------------------------------------------------------------------- 1 | package com.kk.tongfu.douyinloading.view; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.support.annotation.Nullable; 11 | import android.util.AttributeSet; 12 | import android.util.Log; 13 | import android.view.View; 14 | 15 | import com.kk.tongfu.douyinloading.R; 16 | 17 | import java.util.regex.Matcher; 18 | import java.util.regex.Pattern; 19 | 20 | /** 21 | * Created by tongfu 22 | * on 2019/3/9 23 | * Desc: 24 | */ 25 | 26 | public class LoadingView extends View { 27 | 28 | 29 | private int mWidth,mHeight; 30 | private int mDefaultWidth,mDefaultHeight; 31 | private int mProgressWidth; 32 | private int mMinProgressWidth; 33 | private Paint mPaint; 34 | private String mColor; 35 | private Handler mHandler; 36 | private int mTimePeriod=20; 37 | 38 | public LoadingView(Context context) { 39 | this(context,null); 40 | } 41 | 42 | public LoadingView(Context context, @Nullable AttributeSet attrs) { 43 | this(context, attrs,0); 44 | } 45 | 46 | public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 47 | super(context, attrs, defStyleAttr); 48 | 49 | //获取颜色值和最小宽高度,以及进度条最小宽度 50 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView); 51 | String color=typedArray.getString(R.styleable.LoadingView_progressColor); 52 | mDefaultWidth = (int) typedArray.getDimension(R.styleable.LoadingView_minWidth, 600); 53 | mDefaultHeight = (int) typedArray.getDimension(R.styleable.LoadingView_minHeight, 5); 54 | mMinProgressWidth = (int) typedArray.getDimension(R.styleable.LoadingView_minProgressWidth, 100); 55 | mProgressWidth=mMinProgressWidth; 56 | 57 | //根据正则表达式来判断颜色格式是否正确 58 | String regularStr="^#[A-Fa-f0-9]{6}"; 59 | Pattern compile = Pattern.compile(regularStr); 60 | if(color==null){ 61 | mColor="#808080"; 62 | }else{ 63 | Matcher matcher = compile.matcher(color); 64 | if(matcher.matches()){//如果颜色格式正确 65 | mColor=color; 66 | }else{ 67 | //如果颜色格式不正确 68 | throw new IllegalArgumentException("wrong color string type!"); 69 | } 70 | } 71 | typedArray.recycle(); 72 | 73 | 74 | 75 | /* //设置view的默认最小宽度 76 | mDefaultWidth=600; 77 | //设置view的默认最小高度 78 | mDefaultHeight=5; 79 | //设置进度条的初始宽度,这个宽度不能大于view的最小宽度,否则进度条不能向两边延伸 80 | mProgressWidth=100; 81 | //设置默认初始颜色 82 | mColor="#808080";*/ 83 | 84 | mPaint=new Paint(); 85 | //设置虎逼模式为填充带边框 86 | mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 87 | //设置抗锯齿 88 | mPaint.setAntiAlias(true); 89 | 90 | mHandler=new Handler(){ 91 | @Override 92 | public void handleMessage(Message msg) { 93 | super.handleMessage(msg); 94 | invalidate(); 95 | this.sendEmptyMessageDelayed(1,mTimePeriod); 96 | } 97 | }; 98 | mHandler.sendEmptyMessageDelayed(1,mTimePeriod); 99 | 100 | 101 | } 102 | 103 | /** 104 | * 设置重绘的周期 105 | * @param timePeriod 106 | */ 107 | public void setTimePeriod(int timePeriod){ 108 | if(mTimePeriod>0) { 109 | this.mTimePeriod = timePeriod; 110 | } 111 | } 112 | 113 | 114 | @Override 115 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 116 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 117 | //通过widthMeasureSpec,heightMeasureSpec 来获取view的测量模式和宽高 118 | int width = getValue(widthMeasureSpec,true); 119 | int height = getValue(heightMeasureSpec,false); 120 | 121 | //此方法用来设置设置View的具体宽高 122 | setMeasuredDimension(width, height); 123 | } 124 | 125 | /** 126 | * 获取view的宽高值 127 | * @param measureSpec 128 | * @param isWidth 是否是测量宽度 129 | * @return 130 | */ 131 | private int getValue(int measureSpec,boolean isWidth) { 132 | //获取测量模式 133 | int mode = MeasureSpec.getMode(measureSpec); 134 | //获取测量的值 135 | int size = MeasureSpec.getSize(measureSpec); 136 | switch (mode) { 137 | /** 138 | * 如果父控件传递给的MeasureSpec的mode是MeasureSpec.UNSPECIFIED,就说明,父控件对自己没有任何限制,那么尺寸就选择自己需要的尺寸size 139 | 140 | 如果父控件传递给的MeasureSpec的mode是MeasureSpec.EXACTLY,就说明父控件有明确的要求,希望自己能用measureSpec中的尺寸,这时就推荐使用MeasureSpec.getSize(measureSpec) 141 | 142 | 如果父控件传递给的MeasureSpec的mode是MeasureSpec.AT_MOST,就说明父控件希望自己不要超出MeasureSpec.getSize(measureSpec),如果超出了,就选择MeasureSpec.getSize(measureSpec),否则用自己想要的尺寸就行了 143 | */ 144 | case MeasureSpec.EXACTLY: 145 | //子view的大小已经被限定死,我们只能使用其固定大小 146 | return size; 147 | case MeasureSpec.AT_MOST: 148 | //父控件认为子view的大小不能超过size的值,那么我们就取size和默认值之间的最小值 149 | return Math.min(isWidth?mDefaultWidth:mDefaultHeight, size); 150 | case MeasureSpec.UNSPECIFIED: 151 | //父view不限定子view的大小,我们将其值设置为默认值 152 | return isWidth?mDefaultWidth:mDefaultHeight; 153 | default: 154 | return isWidth?mDefaultWidth:mDefaultHeight; 155 | } 156 | } 157 | 158 | @Override 159 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 160 | super.onSizeChanged(w, h, oldw, oldh); 161 | mWidth=w; 162 | mHeight=h; 163 | if(mWidth255){ 189 | //由于mProgressWidth有可能大于view的宽度,要保证颜色值不能大于255 190 | currentColorValue=255; 191 | } 192 | if(currentColorValue<30){ 193 | //此处是为了限制让其不成为全透明,如果设置为全透明,在最后阶段进度宽度渐变观察不到 194 | currentColorValue=30; 195 | } 196 | //将透明度转换为16进制 197 | String s = Integer.toHexString(currentColorValue); 198 | //拼接颜色字符串并转化为颜色值 199 | int color = Color.parseColor("#" + s + mColor.substring(1,mColor.length())); 200 | //给画笔设置颜色 201 | mPaint.setColor(color); 202 | //使用canvas来画进度条(确实就是画一条线) 203 | canvas.drawLine(mWidth/2-mProgressWidth/2,mDefaultHeight/2,mWidth/2+mProgressWidth/2,mDefaultHeight/2,mPaint); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #ffffff 7 | 8 | #66000000 9 | 10 | #FFFFF0 11 | #FFFFE0 12 | #FFFF00 13 | #FFFAFA 14 | #FFFAF0 15 | #FFFACD 16 | #FFF8DC 17 | #FFF5EE 18 | #FFF0F5 19 | #FFEFD5 20 | #FFEBCD 21 | #FFE4E1 22 | #FFE4C4 23 | #FFE4B5 24 | #FFDEAD 25 | #FFDAB9 26 | #FFD700 27 | #FFC0CB 28 | #FFB6C1 29 | #FFA500 30 | #FFA07A 31 | #FF8C00 32 | #FF7F50 33 | #FF69B4 34 | #FF6347 35 | #FF4500 36 | #FF1493 37 | #FF00FF 38 | #FF00FF 39 | #FF0000 40 | #FDF5E6 41 | #FAFAD2 42 | #FAF0E6 43 | #FAEBD7 44 | #FA8072 45 | #F8F8FF 46 | #F5FFFA 47 | #F5F5F5 48 | #F5F5DC 49 | #F5DEB3 50 | #F4A460 51 | #F0FFFF 52 | #F0FFF0 53 | #F0F8FF 54 | #F0E68C 55 | #F08080 56 | #EEE8AA 57 | #EE82EE 58 | #E9967A 59 | #E6E6FA 60 | #E0FFFF 61 | #DEB887 62 | #DDA0DD 63 | #DCDCDC 64 | #DC143C 65 | #DB7093 66 | #DAA520 67 | #DA70D6 68 | #D8BFD8 69 | #D3D3D3 70 | #D3D3D3 71 | #D2B48C 72 | #D2691E 73 | #CD853F 74 | #CD5C5C 75 | #C71585 76 | #C0C0C0 77 | #BDB76B 78 | #BC8F8F 79 | #BA55D3 80 | #B8860B 81 | #B22222 82 | #B0E0E6 83 | #B0C4DE 84 | #AFEEEE 85 | #ADFF2F 86 | #ADD8E6 87 | #A9A9A9 88 | #A9A9A9 89 | #A52A2A 90 | #A0522D 91 | #9932CC 92 | #98FB98 93 | #9400D3 94 | #9370DB 95 | #90EE90 96 | #8FBC8F 97 | #8B4513 98 | #8B008B 99 | #8B0000 100 | #8A2BE2 101 | #87CEFA 102 | #87CEEB 103 | #808080 104 | #808000 105 | #800080 106 | #800000 107 | #7FFFD4 108 | #7FFF00 109 | #7CFC00 110 | #7B68EE 111 | #778899 112 | #778899 113 | #708090 114 | #708090 115 | #6B8E23 116 | #6A5ACD 117 | #696969 118 | #696969 119 | #66CDAA 120 | #6495ED 121 | #5F9EA0 122 | #556B2F 123 | #4B0082 124 | #48D1CC 125 | #483D8B 126 | #4682B4 127 | #4169E1 128 | #40E0D0 129 | #3CB371 130 | #32CD32 131 | #2F4F4F 132 | #2F4F4F 133 | #2E8B57 134 | #228B22 135 | #20B2AA 136 | #1E90FF 137 | #191970 138 | #00FFFF 139 | #00FFFF 140 | #00FF7F 141 | #00FF00 142 | #00FA9A 143 | #00CED1 144 | #00BFFF 145 | #008B8B 146 | #008080 147 | #008000 148 | #006400 149 | #0000FF 150 | #0000CD 151 | #00008B 152 | #000080 153 | #000000 154 | 155 | #808080 156 | #ff808080 157 | #00808080 158 | 159 | 160 | #E3E3E3 161 | #f4f5fa 162 | @android:color/transparent 163 | 164 | #FFE1E8EB 165 | 166 | #fff 167 | #f00 168 | #FFFF00 169 | 170 | #cc363636 171 | 172 | #00000000 173 | #b0000000 174 | #212121 175 | #cccccc 176 | 177 | 178 | #252B37 179 | #FF3B30 180 | #A6A6A6 181 | #077DFF 182 | #838383 183 | #E6F3FF 184 | 185 | 186 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | douyinloading 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/kk/tongfu/douyinloading/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.kk.tongfu.douyinloading; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.2' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongfuzz/douyinloading/1fb0febebc236f66a1b883bfed0433d9c3270d93/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Mar 09 10:54:36 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------