Skip to content

Commit ba9c202

Browse files
committed
Refactor
1 parent a574fe5 commit ba9c202

File tree

5 files changed

+130
-112
lines changed

5 files changed

+130
-112
lines changed

app/src/main/java/com/saulmm/material/Utils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.saulmm.material;
22

3+
import android.animation.Animator;
4+
import android.animation.AnimatorListenerAdapter;
35
import android.content.Context;
6+
import android.content.SharedPreferences;
47
import android.graphics.Outline;
58
import android.transition.Explode;
69
import android.transition.Fade;
710
import android.transition.Slide;
811
import android.view.View;
12+
import android.view.ViewAnimationUtils;
913
import android.view.Window;
1014
import android.view.animation.PathInterpolator;
1115

@@ -38,4 +42,44 @@ public static int getStatusBarHeight(Context c) {
3842
}
3943
return result;
4044
}
45+
46+
47+
public static void hideRevealEffect (final View v, int centerX, int centerY, int initialRadius) {
48+
49+
v.setVisibility(View.VISIBLE);
50+
51+
// create the animation (the final radius is zero)
52+
Animator anim = ViewAnimationUtils.createCircularReveal(
53+
v, centerX, centerY, initialRadius, 0);
54+
55+
anim.setDuration(350);
56+
57+
// make the view invisible when the animation is done
58+
anim.addListener(new AnimatorListenerAdapter() {
59+
@Override
60+
public void onAnimationEnd(Animator animation) {
61+
super.onAnimationEnd(animation);
62+
v.setVisibility(View.INVISIBLE);
63+
}
64+
});
65+
66+
anim.start();
67+
}
68+
69+
public static void showRevealEFfect (final View v, int centerX, int centerY, Animator.AnimatorListener lis) {
70+
71+
v.setVisibility(View.VISIBLE);
72+
73+
int height = v.getHeight();
74+
75+
Animator anim = ViewAnimationUtils.createCircularReveal(
76+
v, centerX, centerY, 0, height);
77+
78+
anim.setDuration(350);
79+
80+
anim.addListener(lis);
81+
anim.start();
82+
}
83+
84+
4185
}

app/src/main/java/com/saulmm/material/activities/ColorActivity.java

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.util.TypedValue;
1717
import android.view.View;
1818
import android.view.ViewAnimationUtils;
19+
import android.widget.CheckBox;
1920
import android.widget.CompoundButton;
2021
import android.widget.Switch;
2122

@@ -26,8 +27,6 @@ public class ColorActivity extends Activity {
2627

2728
private SharedPreferences sharedpreferences;
2829
private View revealView;
29-
private View circleHolder;
30-
private View fabButton;
3130

3231
@Override
3332
protected void onCreate(final Bundle savedInstanceState) {
@@ -36,37 +35,32 @@ protected void onCreate(final Bundle savedInstanceState) {
3635

3736
// Set the saved theme
3837
sharedpreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
39-
// sharedpreferences.edit().clear().apply();
4038
setTheme(sharedpreferences.getInt("theme", R.style.AppTheme));
4139

4240
setContentView(R.layout.activity_color);
4341

4442
// Views
4543
Switch themeSwitch = (Switch) findViewById(R.id.theme_switch);
46-
themeSwitch.setChecked(sharedpreferences.getBoolean("switch", false));
47-
revealView = findViewById(R.id.reveal_view);
48-
fabButton = findViewById(R.id.fab_button);
49-
circleHolder = findViewById(R.id.circle_holder);
44+
themeSwitch.setChecked(sharedpreferences.getBoolean(themeSwitch.getId()+"", false));
45+
themeSwitch.setOnCheckedChangeListener(checkedListener);
5046

51-
themeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
52-
@Override
53-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
47+
CheckBox themeCheck = (CheckBox) findViewById(R.id.theme_check);
48+
themeCheck.setChecked(sharedpreferences.getBoolean(themeCheck.getId()+"", false));
49+
themeCheck.setOnCheckedChangeListener(checkedListener);
5450

55-
// Save the state of the switch
56-
SharedPreferences.Editor ed = sharedpreferences.edit();
57-
ed.putInt("theme", (isChecked) ? R.style.Base_Theme_AppCompat : R.style.Base_Theme_AppCompat_Light);
58-
ed.putBoolean("switch", isChecked);
59-
ed.apply();
60-
}
61-
});
51+
revealView = findViewById(R.id.reveal_view);
6252

63-
// Show the unreveal effect
53+
// Show the unReveal effect
6454
final int cx = sharedpreferences.getInt("x", 0);
6555
final int cy = sharedpreferences.getInt("y", 0);
6656

67-
if (cx != 0 && cy != 0) {
57+
startHideRevealEffect(cx, cy);
58+
}
6859

