Skip to content

Commit

Permalink
Dynamic UI colors
Browse files Browse the repository at this point in the history
  • Loading branch information
timkoers committed Feb 15, 2016
1 parent ec464d6 commit 626dca3
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Currently you can change:
* toggle whether to show crop frame/guidelines
* setup color/width/count of crop frame/rows/columns
* choose whether you want rectangle or oval crop area
* the UI colors (Toolbar, StatusBar, ActiveWidget and the RotationWheel line)
* and more...
# Compatibility
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta1'
classpath 'com.android.tools.build:gradle:2.0.0-beta4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 4 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ android {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
abortOnError false
}

}

dependencies {
Expand Down
11 changes: 11 additions & 0 deletions sample/src/main/java/com/yalantis/ucrop/sample/SampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
Expand Down Expand Up @@ -215,6 +216,16 @@ private UCrop advancedConfig(@NonNull UCrop uCrop) {
}
options.setCompressionQuality(mSeekBarQuality.getProgress());

int TOOLBAR_COLOR = ContextCompat.getColor(this, R.color.toolbar);
int STATUS_BAR_COLOR = ContextCompat.getColor(this, R.color.status_bar);
int ACTIVE_WIDGET_COLOR = ContextCompat.getColor(this, R.color.active_widget);
int ROTATION_WHEEL_LINE_COLOR = ContextCompat.getColor(this, R.color.rotation_wheel_line);

options.setToolbarColor(TOOLBAR_COLOR);
options.setStatusBarColor(STATUS_BAR_COLOR);
options.setActiveWidgetColor(ACTIVE_WIDGET_COLOR);
options.setRotationWheelLineColor(ROTATION_WHEEL_LINE_COLOR);

/*
If you want to configure how gestures work for all UCropActivity tabs
Expand Down
4 changes: 4 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
<color name="colorPrimary">#FF6E40</color>
<color name="colorPrimaryDark">#CC5833</color>
<color name="colorAccent">#FF6E40</color>
<color name="toolbar">#FF6E40</color>
<color name="status_bar">#CC5833</color>
<color name="active_widget">#FF6E40</color>
<color name="rotation_wheel_line">#FF6E40</color>
</resources>
7 changes: 5 additions & 2 deletions ucrop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 10
targetSdkVersion 23
versionCode 9
versionName "1.2.3"
versionName "1.2.4"
}
buildTypes {
release {
Expand All @@ -21,7 +21,10 @@ android {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

lintOptions {
abortOnError false
}

resourcePrefix 'ucrop_'
}

Expand Down
36 changes: 35 additions & 1 deletion ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public static class Options {
public static final String EXTRA_CROP_GRID_COLUMN_COUNT = EXTRA_PREFIX + ".CropGridColumnCount";
public static final String EXTRA_CROP_GRID_COLOR = EXTRA_PREFIX + ".CropGridColor";
public static final String EXTRA_CROP_GRID_STROKE_WIDTH = EXTRA_PREFIX + ".CropGridStrokeWidth";

public static final String EXTRA_TOOL_BAR_COLOR = EXTRA_PREFIX + ".ToolbarColor";
public static final String EXTRA_STATUS_BAR_COLOR = EXTRA_PREFIX + ".StatusBarColor";
public static final String EXTRA_UCROP_COLOR_WIDGET_ACTIVE = EXTRA_PREFIX + ".UcropColorWidgetActive";
public static final String EXTRA_UCROP_COLOR_PROGRESS_WHEEL_LINE = EXTRA_PREFIX + ".UcropColorProgressWheelLine";


private final Bundle mOptionBundle;

Expand Down Expand Up @@ -350,7 +356,35 @@ public void setCropGridColor(@ColorInt int color) {
public void setCropGridStrokeWidth(@IntRange(from = 0) int width) {
mOptionBundle.putInt(EXTRA_CROP_GRID_STROKE_WIDTH, width);
}


/**
* @param color - desired resolved color of the toolbar
*/
public void setToolbarColor(@ColorInt int color){
mOptionBundle.putInt(EXTRA_TOOL_BAR_COLOR, color);
}

/**
* @param color - desired resolved color of the statusbar
*/
public void setStatusBarColor(@ColorInt int color){
mOptionBundle.putInt(EXTRA_STATUS_BAR_COLOR, color);
}

/**
* @param color - desired resolved color of the active and selected widget (default is orange)
*/
public void setActiveWidgetColor(@ColorInt int color){
mOptionBundle.putInt(EXTRA_UCROP_COLOR_WIDGET_ACTIVE, color);
}

/**
* @param color - desired resolved color of the line in the rotation wheel (default is darker orange)
*/
public void setRotationWheelLineColor(@ColorInt int color){
mOptionBundle.putInt(EXTRA_UCROP_COLOR_PROGRESS_WHEEL_LINE, color);
}

}

}
52 changes: 47 additions & 5 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public class UCropActivity extends AppCompatActivity {
private static final int SCALE_WIDGET_SENSITIVITY_COEFFICIENT = 15000;
private static final int ROTATE_WIDGET_SENSITIVITY_COEFFICIENT = 42;

// Enables dynamic coloring
private int TOOLBAR_COLOR = -1;
private int STATUS_BAR_COLOR = -1;
private int ACTIVE_WIDGET_COLOR = -1;
private int PROGRESS_WHEEL_LINE_COLOR = -1;

private GestureCropImageView mGestureCropImageView;
private OverlayView mOverlayView;
private ViewGroup mWrapperStateAspectRatio, mWrapperStateRotate, mWrapperStateScale;
Expand All @@ -84,6 +90,32 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ucrop_activity_photobox);

