From 88598db9f7d99528cdf0f3d337ac16136eb5ff2e Mon Sep 17 00:00:00 2001 From: Ippei Nawate Date: Fri, 8 Aug 2014 11:43:05 +0900 Subject: [PATCH 1/3] fix doesn't be set background color bug --- sample/src/main/java/com/sample/SampleActivity.java | 8 ++++---- sample/src/main/java/com/sample/SampleFragment.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sample/src/main/java/com/sample/SampleActivity.java b/sample/src/main/java/com/sample/SampleActivity.java index f92a725..49088d6 100644 --- a/sample/src/main/java/com/sample/SampleActivity.java +++ b/sample/src/main/java/com/sample/SampleActivity.java @@ -13,6 +13,7 @@ public class SampleActivity extends ActionBarActivity implements ColorPickerCallback{ + private static final String TAG = SampleActivity.class.getSimpleName(); Caladbolg mCaladbolg; RelativeLayout mLayout; @@ -38,10 +39,9 @@ public boolean onTouch(View v, MotionEvent event) { @Override public void onPickColor(int rgb, int alpha) { - Log.v(SampleActivity.class.getSimpleName(), "RGB:" + rgb + " Alpha:" + alpha); - //mCaladbolg = Caladbolg.getInstance(rgb); - mLayout.setBackgroundColor(rgb); - mLayout.setAlpha((float) alpha); + Log.v(TAG, "RGB:" + rgb + " Alpha:" + alpha); + int argb = Color.argb(alpha, Color.red(rgb), Color.green(rgb), Color.blue(rgb)); + mLayout.setBackgroundColor(argb); } @Override diff --git a/sample/src/main/java/com/sample/SampleFragment.java b/sample/src/main/java/com/sample/SampleFragment.java index 838831b..48425a9 100644 --- a/sample/src/main/java/com/sample/SampleFragment.java +++ b/sample/src/main/java/com/sample/SampleFragment.java @@ -49,8 +49,8 @@ public boolean onTouch(View v, MotionEvent event) { @Override public void onPickColor(int rgb, int alpha) { Log.v(SampleFragment.class.getSimpleName(), "RGB:" + rgb + " Alpha:" + alpha); - mLayout.setBackgroundColor(rgb); - mLayout.setAlpha((float) alpha); + int argb = Color.argb(alpha, Color.red(rgb), Color.green(rgb), Color.blue(rgb)); + mLayout.setBackgroundColor(argb); } @Override From 3e635a98deb68ed48a9416218d592c776c018c23 Mon Sep 17 00:00:00 2001 From: Ippei Nawate Date: Fri, 8 Aug 2014 11:46:44 +0900 Subject: [PATCH 2/3] fix crash with FramgmentSampleActivity rotation --- sample/src/main/res/layout/activity_sample_fragment.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sample/src/main/res/layout/activity_sample_fragment.xml b/sample/src/main/res/layout/activity_sample_fragment.xml index 31abf03..d100279 100644 --- a/sample/src/main/res/layout/activity_sample_fragment.xml +++ b/sample/src/main/res/layout/activity_sample_fragment.xml @@ -1,5 +1,6 @@ Date: Fri, 8 Aug 2014 11:55:18 +0900 Subject: [PATCH 3/3] fix maintain background color when recreated activity or fragment --- .../main/java/com/sample/SampleActivity.java | 23 ++++++++++++-- .../main/java/com/sample/SampleFragment.java | 30 +++++++++++++++---- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/sample/src/main/java/com/sample/SampleActivity.java b/sample/src/main/java/com/sample/SampleActivity.java index 49088d6..a439982 100644 --- a/sample/src/main/java/com/sample/SampleActivity.java +++ b/sample/src/main/java/com/sample/SampleActivity.java @@ -1,6 +1,7 @@ package com.sample; import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; @@ -14,6 +15,7 @@ public class SampleActivity extends ActionBarActivity implements ColorPickerCallback{ private static final String TAG = SampleActivity.class.getSimpleName(); + private static final String SAVED_STATE_BACKGROUND_COLOR = "com.sample.SAVED_STATE_BACKGROUND_COLOR"; Caladbolg mCaladbolg; RelativeLayout mLayout; @@ -29,14 +31,31 @@ public boolean onTouch(View v, MotionEvent event) { return false; } }); + if (savedInstanceState != null) { + mLayout.setBackgroundColor(savedInstanceState.getInt(SAVED_STATE_BACKGROUND_COLOR)); + } setContentView(mLayout); - if (getSupportFragmentManager().findFragmentByTag("caladbolg") == null) { - mCaladbolg = Caladbolg.getInstance(Color.BLACK); + mCaladbolg = (Caladbolg) getSupportFragmentManager().findFragmentByTag("caladbolg"); + if (mCaladbolg == null) { + int color = Color.BLACK; + if (mLayout.getBackground() instanceof ColorDrawable) { + color = ((ColorDrawable) mLayout.getBackground()).getColor(); + } + mCaladbolg = Caladbolg.getInstance(color); mCaladbolg.show(getSupportFragmentManager(), "caladbolg"); } } + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (mLayout.getBackground() instanceof ColorDrawable) { + int color = ((ColorDrawable) mLayout.getBackground()).getColor(); + outState.putInt(SAVED_STATE_BACKGROUND_COLOR, color); + } + } + @Override public void onPickColor(int rgb, int alpha) { Log.v(TAG, "RGB:" + rgb + " Alpha:" + alpha); diff --git a/sample/src/main/java/com/sample/SampleFragment.java b/sample/src/main/java/com/sample/SampleFragment.java index 48425a9..48b7c57 100644 --- a/sample/src/main/java/com/sample/SampleFragment.java +++ b/sample/src/main/java/com/sample/SampleFragment.java @@ -1,6 +1,7 @@ package com.sample; import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; @@ -14,6 +15,8 @@ import com.caladbolg.Caladbolg.ColorPickerCallback; public class SampleFragment extends Fragment implements ColorPickerCallback { + private static final String TAG = SampleFragment.class.getSimpleName(); + private static final String SAVED_STATE_BACKGROUND_COLOR = "com.sample.SAVED_STATE_BACKGROUND_COLOR"; Caladbolg mCaladbolg; LinearLayout mLayout; @@ -23,10 +26,16 @@ public static SampleFragment newInstance() { } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getFragmentManager().findFragmentByTag("caladbolg") == null) { - mCaladbolg = Caladbolg.getInstance(this, Color.BLACK); + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + mCaladbolg = (Caladbolg) getFragmentManager().findFragmentByTag("caladbolg"); + if (mCaladbolg == null) { + int color = Color.BLACK; + if (mLayout.getBackground() instanceof ColorDrawable) { + color = ((ColorDrawable) mLayout.getBackground()).getColor(); + } + mCaladbolg = Caladbolg.getInstance(this, color); mCaladbolg.show(getFragmentManager(), "caladbolg"); } } @@ -42,13 +51,24 @@ public boolean onTouch(View v, MotionEvent event) { return false; } }); + if (savedInstanceState != null) { + mLayout.setBackgroundColor(savedInstanceState.getInt(SAVED_STATE_BACKGROUND_COLOR)); + } return mLayout; } + @Override + public void onSaveInstanceState(Bundle outState) { + if (mLayout.getBackground() instanceof ColorDrawable) { + int color = ((ColorDrawable) mLayout.getBackground()).getColor(); + outState.putInt(SAVED_STATE_BACKGROUND_COLOR, color); + } + } + @Override public void onPickColor(int rgb, int alpha) { - Log.v(SampleFragment.class.getSimpleName(), "RGB:" + rgb + " Alpha:" + alpha); + Log.v(TAG, "RGB:" + rgb + " Alpha:" + alpha); int argb = Color.argb(alpha, Color.red(rgb), Color.green(rgb), Color.blue(rgb)); mLayout.setBackgroundColor(argb); }