Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit b055d59

Browse files
committed
add change color programmatically
1 parent 2c59a97 commit b055d59

8 files changed

Lines changed: 64 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Usage
2323
gradle
2424

2525
```
26-
compile 'com.victor:lib:1.0.2'
26+
compile 'com.victor:lib:1.0.3'
2727
```
2828

2929

example/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<attr name="shadow_position" format="integer" />
77
<attr name="loading_speed" format="integer"/>
88
</declare-styleable>
9+
10+
<declare-styleable name="CradleBall">
11+
<attr name="cradle_ball_color" format="color" />
12+
</declare-styleable>
913
</resources>

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 11
1010
targetSdkVersion 23
1111
versionCode 1
12-
versionName "1.0.2"
12+
versionName "1.0.3"
1313
}
1414
buildTypes {
1515
release {

lib/src/main/java/com/victor/loading/book/BookLoading.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public BookLoading(Context context, AttributeSet attrs, int defStyleAttr) {
5353
private void initView(Context context) {
5454
LayoutInflater.from(context).inflate(R.layout.book_loading, this, true);
5555
pageViews = new ArrayList<>();
56-
bookHandler=new BookHandler(this);
56+
bookHandler = new BookHandler(this);
5757
}
5858

5959
@Override
@@ -129,38 +129,38 @@ public void onAnimationRepeat(Animator animation) {
129129

130130
}
131131

132-
public void start(){
133-
isStart=true;
132+
public void start() {
133+
isStart = true;
134134
bookHandler.obtainMessage().sendToTarget();
135135
}
136136

137-
public void stop(){
138-
isStart=false;
137+
public void stop() {
138+
isStart = false;
139139
bookHandler.removeCallbacks(null);
140140
bookHandler.removeCallbacksAndMessages(null);
141141
}
142142

143-
public boolean isStart(){
143+
public boolean isStart() {
144144
return isStart;
145145
}
146146

147-
static class BookHandler extends Handler{
147+
static class BookHandler extends Handler {
148148
private WeakReference<BookLoading> weakReference;
149149

150-
public BookHandler(BookLoading bookLoading){
151-
weakReference=new WeakReference<>(bookLoading);
150+
public BookHandler(BookLoading bookLoading) {
151+
weakReference = new WeakReference<>(bookLoading);
152152
}
153153

154154
@Override
155155
public void handleMessage(Message msg) {
156156
super.handleMessage(msg);
157-
BookLoading bookLoading=weakReference.get();
158-
if (null==bookLoading)
157+
BookLoading bookLoading = weakReference.get();
158+
if (null == bookLoading)
159159
return;
160160
bookLoading.playAnim();
161161

162-
Message message=obtainMessage();
163-
sendMessageDelayed(message,DURATION*5);
162+
Message message = obtainMessage();
163+
sendMessageDelayed(message, DURATION * 5);
164164
}
165165
}
166166
}

lib/src/main/java/com/victor/loading/newton/CradleBall.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.victor.loading.newton;
22

33
import android.content.Context;
4+
import android.content.res.TypedArray;
45
import android.graphics.Canvas;
56
import android.graphics.Color;
67
import android.graphics.Paint;
78
import android.util.AttributeSet;
89
import android.view.View;
910

11+
import com.victor.loading.R;
12+
1013
/**
1114
* @author Yan
1215
* create at 15/8/9
@@ -18,24 +21,31 @@ public class CradleBall extends View {
1821

1922
private Paint paint;
2023

24+
private int loadingColor = Color.WHITE;
25+
2126
public CradleBall(Context context) {
2227
super(context);
23-
initView();
28+
initView(null);
2429
}
2530

2631
public CradleBall(Context context, AttributeSet attrs) {
2732
super(context, attrs);
28-
initView();
33+
initView(attrs);
2934
}
3035

3136
public CradleBall(Context context, AttributeSet attrs, int defStyleAttr) {
3237
super(context, attrs, defStyleAttr);
33-
initView();
38+
initView(attrs);
3439
}
3540

36-
private void initView() {
41+
private void initView(AttributeSet attrs) {
42+
if (null != attrs) {
43+
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CradleBall);
44+
loadingColor = typedArray.getColor(R.styleable.CradleBall_cradle_ball_color, Color.WHITE);
45+
typedArray.recycle();
46+
}
3747
paint = new Paint();
38-
paint.setColor(Color.WHITE);
48+
paint.setColor(loadingColor);
3949
paint.setStyle(Paint.Style.FILL);
4050
paint.setAntiAlias(true);
4151
}
@@ -53,4 +63,14 @@ protected void onDraw(Canvas canvas) {
5363

5464
canvas.drawCircle(width / 2, height / 2, width / 2, paint);
5565
}
66+
67+
public void setLoadingColor(int color) {
68+
loadingColor = color;
69+
paint.setColor(color);
70+
postInvalidate();
71+
}
72+
73+
public int getLoadingColor() {
74+
return loadingColor;
75+
}
5676
}

lib/src/main/java/com/victor/loading/newton/NewtonCradleLoading.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,12 @@ public void stop() {
176176
public boolean isStart() {
177177
return isStart;
178178
}
179+
180+
public void setLoadingColor(int color) {
181+
cradleBallOne.setLoadingColor(color);
182+
cradleBallTwo.setLoadingColor(color);
183+
cradleBallThree.setLoadingColor(color);
184+
cradleBallFour.setLoadingColor(color);
185+
cradleBallFive.setLoadingColor(color);
186+
}
179187
}

lib/src/main/java/com/victor/loading/rotate/RotateLoading.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ protected void onDraw(Canvas canvas) {
140140
}
141141
}
142142

143+
public void setLoadingColor(int color) {
144+
this.color = color;
145+
}
146+
147+
public int getLoadingColor() {
148+
return color;
149+
}
150+
143151
public void start() {
144152
startAnimator();
145153
isStart = true;

lib/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<attr name="shadow_position" format="integer"/>
77
<attr name="loading_speed" format="integer"/>
88
</declare-styleable>
9+
10+
<declare-styleable name="CradleBall">
11+
<attr name="cradle_ball_color" format="color" />
12+
</declare-styleable>
913
</resources>

0 commit comments

Comments
 (0)