69-
// Show the unreveal effect when the view is attached to the window
60+
private void startHideRevealEffect(final int cx, final int cy) {
61+
62+
if (cx != 0 && cy != 0) {
63+
// Show the unReveal effect when the view is attached to the window
7064
revealView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
7165
@Override
7266
public void onViewAttachedToWindow(View v) {
@@ -76,80 +70,29 @@ public void onViewAttachedToWindow(View v) {
7670
getTheme().resolveAttribute(android.R.attr.colorPrimary, outValue, true);
7771
revealView.setBackgroundColor(outValue.data);
7872

79-
hideRevealEffect(cx, cy);
73+
Utils.hideRevealEffect(revealView, cx, cy, 1920);
8074
}
8175

8276
@Override
8377
public void onViewDetachedFromWindow(View v) {}
8478
});
8579
}
86-
8780
}
8881

89-
public void showRevealEffect(View v, int primaryColor) {
90-
revealView.setVisibility(View.VISIBLE);
91-
92-
int [] location = new int[2];
93-
revealView.setBackgroundColor(primaryColor);
94-
v.getLocationOnScreen(location);
95-
96-
int cx = (location[0] + (v.getWidth() / 2));
97-
int cy = location[1] + (Utils.getStatusBarHeight(this) / 2);
98-
99-
100-
SharedPreferences.Editor ed = sharedpreferences.edit();
101-
ed.putInt("x", cx);
102-
ed.putInt("y", cy);
103-
ed.apply();
104-
105-
int height = revealView.getHeight();
106-
107-
Animator anim = ViewAnimationUtils.createCircularReveal(
108-
revealView, cx, cy, 0, height);
109-
110-
111-
anim.addListener(revealAnimationListener);
112-
anim.start();
113-
114-
hideNavigationStatus();
115-
}
116-
117-
118-
public void hideRevealEffect (int x, int y) {
119-
120-
revealView.setVisibility(View.VISIBLE);
121-
int initialRadius = 1920;
122-
123-
// create the animation (the final radius is zero)
124-
Animator anim = ViewAnimationUtils.createCircularReveal(
125-
revealView, x, y, 1080, 0);
126-
127-
Log.d("WTF", "X: "+x+" Y: "+y+" - "+initialRadius);
128-
129-
// make the view invisible when the animation is done
130-
anim.addListener(new AnimatorListenerAdapter() {
131-
@Override
132-
public void onAnimationEnd(Animator animation) {
133-
super.onAnimationEnd(animation);
134-
revealView.setVisibility(View.INVISIBLE);
135-
}
136-
});
137-
138-
anim.start();
139-
}
14082

14183
private void hideNavigationStatus() {
84+
14285
View decorView = getWindow().getDecorView();
14386

14487
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
14588
decorView.setSystemUiVisibility(uiOptions);
14689
}
14790

91+
14892
Animator.AnimatorListener revealAnimationListener = new Animator.AnimatorListener() {
149-
@Override
150-
public void onAnimationStart(Animator animation) {
15193

152-
}
94+
@Override
95+
public void onAnimationStart(Animator animation) {}
15396

15497
@Override
15598
public void onAnimationEnd(Animator animation) {
@@ -159,28 +102,22 @@ public void onAnimationEnd(Animator animation) {
159102
startActivity(i);
160103
overridePendingTransition(0, 0);
161104
finish();
162-
163105
}
164106

165107
@Override
166-
public void onAnimationCancel(Animator animation) {
167-
168-
}
108+
public void onAnimationCancel(Animator animation) {}
169109

170110
@Override
171-
public void onAnimationRepeat(Animator animation) {
172-
173-
174-
}
111+
public void onAnimationRepeat(Animator animation) {}
175112
};
176113

177114

178115
public void view(View view) {
116+
179117
int selectedTheme = 0;
180118
int primaryColor = 0;
181119

182120
switch (view.getId()) {
183-
184121
case R.id.circle1:
185122
selectedTheme = R.style.theme1;
186123
primaryColor = getResources().getColor(R.color.color_set_1_primary);
@@ -202,13 +139,32 @@ public void view(View view) {
202139
break;
203140
}
204141

142+
int [] location = new int[2];
143+
revealView.setBackgroundColor(primaryColor);
144+
view.getLocationOnScreen(location);
205145

206-
showRevealEffect(view, primaryColor);
146+
int cx = (location[0] + (view.getWidth() / 2));
147+
int cy = location[1] + (Utils.getStatusBarHeight(this) / 2);
207148

208149
SharedPreferences.Editor ed = sharedpreferences.edit();
150+
ed.putInt("x", cx);
151+
ed.putInt("y", cy);
209152
ed.putInt("theme", selectedTheme);
210153
ed.apply();
211-
//
212-
//
154+
155+
hideNavigationStatus();
156+
Utils.showRevealEFfect(revealView, cx, cy, revealAnimationListener);
213157
}
158+
159+
160+
CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() {
161+
162+
@Override
163+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
164+
SharedPreferences.Editor ed = sharedpreferences.edit();
165+
ed.putInt("theme", (isChecked) ? R.style.Base_Theme_AppCompat : R.style.Base_Theme_AppCompat_Light);
166+
ed.putBoolean(buttonView.getId()+"", isChecked);
167+
ed.apply();
168+
}
169+
};
214170
}

