Skip to content

Commit

Permalink
Merge pull request #19 from nohana/feature/fix_sample
Browse files Browse the repository at this point in the history
fix some sample bug #18
  • Loading branch information
KeithYokoma committed Aug 25, 2014
2 parents 472f046 + 51827d4 commit d55965d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
31 changes: 25 additions & 6 deletions sample/src/main/java/com/sample/SampleActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +14,8 @@


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;
Expand All @@ -28,20 +31,36 @@ 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(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
Expand Down
34 changes: 27 additions & 7 deletions sample/src/main/java/com/sample/SampleFragment.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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");
}
}
Expand All @@ -42,15 +51,26 @@ 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);
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
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_sample_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frament_sample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.sample.SampleFragment"
Expand Down

0 comments on commit d55965d

Please sign in to comment.