// Make sure that the colors are not empty (==-1) before proceeding
STATUS_BAR_COLOR = getResources().getColor(R.color.ucrop_color_statusbar);
TOOLBAR_COLOR = getResources().getColor(R.color.ucrop_color_toolbar);
ACTIVE_WIDGET_COLOR = getResources().getColor(R.color.ucrop_color_widget_active);
PROGRESS_WHEEL_LINE_COLOR = getResources().getColor(R.color.ucrop_color_progress_wheel_line);

// Then check if the intent contains the color data
final Intent intent = getIntent();
Bundle optionsBundle = intent.getBundleExtra(UCrop.EXTRA_OPTIONS);

if(optionsBundle.containsKey(UCrop.Options.EXTRA_TOOL_BAR_COLOR)){
TOOLBAR_COLOR = optionsBundle.getInt(UCrop.Options.EXTRA_TOOL_BAR_COLOR);
}

if(optionsBundle.containsKey(UCrop.Options.EXTRA_STATUS_BAR_COLOR)){
STATUS_BAR_COLOR = optionsBundle.getInt(UCrop.Options.EXTRA_STATUS_BAR_COLOR);
}

if(optionsBundle.containsKey(UCrop.Options.EXTRA_UCROP_COLOR_WIDGET_ACTIVE)){
ACTIVE_WIDGET_COLOR = optionsBundle.getInt(UCrop.Options.EXTRA_UCROP_COLOR_WIDGET_ACTIVE);
}

if(optionsBundle.containsKey(UCrop.Options.EXTRA_UCROP_COLOR_PROGRESS_WHEEL_LINE)){
PROGRESS_WHEEL_LINE_COLOR = optionsBundle.getInt(UCrop.Options.EXTRA_UCROP_COLOR_PROGRESS_WHEEL_LINE);
}

setupViews();
setImageData();
setInitialState();
Expand Down Expand Up @@ -209,13 +241,21 @@ private void processOptions(@NonNull Intent intent) {

private void setupViews() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

if(TOOLBAR_COLOR != -1){
toolbar.setBackgroundColor(TOOLBAR_COLOR);
}

setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ucrop_ic_cross);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
}
setStatusBarColor(getResources().getColor(R.color.ucrop_color_statusbar));

if(STATUS_BAR_COLOR != -1){
setStatusBarColor(STATUS_BAR_COLOR);
}

UCropView uCropView = (UCropView) findViewById(R.id.ucrop);
mGestureCropImageView = uCropView.getCropImageView();
Expand Down Expand Up @@ -251,7 +291,7 @@ public void onScale(float currentScale) {
}

