Skip to content

Commit c120ffe

Browse files
committed
add custom ripple view
1 parent 846acdd commit c120ffe

File tree

8 files changed

+197
-4
lines changed

8 files changed

+197
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.avenwu.deepinandroid;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
/**
11+
* Created by chaobin on 3/6/15.
12+
*/
13+
public class GradientColorFragment extends Fragment {
14+
@Override
15+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
16+
return inflater.inflate(R.layout.gradient_layout, null);
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.avenwu.deepinandroid;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.LinearGradient;
7+
import android.graphics.Paint;
8+
import android.graphics.Shader;
9+
import android.util.AttributeSet;
10+
import android.view.View;
11+
12+
/**
13+
* Created by chaobin on 3/6/15.
14+
*/
15+
public class LinearGradientView extends View {
16+
Paint mPaint;
17+
LinearGradient mGradient;
18+
19+
public LinearGradientView(Context context) {
20+
this(context, null);
21+
}
22+
23+
public LinearGradientView(Context context, AttributeSet attrs) {
24+
this(context, attrs, 0);
25+
}
26+
27+
public LinearGradientView(Context context, AttributeSet attrs, int defStyleAttr) {
28+
super(context, attrs, defStyleAttr);
29+
mGradient = new LinearGradient(0, 0, 500, 0, new int[]{
30+
Color.RED, Color.YELLOW, Color.GREEN
31+
}, null, Shader.TileMode.CLAMP);
32+
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
33+
mPaint.setShader(mGradient);
34+
}
35+
36+
@Override
37+
protected void onDraw(Canvas canvas) {
38+
canvas.drawRect(0, 0, 500, 500, mPaint);
39+
}
40+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.support.v4.app.Fragment;
88
import android.support.v7.app.ActionBarActivity;
99
import android.view.View;
10+
import android.widget.Toast;
1011

1112
import com.avenwu.annotation.PrintMe;
1213

@@ -41,7 +42,7 @@ public void openActivity(View view) {
4142
try {
4243
startActivity(new Intent(this, Class.forName((String) view.getTag())));
4344
} catch (ClassNotFoundException e) {
44-
e.printStackTrace();
45+
error(e);
4546
}
4647
}
4748

@@ -52,12 +53,16 @@ public void openFragment(View view) {
5253
.addToBackStack(null)
5354
.commitAllowingStateLoss();
5455
} catch (InstantiationException e) {
55-
e.printStackTrace();
56+
error(e);
5657
} catch (IllegalAccessException e) {
57-
e.printStackTrace();
58+
error(e);
5859
} catch (ClassNotFoundException e) {
59-
e.printStackTrace();
60+
error(e);
6061
}
6162
}
6263

64+
private void error(Exception e) {
65+
e.printStackTrace();
66+
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
67+
}
6368
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.avenwu.deepinandroid;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.app.Fragment;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
9+
/**
10+
* Created by chaobin on 4/3/15.
11+
*/
12+
public class RippleFragment extends Fragment {
13+
@Override
14+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
15+
return inflater.inflate(R.layout.ripple_layout, null);
16+
}
17+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@
8484
android:text="Exif信息"
8585
android:onClick="openFragment"
8686
android:tag="com.avenwu.deepinandroid.ExifViewerFragment" />
87+
<Button
88+
style="@style/DemoItemStyle"
89+
android:text="Gradient渐变"
90+
android:onClick="openFragment"
91+
android:tag="com.avenwu.deepinandroid.GradientColorFragment" />
92+
<Button
93+
style="@style/DemoItemStyle"
94+
android:text="呼吸灯"
95+
android:onClick="openFragment"
96+
android:tag="com.avenwu.deepinandroid.RippleFragment" />
8797
</LinearLayout>
8898
</FrameLayout>
8999
</ScrollView>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="#fff">
7+
8+
<TextView
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="LinearGradient"
12+
android:textAppearance="?android:textAppearanceLarge"
13+
android:layout_margin="16dp" />
14+
15+
<com.avenwu.deepinandroid.LinearGradientView
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_margin="16dp" />
19+
</LinearLayout>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="#fff">
6+
7+
<net.avenwu.support.widget.BreathLightView xmlns:android="http://schemas.android.com/apk/res/android"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent">
10+
11+
</net.avenwu.support.widget.BreathLightView>
12+
</FrameLayout>

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
import android.content.Context;
44
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.RectF;
7+
import android.os.CountDownTimer;
58
import android.util.AttributeSet;
9+
import android.util.Log;
10+
import android.view.animation.LinearInterpolator;
611
import android.widget.FrameLayout;
12+
import android.widget.Scroller;
713

814
/**
915
* Created by chaobin on 3/3/15.
1016
*/
1117
public class BreathLightView extends FrameLayout {
18+
private Paint mPaint;
19+
private RectF mRippleRect = new RectF();
20+
private float mRippleRadius = 200;
21+
private float mInitRadius = 50;
22+
private int mDuration = 3000;
23+
private RectF mBorderRect = new RectF();
24+
private Scroller mScroller;
25+
private CountDownTimer mTimer;
26+
private boolean isZoomUp = true;
27+
1228
public BreathLightView(Context context) {
1329
this(context, null);
1430
}
@@ -24,10 +40,66 @@ public BreathLightView(Context context, AttributeSet attrs, int defStyleAttr) {
2440

2541
private void init() {
2642
setWillNotDraw(false);
43+
setDrawingCacheEnabled(false);
44+
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
45+
mPaint.setColor(0xFF0099CC);
46+
mPaint.setStyle(Paint.Style.FILL);
47+
mScroller = new Scroller(getContext(), new LinearInterpolator());
48+
mTimer = new CountDownTimer(100, 1000 / 30) {
49+
@Override
50+
public void onTick(long millisUntilFinished) {
51+
Log.d("BreathLightView", "currentX=" + mScroller.getCurrX());
52+
final float dx = mBorderRect.centerX() - mScroller.getCurrX();
53+
final float dy = mBorderRect.centerY() - mScroller.getCurrY();
54+
mRippleRect.set(mScroller.getCurrX(), mScroller.getCurrY(), mBorderRect.centerX() + dx, mBorderRect.centerY() + dy);
55+
int alpha = ((int) (dx / mRippleRadius * 0xff));
56+
Log.d("BreathLightView", "alpha=" + alpha);
57+
int a = 255 - alpha;
58+
// mPaint.setAlpha(a);
59+
Log.d("BreathLightView", "rect:=" + mRippleRect.toString());
60+
invalidate();
61+
}
62+
63+
@Override
64+
public void onFinish() {
65+
if (!mScroller.computeScrollOffset()) {
66+
isZoomUp = !isZoomUp;
67+
final int radius = (int) (isZoomUp ? -mRippleRadius : mRippleRadius);
68+
mScroller.startScroll(mScroller.getCurrX(), mScroller.getCurrY(), radius, radius, mDuration);
69+
}
70+
mTimer.start();
71+
72+
}
73+
};
74+
mTimer.start();
2775
}
2876

2977
@Override
3078
protected void onDraw(Canvas canvas) {
79+
canvas.drawOval(mRippleRect, mPaint);
80+
Log.d("BreathLightView", "onDraw");
81+
}
3182

83+
@Override
84+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85+
super.onLayout(changed, left, top, right, bottom);
86+
mBorderRect.set(left, top, right, bottom);
87+
final float centerX = mBorderRect.centerX();
88+
final float centerY = mBorderRect.centerY();
89+
final float l = centerX - mInitRadius;
90+
final float t = centerY - mInitRadius;
91+
mRippleRect.set(l, t, centerX + mInitRadius, centerY + mInitRadius);
92+
int radius = (int) (isZoomUp ? (-mRippleRadius) : (mRippleRadius));
93+
mScroller.startScroll((int) l, (int) t, radius, radius, mDuration);
94+
Log.d("BreathLightView", "onLayout:" + mRippleRect.toString());
95+
}
96+
97+
@Override
98+
protected void onDetachedFromWindow() {
99+
super.onDetachedFromWindow();
100+
if (mTimer != null) {
101+
mTimer.cancel();
102+
}
103+
Log.d("BreathLightView", "onDetachedFromWindow");
32104
}
33105
}

0 commit comments

Comments
 (0)