From 3cba4804a1ea10574df29c180cf7c3f98b9ec7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Coexistence=E3=80=81?= Date: Wed, 1 Dec 2021 10:08:22 +0800 Subject: [PATCH 1/2] add a method to set title size and alignment mode --- .../main/java/com/yalantis/ucrop/UCrop.java | 19 ++++++++++- .../com/yalantis/ucrop/UCropActivity.java | 32 +++++++++++++------ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java b/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java index e28fd5bc9..a6ade67b3 100644 --- a/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java @@ -287,6 +287,8 @@ public static class Options { public static final String EXTRA_UCROP_COLOR_CONTROLS_WIDGET_ACTIVE = EXTRA_PREFIX + ".UcropColorControlsWidgetActive"; public static final String EXTRA_UCROP_WIDGET_COLOR_TOOLBAR = EXTRA_PREFIX + ".UcropToolbarWidgetColor"; + public static final String EXTRA_UCROP_TITLE_GRAVITY_TOOLBAR = EXTRA_PREFIX + ".UcropToolbarTitleGravity"; + public static final String EXTRA_UCROP_TITLE_SIZE_TOOLBAR = EXTRA_PREFIX + ".UcropToolbarTitleSize"; public static final String EXTRA_UCROP_TITLE_TEXT_TOOLBAR = EXTRA_PREFIX + ".UcropToolbarTitleText"; public static final String EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE = EXTRA_PREFIX + ".UcropToolbarCancelDrawable"; public static final String EXTRA_UCROP_WIDGET_CROP_DRAWABLE = EXTRA_PREFIX + ".UcropToolbarCropDrawable"; @@ -314,7 +316,7 @@ public Bundle getOptionBundle() { } /** - * Set one of {@link android.graphics.Bitmap.CompressFormat} that will be used to save resulting Bitmap. + * Set one of {@link Bitmap.CompressFormat} that will be used to save resulting Bitmap. */ public void setCompressionFormat(@NonNull Bitmap.CompressFormat format) { mOptionBundle.putString(EXTRA_COMPRESSION_FORMAT_NAME, format.name()); @@ -468,6 +470,21 @@ public void setToolbarWidgetColor(@ColorInt int color) { mOptionBundle.putInt(EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, color); } + /** + * @param gravity - desired text for Toolbar title alignment mode + * @see android.view.Gravity + */ + public void setToolbarTitleTextGravity(@Nullable int gravity) { + mOptionBundle.putInt(EXTRA_UCROP_TITLE_GRAVITY_TOOLBAR, gravity); + } + + /** + * @param textSize - desired text for Toolbar title text size + */ + public void setToolbarTitleTextSize(@Nullable Float textSize) { + mOptionBundle.putFloat(EXTRA_UCROP_TITLE_SIZE_TOOLBAR, textSize); + } + /** * @param text - desired text for Toolbar title */ diff --git a/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java b/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java index 0b32aafe8..2ff2b7947 100644 --- a/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java @@ -11,6 +11,7 @@ import android.os.Bundle; import android.text.TextUtils; import android.util.Log; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -84,6 +85,8 @@ public class UCropActivity extends AppCompatActivity { private static final int ROTATE_WIDGET_SENSITIVITY_COEFFICIENT = 42; private String mToolbarTitle; + private int mGravity = Gravity.START; + private Float mToolbarTextSize = 14F; // Enables dynamic coloring private int mToolbarColor; @@ -102,6 +105,8 @@ public class UCropActivity extends AppCompatActivity { private boolean mShowLoader = true; private UCropView mUCropView; + private TextView mToolbarTextView; + private Toolbar mToolbarView; private GestureCropImageView mGestureCropImageView; private OverlayView mOverlayView; private ViewGroup mWrapperStateAspectRatio, mWrapperStateRotate, mWrapperStateScale; @@ -212,7 +217,7 @@ private void setImageData(@NonNull Intent intent) { } /** - * This method extracts {@link com.yalantis.ucrop.UCrop.Options #optionsBundle} from incoming intent + * This method extracts {@link UCrop.Options #optionsBundle} from incoming intent * and setups Activity, {@link OverlayView} and {@link CropImageView} properly. */ @SuppressWarnings("deprecation") @@ -289,6 +294,8 @@ private void setupViews(@NonNull Intent intent) { mToolbarColor = intent.getIntExtra(UCrop.Options.EXTRA_TOOL_BAR_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar)); mActiveControlsWidgetColor = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_COLOR_CONTROLS_WIDGET_ACTIVE, ContextCompat.getColor(this, R.color.ucrop_color_active_controls_color)); + mGravity = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_TITLE_GRAVITY_TOOLBAR, Gravity.START); + mToolbarTextSize = intent.getFloatExtra(UCrop.Options.EXTRA_UCROP_TITLE_SIZE_TOOLBAR, 20F); mToolbarWidgetColor = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar_widget)); mToolbarCancelDrawable = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE, R.drawable.ucrop_ic_cross); mToolbarCropDrawable = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_WIDGET_CROP_DRAWABLE, R.drawable.ucrop_ic_done); @@ -335,22 +342,29 @@ private void setupViews(@NonNull Intent intent) { private void setupAppBar() { setStatusBarColor(mStatusBarColor); - final Toolbar toolbar = findViewById(R.id.toolbar); + mToolbarView = findViewById(R.id.toolbar); // Set all of the Toolbar coloring - toolbar.setBackgroundColor(mToolbarColor); - toolbar.setTitleTextColor(mToolbarWidgetColor); + mToolbarView.setBackgroundColor(mToolbarColor); + mToolbarView.setTitleTextColor(mToolbarWidgetColor); - final TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title); - toolbarTitle.setTextColor(mToolbarWidgetColor); - toolbarTitle.setText(mToolbarTitle); + mToolbarTextView = mToolbarView.findViewById(R.id.toolbar_title); + //Set the title size + mToolbarTextView.setTextSize(mToolbarTextSize); + mToolbarTextView.setTextColor(mToolbarWidgetColor); + mToolbarTextView.setText(mToolbarTitle); + + //Set the title alignment mode + Toolbar.LayoutParams lp = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT); + lp.gravity = mGravity; + mToolbarTextView.setLayoutParams(lp); // Color buttons inside the Toolbar Drawable stateButtonDrawable = ContextCompat.getDrawable(this, mToolbarCancelDrawable).mutate(); stateButtonDrawable.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP); - toolbar.setNavigationIcon(stateButtonDrawable); + mToolbarView.setNavigationIcon(stateButtonDrawable); - setSupportActionBar(toolbar); + setSupportActionBar(mToolbarView); final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayShowTitleEnabled(false); From 63e6965695616fa61f725c54d99708c97bd12264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Coexistence=E3=80=81?= Date: Wed, 23 Mar 2022 17:02:57 +0800 Subject: [PATCH 2/2] add constant value --- .../java/com/yalantis/ucrop/UCropActivity.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java b/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java index 094672b1b..1dddae785 100644 --- a/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java @@ -83,10 +83,14 @@ public class UCropActivity extends AppCompatActivity { private static final int TABS_COUNT = 3; private static final int SCALE_WIDGET_SENSITIVITY_COEFFICIENT = 15000; private static final int ROTATE_WIDGET_SENSITIVITY_COEFFICIENT = 42; + private static final float DEFAULT_TOOLBAR_TEXT_SIZE = 14F; + private static final int DEFAULT_TOOLBAR_GRAVITY = Gravity.START; private String mToolbarTitle; - private int mGravity = Gravity.START; - private Float mToolbarTextSize = 14F; + // Toolbar title alignment mode + private int mToolbarGravity = DEFAULT_TOOLBAR_GRAVITY; + // Toolbar title text size + private Float mToolbarTextSize = DEFAULT_TOOLBAR_TEXT_SIZE; // Enables dynamic coloring private int mToolbarColor; @@ -217,7 +221,7 @@ private void setImageData(@NonNull Intent intent) { } /** - * This method extracts {@link UCrop.Options #optionsBundle} from incoming intent + * This method extracts {@link com.yalantis.ucrop.UCrop.Options #optionsBundle} from incoming intent * and setups Activity, {@link OverlayView} and {@link CropImageView} properly. */ @SuppressWarnings("deprecation") @@ -295,7 +299,7 @@ private void setupViews(@NonNull Intent intent) { mToolbarColor = intent.getIntExtra(UCrop.Options.EXTRA_TOOL_BAR_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar)); mActiveControlsWidgetColor = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_COLOR_CONTROLS_WIDGET_ACTIVE, ContextCompat.getColor(this, R.color.ucrop_color_active_controls_color)); - mGravity = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_TITLE_GRAVITY_TOOLBAR, Gravity.START); + mToolbarGravity = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_TITLE_GRAVITY_TOOLBAR, Gravity.START); mToolbarTextSize = intent.getFloatExtra(UCrop.Options.EXTRA_UCROP_TITLE_SIZE_TOOLBAR, 20F); mToolbarWidgetColor = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar_widget)); mToolbarCancelDrawable = intent.getIntExtra(UCrop.Options.EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE, R.drawable.ucrop_ic_cross); @@ -357,7 +361,7 @@ private void setupAppBar() { //Set the title alignment mode Toolbar.LayoutParams lp = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT); - lp.gravity = mGravity; + lp.gravity = mToolbarGravity; mToolbarTextView.setLayoutParams(lp); // Color buttons inside the Toolbar