Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
saulmm committed Oct 29, 2014
1 parent a574fe5 commit ba9c202
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 112 deletions.
44 changes: 44 additions & 0 deletions app/src/main/java/com/saulmm/material/Utils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.saulmm.material;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Outline;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.Window;
import android.view.animation.PathInterpolator;

Expand Down Expand Up @@ -38,4 +42,44 @@ public static int getStatusBarHeight(Context c) {
}
return result;
}


public static void hideRevealEffect (final View v, int centerX, int centerY, int initialRadius) {

v.setVisibility(View.VISIBLE);

// create the animation (the final radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(
v, centerX, centerY, initialRadius, 0);

anim.setDuration(350);

// make the view invisible when the animation is done
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
v.setVisibility(View.INVISIBLE);
}
});

anim.start();
}

public static void showRevealEFfect (final View v, int centerX, int centerY, Animator.AnimatorListener lis) {

v.setVisibility(View.VISIBLE);

int height = v.getHeight();

Animator anim = ViewAnimationUtils.createCircularReveal(
v, centerX, centerY, 0, height);

anim.setDuration(350);

anim.addListener(lis);
anim.start();
}


}
132 changes: 44 additions & 88 deletions app/src/main/java/com/saulmm/material/activities/ColorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.util.TypedValue;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;

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

private SharedPreferences sharedpreferences;
private View revealView;
private View circleHolder;
private View fabButton;

@Override
protected void onCreate(final Bundle savedInstanceState) {
Expand All @@ -36,37 +35,32 @@ protected void onCreate(final Bundle savedInstanceState) {

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

setContentView(R.layout.activity_color);

// Views
Switch themeSwitch = (Switch) findViewById(R.id.theme_switch);
themeSwitch.setChecked(sharedpreferences.getBoolean("switch", false));
revealView = findViewById(R.id.reveal_view);
fabButton = findViewById(R.id.fab_button);
circleHolder = findViewById(R.id.circle_holder);
themeSwitch.setChecked(sharedpreferences.getBoolean(themeSwitch.getId()+"", false));
themeSwitch.setOnCheckedChangeListener(checkedListener);

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

// Save the state of the switch
SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putInt("theme", (isChecked) ? R.style.Base_Theme_AppCompat : R.style.Base_Theme_AppCompat_Light);
ed.putBoolean("switch", isChecked);
ed.apply();
}
});
revealView = findViewById(R.id.reveal_view);

// Show the unreveal effect
// Show the unReveal effect
final int cx = sharedpreferences.getInt("x", 0);
final int cy = sharedpreferences.getInt("y", 0);

if (cx != 0 && cy != 0) {
startHideRevealEffect(cx, cy);
}

// Show the unreveal effect when the view is attached to the window
private void startHideRevealEffect(final int cx, final int cy) {

if (cx != 0 && cy != 0) {
// Show the unReveal effect when the view is attached to the window
revealView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
Expand All @@ -76,80 +70,29 @@ public void onViewAttachedToWindow(View v) {
getTheme().resolveAttribute(android.R.attr.colorPrimary, outValue, true);
revealView.setBackgroundColor(outValue.data);

hideRevealEffect(cx, cy);
Utils.hideRevealEffect(revealView, cx, cy, 1920);
}

@Override
public void onViewDetachedFromWindow(View v) {}
});
}

}

public void showRevealEffect(View v, int primaryColor) {
revealView.setVisibility(View.VISIBLE);

int [] location = new int[2];
revealView.setBackgroundColor(primaryColor);
v.getLocationOnScreen(location);

int cx = (location[0] + (v.getWidth() / 2));
int cy = location[1] + (Utils.getStatusBarHeight(this) / 2);


SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putInt("x", cx);
ed.putInt("y", cy);
ed.apply();

int height = revealView.getHeight();

Animator anim = ViewAnimationUtils.createCircularReveal(
revealView, cx, cy, 0, height);


anim.addListener(revealAnimationListener);
anim.start();

hideNavigationStatus();
}


public void hideRevealEffect (int x, int y) {

revealView.setVisibility(View.VISIBLE);
int initialRadius = 1920;

// create the animation (the final radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(
revealView, x, y, 1080, 0);

Log.d("WTF", "X: "+x+" Y: "+y+" - "+initialRadius);

// make the view invisible when the animation is done
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
revealView.setVisibility(View.INVISIBLE);
}
});

