Skip to content

Commit 42c4690

Browse files
committed
polish
1 parent c120ffe commit 42c4690

File tree

8 files changed

+276
-130
lines changed

8 files changed

+276
-130
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.avenwu.deepinandroid;
22

3+
import android.os.Bundle;
34
import android.support.v4.app.Fragment;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
48

59
/**
610
* Created by chaobin on 3/3/15.
711
*/
812
public class BreathLightViewDemo extends Fragment {
9-
13+
@Override
14+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
15+
View view = inflater.inflate(R.layout.ripple_layout, null);
16+
view.setOnClickListener(null);
17+
return view;
18+
}
1019
}

sample/src/main/java/com/avenwu/deepinandroid/RippleFragment.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

sample/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
style="@style/DemoItemStyle"
9494
android:text="呼吸灯"
9595
android:onClick="openFragment"
96-
android:tag="com.avenwu.deepinandroid.RippleFragment" />
96+
android:tag="com.avenwu.deepinandroid.BreathLightViewDemo" />
9797
</LinearLayout>
9898
</FrameLayout>
9999
</ScrollView>
Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
5-
android:background="#fff">
6+
android:background="#fff"
7+
android:clickable="false">
68

7-
<net.avenwu.support.widget.BreathLightView xmlns:android="http://schemas.android.com/apk/res/android"
9+
<net.avenwu.support.widget.BreathingLayout
10+
android:id="@+id/custom"
811
android:layout_width="match_parent"
9-
android:layout_height="match_parent">
12+
android:layout_height="300dp"
13+
app:autoStart="true"
14+
app:rippleEndRadius="100dp"
15+
app:rippleStartRadius="20dp"
16+
app:rippleColor="@color/tab_orange">
1017

11-
</net.avenwu.support.widget.BreathLightView>
12-
</FrameLayout>
18+
<Button
19+
android:id="@+id/btnPlay"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:text="Click to Play animation"
23+
android:layout_gravity="bottom"
24+
android:layout_margin="16dp"
25+
android:visibility="gone" />
26+
27+
<TextView
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="└(^o^)┘"
31+
android:textStyle="bold"
32+
android:textAppearance="?android:textAppearanceMedium"
33+
android:layout_gravity="center"
34+
android:textColor="#fff"/>
35+
</net.avenwu.support.widget.BreathingLayout>
36+
37+
<net.avenwu.support.widget.BreathingLayout
38+
android:layout_width="450dp"
39+
android:layout_height="100dp"
40+
app:rippleEndRadius="50dp"
41+
app:rippleTime="2000" />
42+
43+
<net.avenwu.support.widget.BreathingLayout
44+
android:layout_width="100dp"
45+
android:layout_height="250dp"
46+
app:rippleColor="@color/tab_green"
47+
app:rippleStartRadius="30dp"
48+
app:rippleTime="1500"/>
49+
50+
<net.avenwu.support.widget.BreathingLayout
51+
android:layout_width="match_parent"
52+
android:layout_height="500dp"
53+
app:rippleColor="@color/tab_purple" />
54+
</RelativeLayout>

