GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 新增线性loading view · loosaSH/AndroidCustomView@1f9c93a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main
    • java/com/allen/androidcustomview/widget
      • LoadingLineView.java
    • res/layout
      • activity_pregress_bar.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/com/allen/androidcustomview/widget/LoadingLineView.java‎

Copy file name to clipboard
+264Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
package com.allen.androidcustomview.widget;
2+
3+
import android.animation.ValueAnimator;
4+
import android.content.Context;
5+
import android.graphics.Canvas;
6+
import android.graphics.Paint;
7+
import android.support.annotation.Nullable;
8+
import android.util.AttributeSet;
9+
import android.util.TypedValue;
10+
import android.view.View;
11+
import android.view.animation.LinearInterpolator;
12+
13+
/**
14+
* Created by Allen on 2018/1/31.
15+
* <p>
16+
* 线性加载动画
17+
*/
18+
19+
public class LoadingLineView extends View {
20+
21+
private int mWidth;
22+
private int mHeight;
23+
24+
/**
25+
* 动画起点x坐标
26+
*/
27+
private int centerX;
28+
/**
29+
* 动画起点y坐标
30+
*/
31+
private int centerY;
32+
33+
/**
34+
* 偏移距离
35+
*/
36+
private float dis;
37+
38+
/**
39+
* view真实高度
40+
*/
41+
private int mViewHeight;
42+
43+
/**
44+
* 背景色画笔
45+
*/
46+
private Paint bgPaint;
47+
/**
48+
* loading画笔
49+
*/
50+
private Paint loadingPaint;
51+
52+
/**
53+
* 画笔宽度(等于vie高度)
54+
*/
55+
private int paintWidth;
56+
57+
/**
58+
* 底色
59+
*/
60+
private int bgColor = 0xFFe1e5e8;
61+
62+
/**
63+
* loading颜色
64+
*/
65+
private int loadingColor = 0xFFf66b12;
66+
67+
68+
/**
69+
* 动画
70+
*/
71+
private ValueAnimator loadingAnimator;
72+
/**
73+
* 动画执行时间
74+
*/
75+
private int duration = 800;
76+
/**
77+
* 动画延时启动时间
78+
*/
79+
private int startDelay = 0;
80+
81+
public LoadingLineView(Context context) {
82+
this(context, null);
83+
}
84+
85+
public LoadingLineView(Context context, @Nullable AttributeSet attrs) {
86+
this(context, attrs, 0);
87+
}
88+
89+
public LoadingLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
90+
super(context, attrs, defStyleAttr);
91+
getAtt(attrs);
92+
init();
93+
}
94+
95+
/**
96+
* 想实现颜色等参数可配置,在这里实现配置即可,笔者就不多写了
97+
*
98+
* @param attrs attrs
99+
*/
100+
private void getAtt(AttributeSet attrs) {
101+
102+
}
103+
104+
private void init() {
105+
paintWidth = dp2px(2);
106+
mViewHeight = paintWidth;
107+
bgPaint = getPaint(paintWidth, bgColor, Paint.Style.FILL);
108+
loadingPaint = getPaint(paintWidth, loadingColor, Paint.Style.FILL);
109+
}
110+
111+
112+
/**
113+
* 统一处理paint
114+
*
115+
* @param strokeWidth 画笔宽度
116+
* @param color 颜色
117+
* @param style 风格
118+
* @return paint
119+
*/
120+
private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
121+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
122+
paint.setStrokeWidth(strokeWidth);
123+
paint.setColor(color);
124+
paint.setAntiAlias(true);
125+
paint.setStyle(style);
126+
return paint;
127+
}
128+
129+
@Override
130+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
131+
super.onSizeChanged(w, h, oldw, oldh);
132+
133+
mWidth = w;
134+
mHeight = h;
135+
136+
centerX = w / 2;
137+
centerY = h / 2;
138+
139+
initLoadingAnimation();
140+
startLoading();
141+
}
142+
143+
144+
@Override
145+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
146+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
147+
int width = MeasureSpec.getSize(widthMeasureSpec);
148+
149+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
150+
int height = MeasureSpec.getSize(heightMeasureSpec);
151+
152+
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
153+
}
154+
155+
/**
156+
* 测量宽度
157+
*
158+
* @param mode
159+
* @param width
160+
* @return
161+
*/
162+
private int measureWidth(int mode, int width) {
163+
switch (mode) {
164+
case MeasureSpec.UNSPECIFIED:
165+
case MeasureSpec.AT_MOST:
166+
break;
167+
case MeasureSpec.EXACTLY:
168+
mWidth = width;
169+
break;
170+
}
171+
return mWidth;
172+
}
173+
174+
/**
175+
* 测量高度
176+
*
177+
* @param mode
178+
* @param height
179+
* @return
180+
*/
181+
private int measureHeight(int mode, int height) {
182+
switch (mode) {
183+
case MeasureSpec.UNSPECIFIED:
184+
case MeasureSpec.AT_MOST:
185+
mHeight = mViewHeight;
186+
break;
187+
case MeasureSpec.EXACTLY:
188+
mHeight = height;
189+
break;
190+
}
191+
return mHeight;
192+
}
193+
194+
@Override
195+
protected void onDraw(Canvas canvas) {
196+
super.onDraw(canvas);
197+
/**
198+
* 画底色
199+
*/
200+
canvas.drawLine(0, centerY, mWidth, centerY, bgPaint);
201+
/**
202+
* loading向左扩散
203+
*/
204+
canvas.drawLine(centerX, centerY, centerX - dis, centerY, loadingPaint);
205+
/**
206+
* loading向右扩散
207+
*/
208+
canvas.drawLine(centerX, centerY, centerX + dis, centerY, loadingPaint);
209+
}
210+
211+
/**
212+
* 初始化动画
213+
*/
214+
private void initLoadingAnimation() {
215+
final float loadingMoveDistance = mWidth / 2;
216+
loadingAnimator = ValueAnimator.ofFloat(0, loadingMoveDistance);
217+
loadingAnimator.setDuration(duration);
218+
loadingAnimator.setStartDelay(startDelay);
219+
loadingAnimator.setRepeatCount(ValueAnimator.INFINITE);
220+
loadingAnimator.setInterpolator(new LinearInterpolator());
221+
loadingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
222+
@Override
223+
public void onAnimationUpdate(ValueAnimator valueAnimator) {
224+
float value = (float) valueAnimator.getAnimatedValue();
225+
dis = value;
226+
if (value <= loadingMoveDistance / 2) {
227+
loadingPaint.setAlpha((int) ((255 * value) * 2 / loadingMoveDistance));
228+
} else {
229+
loadingPaint.setAlpha((int) (255 - (255 * value) * 2 / loadingMoveDistance));
230+
}
231+
invalidate();
232+
}
233+
});
234+
}
235+
236+
/**
237+
* 开启动画
238+
*/
239+
public void startLoading() {
240+
if (loadingAnimator != null) {
241+
loadingAnimator.start();
242+
}
243+
}
244+
245+
/**
246+
* 结束动画
247+
*/
248+
public void stopLoading() {
249+
if (loadingAnimator != null) {
250+
loadingAnimator.cancel();
251+
}
252+
}
253+
254+
255+
/**
256+
* dp 2 px
257+
*
258+
* @param dpVal dp
259+
*/
260+
protected int dp2px(int dpVal) {
261+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
262+
dpVal, getResources().getDisplayMetrics());
263+
}
264+
}
Collapse file

‎app/src/main/res/layout/activity_pregress_bar.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/activity_pregress_bar.xml
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
android:layout_height="wrap_content"
3939
android:layout_margin="20dp" />
4040

41+
<com.allen.androidcustomview.widget.LoadingLineView
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:layout_margin="20dp" />
45+
4146
<Button
4247
android:id="@+id/startAnimationBtn"
4348
android:layout_width="match_parent"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.