Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8fa94c5

Browse filesBrowse files
committed
test button,text,image
test custom radius
1 parent 38fcac4 commit 8fa94c5
Copy full SHA for 8fa94c5

File tree

Expand file treeCollapse file tree

3 files changed

+154
-48
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+154
-48
lines changed
Open diff view settings
Collapse file

‎library/src/main/java/xyz/hanks/library/SmallBang.java‎

Copy file name to clipboardExpand all lines: library/src/main/java/xyz/hanks/library/SmallBang.java
+75-33Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public class SmallBang extends View {
4444
private float DOT_BIG_RADIUS = 8;
4545
private float DOT_SMALL_RADIUS = 5;
4646
private int[] mExpandInset = new int[2];
47-
private SmallBangListener listener;
47+
private SmallBangListener mListener;
48+
private int centerY;
49+
private int centerX;
50+
4851
// 将下面的view变小
4952
// 画圆半径从小到大,同时颜色渐变 (P1)
5053
// 当半径到达 MAX_RADIUS, 开始画空心圆,空闲圆半径变大,画笔宽度从MAX_RADIUS变小
@@ -71,6 +74,14 @@ public SmallBang(Context context, AttributeSet attrs, int defStyleAttr, int defS
7174
init(attrs, defStyleAttr);
7275
}
7376

77+
public static SmallBang attach2Window(Activity activity) {
78+
ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
79+
SmallBang smallBang = new SmallBang(activity);
80+
rootView.addView(smallBang, new ViewGroup.LayoutParams(
81+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
82+
return smallBang;
83+
}
84+
7485
private void init(AttributeSet attrs, int defStyleAttr) {
7586
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
7687
circlePaint.setStyle(Paint.Style.FILL);
@@ -79,46 +90,89 @@ private void init(AttributeSet attrs, int defStyleAttr) {
7990

8091
/**
8192
* listener for animation change time
93+
*
8294
* @param listener
8395
*/
84-
public void setListener(SmallBangListener listener){
85-
this.listener = listener;
96+
public void setmListener(SmallBangListener listener) {
97+
this.mListener = listener;
8698
}
8799

88100
/**
89101
* set different colors for last dots
102+
*
90103
* @param newColors
91104
*/
92-
public void setColors(int[] newColors){
93-
this.colors = Arrays.copyOf(newColors,newColors.length);
105+
public void setColors(int[] newColors) {
106+
this.colors = Arrays.copyOf(newColors, newColors.length);
94107
}
95108

96-
public void bang(final View view) {
109+
/**
110+
* set small dot number
111+
* @param dotNumber
112+
*/
113+
public void setDotNumber(int dotNumber){
114+
DOT_NUMBER = dotNumber;
115+
}
116+
117+
public void bang(final View view, SmallBangListener listener) {
118+
bang(view, -1, listener);
119+
}
120+
121+
public void bang(final View view, float radius, SmallBangListener listener) {
122+
123+
// set listener
124+
if (listener != null) {
125+
setmListener(listener);
126+
this.mListener.onAnimationStart();
127+
}
128+
129+
Rect r = new Rect();
130+
view.getGlobalVisibleRect(r);
131+
int[] location = new int[2];
132+
getLocationOnScreen(location);
133+
r.offset(-location[0], -location[1]);
134+
r.inset(-mExpandInset[0], -mExpandInset[1]);
97135

98-
if(listener!=null) {
99-
listener.onAnimationStart();
136+
centerX = r.left + r.width() / 2;
137+
centerY = r.top + r.height() / 2;
138+
139+
if (radius != -1) {
140+
initRadius(radius);
141+
} else {
142+
initRadius(Math.max(r.width(),r.height()));
100143
}
101144

102-
int startDelay = 100;
103145
view.setScaleX(0.1f);
104146
view.setScaleY(0.1f);
105-
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration((long) (ANIMATE_DURATION*0.5f));
147+
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration((long) (ANIMATE_DURATION * 0.5f));
106148
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
107149
@Override
108150
public void onAnimationUpdate(ValueAnimator animation) {
109151
float animatedFraction = animation.getAnimatedFraction();
110-
view.setScaleX(0.1f + animatedFraction*0.9f);
111-
view.setScaleY(0.1f + animatedFraction*0.9f);
152+
view.setScaleX(0.1f + animatedFraction * 0.9f);
153+
view.setScaleY(0.1f + animatedFraction * 0.9f);
112154
}
113155
});
114156
animator.setInterpolator(new OvershootInterpolator(2));
115157
animator.setStartDelay((long) (ANIMATE_DURATION * P3));
116158

117159
animator.start();
118160
bang();
161+
162+
}
163+
164+
private void initRadius(float max_circle_radius) {
165+
MAX_CIRCLE_RADIUS = max_circle_radius;
166+
MAX_RADIUS = MAX_CIRCLE_RADIUS * 1.1f;
167+
DOT_BIG_RADIUS = MAX_CIRCLE_RADIUS * 0.07f;
168+
DOT_SMALL_RADIUS = DOT_BIG_RADIUS * 0.5f;
169+
}
170+
171+
public void bang(final View view) {
172+
bang(view, null);
119173
}
120174

121-
public void bang() {
175+
private void bang() {
122176
ValueAnimator valueAnimator = new ValueAnimator().ofFloat(0, 1).setDuration(ANIMATE_DURATION);
123177
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
124178
@Override
@@ -131,8 +185,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
131185
valueAnimator.addListener(new AnimatorListenerAdapter() {
132186
@Override
133187
public void onAnimationEnd(Animator animation) {
134-
if(listener!=null) {
135-
listener.onAnimationEnd();
188+
if (mListener != null) {
189+
mListener.onAnimationEnd();
136190
}
137191
}
138192
});
@@ -148,7 +202,6 @@ private void initDots() {
148202
dot.endColor = colors[random.nextInt(99999) % colors.length];
149203
dotList.add(dot);
150204
}
151-
152205
}
153206

154207
@Override
@@ -163,7 +216,7 @@ protected void onDraw(Canvas canvas) {
163216
int endColor = colors[1];
164217
circlePaint.setStyle(Paint.Style.FILL);
165218
circlePaint.setColor(evaluateColor(startColor, endColor, progress1));
166-
canvas.drawCircle(getWidth() / 2, getHeight() / 2, MAX_CIRCLE_RADIUS * progress1, circlePaint);
219+
canvas.drawCircle(centerX, centerY, MAX_CIRCLE_RADIUS * progress1, circlePaint);
167220
} else if (progress > P1) {
168221

169222
if (progress > P1 && progress <= P3) {
@@ -176,7 +229,7 @@ protected void onDraw(Canvas canvas) {
176229
float strokeWidth = (MAX_CIRCLE_RADIUS) * (1 - progress2);
177230
circlePaint.setStrokeWidth(strokeWidth);
178231

179-
canvas.drawCircle(getWidth() / 2, getHeight() / 2, (MAX_CIRCLE_RADIUS) * progress2 + strokeWidth / 2, circlePaint);
232+
canvas.drawCircle(centerX, centerY, (MAX_CIRCLE_RADIUS) * progress2 + strokeWidth / 2, circlePaint);
180233
}
181234
if (progress >= P2) {
182235
circlePaint.setStyle(Paint.Style.FILL);
@@ -188,33 +241,22 @@ protected void onDraw(Canvas canvas) {
188241
Dot dot = dotList.get(i);
189242
circlePaint.setColor(evaluateColor(dot.startColor, dot.endColor, progress3));
190243

191-
float x = (float) (r * Math.cos(i * 2 * Math.PI / DOT_NUMBER)) + getWidth() / 2;
192-
float y = (float) (r * Math.sin(i * 2 * Math.PI / DOT_NUMBER)) + getHeight() / 2;
244+
float x = (float) (r * Math.cos(i * 2 * Math.PI / DOT_NUMBER)) + centerX;
245+
float y = (float) (r * Math.sin(i * 2 * Math.PI / DOT_NUMBER)) + centerY;
193246
canvas.drawCircle(x, y, DOT_BIG_RADIUS * (1 - progress3), circlePaint);
194247

195248
Dot dot2 = dotList.get(i + 1);
196249

197250
circlePaint.setColor(evaluateColor(dot2.startColor, dot2.endColor, progress3));
198-
float x2 = (float) (r * Math.cos(i * 2 * Math.PI / DOT_NUMBER + 0.2)) + getWidth() / 2;
199-
float y2 = (float) (r * Math.sin(i * 2 * Math.PI / DOT_NUMBER + 0.2)) + getHeight() / 2;
251+
float x2 = (float) (r * Math.cos(i * 2 * Math.PI / DOT_NUMBER + 0.2)) + centerX;
252+
float y2 = (float) (r * Math.sin(i * 2 * Math.PI / DOT_NUMBER + 0.2)) + centerY;
200253
canvas.drawCircle(x2, y2, DOT_SMALL_RADIUS * (1 - progress3), circlePaint);
201254

202255
}
203256
}
204257
}
205-
206-
207258
}
208259

209-
public static SmallBang attach2Window(Activity activity) {
210-
ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
211-
SmallBang smallBang = new SmallBang(activity);
212-
rootView.addView(smallBang, new ViewGroup.LayoutParams(
213-
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
214-
return smallBang;
215-
}
216-
217-
218260
private int evaluateColor(int startValue, int endValue, float fraction) {
219261
if (fraction <= 0) {
220262
return startValue;
Collapse file

‎simple/src/main/java/xyz/hanks/smallbang/MainActivity.java‎

Copy file name to clipboardExpand all lines: simple/src/main/java/xyz/hanks/smallbang/MainActivity.java
+54-5Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
55
import android.view.View;
6+
import android.widget.Button;
67
import android.widget.ImageView;
8+
import android.widget.TextView;
79
import android.widget.Toast;
810

911
import xyz.hanks.library.SmallBang;
@@ -13,33 +15,80 @@ public class MainActivity extends AppCompatActivity {
1315

1416
private SmallBang mSmallBang;
1517
private ImageView mImage;
18+
private Button mButton;
19+
private TextView mText;
1620

1721
@Override
1822
protected void onCreate(Bundle savedInstanceState) {
1923
super.onCreate(savedInstanceState);
2024
setContentView(R.layout.activity_main);
25+
2126
mSmallBang = SmallBang.attach2Window(this);
27+
2228
mImage = (ImageView)findViewById(R.id.image);
2329
mImage.setOnClickListener(new View.OnClickListener() {
2430
@Override
2531
public void onClick(View v) {
26-
show(v);
32+
like(v);
33+
}
34+
});
35+
36+
37+
mButton = (Button) findViewById(R.id.button);
38+
mButton.setOnClickListener(new View.OnClickListener() {
39+
@Override
40+
public void onClick(View v) {
41+
addNumber(v);
42+
}
43+
});
44+
45+
mText = (TextView) findViewById(R.id.text);
46+
mText.setOnClickListener(new View.OnClickListener() {
47+
@Override
48+
public void onClick(View v) {
49+
redText(v);
50+
}
51+
});
52+
}
53+
54+
public void addNumber(View view){
55+
mSmallBang.bang(view,new SmallBangListener() {
56+
@Override
57+
public void onAnimationStart() {
58+
}
59+
60+
@Override
61+
public void onAnimationEnd() {
62+
toast("button +1");
2763
}
2864
});
65+
}
2966

67+
public void redText(View view){
68+
mText.setTextColor(0xFFCD8BF8);
69+
mSmallBang.bang(view,50,new SmallBangListener() {
70+
@Override
71+
public void onAnimationStart() {
72+
}
73+
74+
@Override
75+
public void onAnimationEnd() {
76+
toast("text+1");
77+
}
78+
});
3079
}
3180

32-
public void show(View view){
81+
public void like(View view){
82+
mImage.setImageResource(R.drawable.heart_red);
3383
mSmallBang.bang(view);
34-
mSmallBang.setListener(new SmallBangListener() {
84+
mSmallBang.setmListener(new SmallBangListener() {
3585
@Override
3686
public void onAnimationStart() {
37-
mImage.setImageResource(R.drawable.heart_red);
3887
}
3988

4089
@Override
4190
public void onAnimationEnd() {
42-
toast("+1");
91+
toast("heart+1");
4392
}
4493
});
4594
}
Collapse file
+25-10Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
android:layout_width="match_parent"
54
android:layout_height="match_parent"
6-
android:paddingBottom="@dimen/activity_vertical_margin"
7-
android:paddingLeft="@dimen/activity_horizontal_margin"
8-
android:paddingRight="@dimen/activity_horizontal_margin"
9-
android:paddingTop="@dimen/activity_vertical_margin"
10-
tools:context="xyz.hanks.smallbang.MainActivity">
5+
android:gravity="center_horizontal"
6+
android:orientation="vertical">
117

128
<ImageView
139
android:id="@+id/image"
14-
android:src="@drawable/heart"
1510
android:layout_width="30dp"
16-
android:layout_centerInParent="true"
1711
android:layout_height="30dp"
12+
android:layout_marginTop="50dp"
13+
android:src="@drawable/heart"
1814
android:text="Hello World!" />
1915

16+
<Button
17+
android:id="@+id/button"
18+
android:layout_width="40dp"
19+
android:layout_height="40dp"
20+
android:layout_marginTop="50dp"
21+
android:src="@drawable/heart"
22+
android:text="h"
23+
android:textSize="12sp" />
24+
25+
<TextView
26+
android:id="@+id/text"
27+
android:layout_width="50dp"
28+
android:layout_height="30dp"
29+
android:layout_marginTop="50dp"
30+
android:gravity="center"
31+
android:src="@drawable/heart"
32+
android:text="hanks"
33+
android:textSize="14sp" />
34+
2035

21-
</RelativeLayout>
36+
</LinearLayout>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.