/**
* use {@link R.color#ucrop_color_widget_active} for color filter
* use {@link ACTIVE_WIDGET_COLOR} for color filter
*/
private void setupStatesWrapper() {
ImageView stateScaleImageView = (ImageView) findViewById(R.id.image_view_state_scale);
Expand All @@ -263,17 +303,17 @@ private void setupStatesWrapper() {
StateListDrawable stateAspectRatioSelector = new StateListDrawable();

Drawable stateScaleSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_scale).mutate();
stateScaleSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateScaleSelectedDrawable.setColorFilter(ACTIVE_WIDGET_COLOR, PorterDuff.Mode.SRC_ATOP);
stateScaleSelector.addState(new int[]{android.R.attr.state_selected}, stateScaleSelectedDrawable);
stateScaleSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_scale));

Drawable stateRotateSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_rotate).mutate();
stateRotateSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateRotateSelectedDrawable.setColorFilter(ACTIVE_WIDGET_COLOR, PorterDuff.Mode.SRC_ATOP);
stateRotateSelector.addState(new int[]{android.R.attr.state_selected}, stateRotateSelectedDrawable);
stateRotateSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_rotate));

Drawable stateAspectRatioSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.ucrop_ic_crop).mutate();
stateAspectRatioSelectedDrawable.setColorFilter(ContextCompat.getColor(this, R.color.ucrop_color_widget_active), PorterDuff.Mode.SRC_ATOP);
stateAspectRatioSelectedDrawable.setColorFilter(ACTIVE_WIDGET_COLOR, PorterDuff.Mode.SRC_ATOP);
stateAspectRatioSelector.addState(new int[]{android.R.attr.state_selected}, stateAspectRatioSelectedDrawable);
stateAspectRatioSelector.addState(new int[0], ContextCompat.getDrawable(this, R.drawable.ucrop_ic_crop));

Expand Down Expand Up @@ -340,6 +380,8 @@ public void onScrollStart() {
mGestureCropImageView.cancelAllAnimations();
}
});

((HorizontalProgressWheelView) findViewById(R.id.rotate_scroll_wheel)).setLineColor(PROGRESS_WHEEL_LINE_COLOR);


findViewById(R.id.wrapper_reset_rotate).setOnClickListener(new View.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yalantis.ucrop.view.widget;

import android.support.annotation.ColorInt;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
Expand Down Expand Up @@ -29,6 +30,8 @@ public class HorizontalProgressWheelView extends View {
private boolean mScrollStarted;
private float mTotalScrollDistance;

private int LINE_COLOR = -1;

public HorizontalProgressWheelView(Context context) {
this(context, null);
}
Expand All @@ -50,6 +53,11 @@ public HorizontalProgressWheelView(Context context, AttributeSet attrs, int defS
public void setScrollingListener(ScrollingListener scrollingListener) {
mScrollingListener = scrollingListener;
}

public void setLineColor(@ColorInt int LINE_COLOR){
this.LINE_COLOR = LINE_COLOR;
invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Expand Down Expand Up @@ -87,7 +95,7 @@ protected void onDraw(Canvas canvas) {
int linesCount = mCanvasClipBounds.width() / (mProgressLineWidth + mProgressLineMargin);
float deltaX = (mTotalScrollDistance) % (float) (mProgressLineMargin + mProgressLineWidth);

mProgressLinePaint.setColor(getResources().getColor(R.color.ucrop_color_progress_wheel_line));
mProgressLinePaint.setColor(LINE_COLOR);
for (int i = 0; i < linesCount; i++) {
if (i < (linesCount / 4)) {
mProgressLinePaint.setAlpha((int) (255 * (i / (float) (linesCount / 4))));
Expand Down Expand Up @@ -120,6 +128,8 @@ private void onScrollEvent(MotionEvent event, float distance) {
@SuppressWarnings("deprecation")
private void init() {

LINE_COLOR = getResources().getColor(R.color.ucrop_color_progress_wheel_line);

mProgressLineWidth = getContext().getResources().getDimensionPixelSize(R.dimen.ucrop_width_horizontal_wheel_progress_line);
mProgressLineHeight = getContext().getResources().getDimensionPixelSize(R.dimen.ucrop_height_horizontal_wheel_progress_line);
mProgressLineMargin = getContext().getResources().getDimensionPixelSize(R.dimen.ucrop_margin_horizontal_wheel_progress_line);
Expand Down

0 comments on commit 626dca3

Please sign in to comment.