anim.start();
}

private void hideNavigationStatus() {

View decorView = getWindow().getDecorView();

int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
}


Animator.AnimatorListener revealAnimationListener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}
@Override
public void onAnimationStart(Animator animation) {}

@Override
public void onAnimationEnd(Animator animation) {
Expand All @@ -159,28 +102,22 @@ public void onAnimationEnd(Animator animation) {
startActivity(i);
overridePendingTransition(0, 0);
finish();

}

@Override
public void onAnimationCancel(Animator animation) {

}
public void onAnimationCancel(Animator animation) {}

@Override
public void onAnimationRepeat(Animator animation) {


}
public void onAnimationRepeat(Animator animation) {}
};


public void view(View view) {

int selectedTheme = 0;
int primaryColor = 0;

switch (view.getId()) {

case R.id.circle1:
selectedTheme = R.style.theme1;
primaryColor = getResources().getColor(R.color.color_set_1_primary);
Expand All @@ -202,13 +139,32 @@ public void view(View view) {
break;
}

int [] location = new int[2];
revealView.setBackgroundColor(primaryColor);
view.getLocationOnScreen(location);

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

SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putInt("x", cx);
ed.putInt("y", cy);
ed.putInt("theme", selectedTheme);
ed.apply();
//
//

hideNavigationStatus();
Utils.showRevealEFfect(revealView, cx, cy, revealAnimationListener);
}


CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putInt("theme", (isChecked) ? R.style.Base_Theme_AppCompat : R.style.Base_Theme_AppCompat_Light);
ed.putBoolean(buttonView.getId()+"", isChecked);
ed.apply();
}
};
}
40 changes: 36 additions & 4 deletions app/src/main/res/layout/activity_color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
tools:text="Amazing text"/>

</RelativeLayout>




<LinearLayout
android:layout_width="match_parent"
Expand All @@ -72,12 +73,44 @@

<Switch
android:id="@+id/theme_switch"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>

<TextView
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:text="Awesome"
android:layout_marginLeft="16dp"
android:layout_gravity="center_vertical"/>

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="More controls"
android:textColor="?android:colorAccent"
android:gravity="center_vertical"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">

<CheckBox
android:id="@+id/theme_check"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>

<TextView
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:text="Awesome"
android:layout_marginLeft="16dp"
Expand Down Expand Up @@ -132,7 +165,6 @@
android:onClick="view"
android:background="@drawable/color4"/>


</LinearLayout>
</LinearLayout>

Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<!-- android theme colors -->
<item name="android:colorPrimary">@color/theme_default_primary</item>
<item name="android:colorPrimaryDark">@color/theme_default_primary_dark</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:colorControlActivated">@color/accent</item>
Expand All @@ -20,36 +19,30 @@
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>


<style name="theme1" parent="AppTheme">
<item name="android:colorPrimary">@color/color_set_1_primary</item>
<item name="android:colorPrimaryDark">@color/color_set_1_primary_dark</item>
<item name="android:colorControlActivated">@color/color_set_1_accent</item>
<item name="android:colorAccent">@color/color_set_1_accent</item>
</style>

<style name="theme2" parent="AppTheme">
<item name="android:colorPrimary">@color/color_set_2_primary</item>
<item name="android:colorPrimaryDark">@color/color_set_2_primary_dark</item>
<item name="android:colorControlActivated">@color/color_set_2_accent</item>
<item name="android:colorAccent">@color/color_set_2_accent</item>
</style>

<style name="theme3" parent="AppTheme">
<item name="android:colorPrimary">@color/color_set_3_primary</item>
<item name="android:colorPrimaryDark">@color/color_set_3_primary_dark</item>
<item name="android:colorControlActivated">@color/color_set_3_accent</item>
<item name="android:colorAccent">@color/color_set_3_accent</item>
</style>

<style name="theme4" parent="AppTheme">
<item name="android:colorPrimary">@color/color_set_4_primary</item>
<item name="android:colorPrimaryDark">@color/color_set_4_primary_dark</item>
<item name="android:colorControlActivated">@color/color_set_4_accent</item>
<item name="android:colorAccent">@color/color_set_4_accent</item>
</style>


<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
Expand All @@ -63,6 +56,4 @@
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
</style>



</resources>
Loading

1 comment on commit ba9c202

@Yilimmilk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Please sign in to comment.