app/src/main/res/layout/activity_color.xml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
tools:text="Amazing text"/>
4747

4848
</RelativeLayout>
49-
49+
50+
5051

5152
<LinearLayout
5253
android:layout_width="match_parent"
@@ -72,12 +73,44 @@
7273

7374
<Switch
7475
android:id="@+id/theme_switch"
75-
android:layout_width="wrap_content"
76+
android:layout_width="0dp"
77+
android:layout_weight="1"
7678
android:layout_height="wrap_content"
7779
android:layout_gravity="center_vertical"/>
7880

7981
<TextView
80-
android:layout_width="wrap_content"
82+
android:layout_width="0dp"
83+
android:layout_weight="7"
84+
android:layout_height="wrap_content"
85+
android:text="Awesome"
86+
android:layout_marginLeft="16dp"
87+
android:layout_gravity="center_vertical"/>
88+
89+
</LinearLayout>
90+
91+
<TextView
92+
android:layout_width="match_parent"
93+
android:layout_height="48dp"
94+
android:text="More controls"
95+
android:textColor="?android:colorAccent"
96+
android:gravity="center_vertical"/>
97+
98+
<LinearLayout
99+
android:layout_width="match_parent"
100+
android:layout_height="48dp"
101+
android:layout_marginTop="8dp"
102+
android:orientation="horizontal">
103+
104+
<CheckBox
105+
android:id="@+id/theme_check"
106+
android:layout_width="0dp"
107+
android:layout_weight="1"
108+
android:layout_height="wrap_content"
109+
android:layout_gravity="center_vertical"/>
110+
111+
<TextView
112+
android:layout_width="0dp"
113+
android:layout_weight="7"
81114
android:layout_height="wrap_content"
82115
android:text="Awesome"
83116
android:layout_marginLeft="16dp"
@@ -132,7 +165,6 @@
132165
android:onClick="view"
133166
android:background="@drawable/color4"/>
134167

135-
136168
</LinearLayout>
137169
</LinearLayout>
138170

app/src/main/res/values-v21/styles.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
<!-- android theme colors -->
88
<item name="android:colorPrimary">@color/theme_default_primary</item>
9-
<item name="android:colorPrimaryDark">@color/theme_default_primary_dark</item>
109
<item name="android:windowTranslucentStatus">true</item>
1110
<item name="android:windowTranslucentNavigation">true</item>
1211
<item name="android:colorControlActivated">@color/accent</item>
@@ -20,36 +19,30 @@
2019
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
2120
</style>
2221

23-
2422
<style name="theme1" parent="AppTheme">
2523
<item name="android:colorPrimary">@color/color_set_1_primary</item>
26-
<item name="android:colorPrimaryDark">@color/color_set_1_primary_dark</item>
2724
<item name="android:colorControlActivated">@color/color_set_1_accent</item>
2825
<item name="android:colorAccent">@color/color_set_1_accent</item>
2926
</style>
3027

3128
<style name="theme2" parent="AppTheme">
3229
<item name="android:colorPrimary">@color/color_set_2_primary</item>
33-
<item name="android:colorPrimaryDark">@color/color_set_2_primary_dark</item>
3430
<item name="android:colorControlActivated">@color/color_set_2_accent</item>
3531
<item name="android:colorAccent">@color/color_set_2_accent</item>
3632
</style>
3733

3834
<style name="theme3" parent="AppTheme">
3935
<item name="android:colorPrimary">@color/color_set_3_primary</item>
40-
<item name="android:colorPrimaryDark">@color/color_set_3_primary_dark</item>
4136
<item name="android:colorControlActivated">@color/color_set_3_accent</item>
4237
<item name="android:colorAccent">@color/color_set_3_accent</item>
4338
</style>
4439

4540
<style name="theme4" parent="AppTheme">
4641
<item name="android:colorPrimary">@color/color_set_4_primary</item>
47-
<item name="android:colorPrimaryDark">@color/color_set_4_primary_dark</item>
4842
<item name="android:colorControlActivated">@color/color_set_4_accent</item>
4943
<item name="android:colorAccent">@color/color_set_4_accent</item>
5044
</style>
5145

52-
5346
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
5447
<item name="spinBars">true</item>
5548
<item name="color">@android:color/white</item>
@@ -63,6 +56,4 @@
6356
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
6457
</style>
6558

66-
67-
6859
</resources>

0 commit comments

Comments
 (0)