support/src/main/java/net/avenwu/support/widget/BreathLightView.java

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package net.avenwu.support.widget;
2+
3+
import android.animation.AnimatorSet;
4+
import android.animation.ObjectAnimator;
5+
import android.animation.ValueAnimator;
6+
import android.content.Context;
7+
import android.content.res.TypedArray;
8+
import android.graphics.Canvas;
9+
import android.graphics.Paint;
10+
import android.graphics.RectF;
11+
import android.util.AttributeSet;
12+
import android.util.Log;
13+
import android.util.Property;
14+
import android.util.TypedValue;
15+
import android.view.View;
16+
import android.view.animation.LinearInterpolator;
17+
18+
import net.avenwu.support.R;
19+
20+
/**
21+
* Created by chaobin on 4/6/15.
22+
*/
23+
@SuppressWarnings("NewApi")
24+
public class BreathingDelegate {
25+
private Paint mPaint;
26+
private RectF mRippleRect = new RectF();
27+
private float mRippleRadius;
28+
private float mEndRadius;
29+
private int mRippleAlpha = 0xff;
30+
private int mDuration;
31+
private RectF mBorderRect = new RectF();
32+
private AnimatorSet mAnimatorSet = new AnimatorSet();
33+
private boolean mAutoStart;
34+
private int mRippleColor;
35+
private View mTarget;
36+
37+
public BreathingDelegate(Context context, AttributeSet attrs, View view) {
38+
mTarget = view;
39+
mTarget.setWillNotDraw(false);
40+
mTarget.setDrawingCacheEnabled(false);
41+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BreathingLayout);
42+
mAutoStart = a.getBoolean(R.styleable.BreathingLayout_autoStart, true);
43+
mRippleColor = a.getColor(R.styleable.BreathingLayout_rippleColor, 0xFF0099CC);
44+
mRippleRadius = a.getDimensionPixelSize(R.styleable.BreathingLayout_rippleStartRadius, -1);
45+
if (mRippleRadius == -1) {
46+
mRippleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getContext().getResources().getDisplayMetrics());
47+
}
48+
mEndRadius = a.getDimensionPixelSize(R.styleable.BreathingLayout_rippleEndRadius, -1);
49+
if (mEndRadius == -1) {
50+
mEndRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getContext().getResources().getDisplayMetrics());
51+
}
52+
if (mEndRadius <= mRippleRadius) {
53+
throw new IllegalStateException("rippleEndRadius can not be smaller than rippleStartRadius");
54+
}
55+
mDuration = a.getInt(R.styleable.BreathingLayout_rippleTime, 3000);
56+
a.recycle();
57+
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
58+
mPaint.setColor(mRippleColor);
59+
mPaint.setStyle(Paint.Style.FILL);
60+
prepareAnimation();
61+
if (mAutoStart) {
62+
start();
63+
}
64+
}
65+
66+
Property<BreathingDelegate, Float> mRadiusProperty = new Property<BreathingDelegate, Float>(Float.class, "mRippleRadius") {
67+
@Override
68+
public Float get(BreathingDelegate object) {
69+
return object.getRadius();
70+
}
71+
72+
@Override
73+
public void set(BreathingDelegate object, Float value) {
74+
object.setRadius(value);
75+
}
76+
};
77+
Property<BreathingDelegate, Integer> mAlphaProperty = new Property<BreathingDelegate, Integer>(Integer.class, "mRippleAlpha") {
78+
@Override
79+
public Integer get(BreathingDelegate object) {
80+
return object.getRippleAlpha();
81+
}
82+
83+
@Override
84+
public void set(BreathingDelegate object, Integer value) {
85+
object.setRippleAlpha(value);
86+
}
87+
};
88+
89+
private float getRadius() {
90+
return mRippleRadius;
91+
}
92+
93+
private void setRadius(float radius) {
94+
this.mRippleRadius = radius;
95+
}
96+
97+
public int getRippleAlpha() {
98+
return mRippleAlpha;
99+
}
100+
101+
public void setRippleAlpha(int rippleAlpha) {
102+
this.mRippleAlpha = rippleAlpha;
103+
}
104+
105+
private void prepareAnimation() {
106+
ObjectAnimator animator = ObjectAnimator.ofFloat(this, mRadiusProperty, mRippleRadius, mEndRadius);
107+
animator.setDuration(mDuration);
108+
animator.setRepeatCount(ValueAnimator.INFINITE);
109+
animator.setRepeatMode(ValueAnimator.REVERSE);
110+
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
111+
@Override
112+
public void onAnimationUpdate(ValueAnimator animation) {
113+
mTarget.invalidate();
114+
}
115+
});
116+
117+
ObjectAnimator alpha = ObjectAnimator.ofInt(this, mAlphaProperty, mRippleAlpha, 0x00);
118+
alpha.setDuration(mDuration);
119+
alpha.setRepeatCount(ValueAnimator.INFINITE);
120+
alpha.setRepeatMode(ValueAnimator.RESTART);
121+
mAnimatorSet.setInterpolator(new LinearInterpolator());
122+
mAnimatorSet.playTogether(animator, alpha);
123+
}
124+
125+
public void toggle() {
126+
if (mAnimatorSet.isRunning()) {
127+
stop();
128+
}/* else if (mAnimatorSet.isPaused()) {
129+
mAnimatorSet.resume();
130+
} */ else {
131+
mAnimatorSet.start();
132+
}
133+
}
134+
135+
public void start() {
136+
mAnimatorSet.start();
137+
}
138+
139+
public void stop() {
140+
mAnimatorSet.cancel();
141+
}
142+
143+
private Context getContext() {
144+
return mTarget.getContext();
145+
}
146+
147+
public void onDraw(Canvas canvas) {
148+
mRippleRect.set(mBorderRect.centerX() - mRippleRadius, mBorderRect.centerY() - mRippleRadius,
149+
mBorderRect.centerX() + mRippleRadius, mBorderRect.centerY() + mRippleRadius);
150+
canvas.drawOval(mRippleRect, mPaint);
151+
Log.d("BreathingLayout", "onDraw=" + mRippleRect.toString());
152+
}
153+
154+
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
155+
mBorderRect.set(left, top, right, bottom);
156+
Log.d("BreathingLayout", "onLayout:" + mBorderRect.toString());
157+
}
158+
159+
public void onDetachedFromWindow() {
160+
mAnimatorSet.cancel();
161+
Log.d("BreathingLayout", "onDetachedFromWindow");
162+
}
163+
}

0 commit comments

Comments
 (0)