From a287cc730de6c46c9d2fb68daae46e23e031bdff Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 8 Dec 2016 17:08:17 +0300 Subject: [PATCH] loading bar view added --- .../components/views/MaterialLoadingBar.java | 110 ++++++++++++++++++ .../views/MaterialProgressDrawable.java | 16 +++ src/main/res/values/attrs.xml | 7 ++ 3 files changed, 133 insertions(+) create mode 100644 src/main/java/ru/touchin/roboswag/components/views/MaterialLoadingBar.java diff --git a/src/main/java/ru/touchin/roboswag/components/views/MaterialLoadingBar.java b/src/main/java/ru/touchin/roboswag/components/views/MaterialLoadingBar.java new file mode 100644 index 0000000..726e08d --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/views/MaterialLoadingBar.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.views; + +import android.content.Context; +import android.content.res.TypedArray; +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.widget.ImageView; + +import ru.touchin.roboswag.components.R; +import ru.touchin.roboswag.components.utils.UiUtils; + +/** + * Created by Ilia Kurtov on 07/12/2016. + * Simple endless progress bar view in material (round circle) style. + * It is able to setup size, stroke width and color. + * See MaterialLoadingBar Attributes: + * R.styleable#MaterialLoadingBar_size + * R.styleable#MaterialLoadingBar_strokeWidth + * R.styleable#MaterialLoadingBar_color + * Use + * R.styleable#MaterialLoadingBar_materialLoadingBarStyle + * to set default style of MaterialLoadingBar in your Theme. + * Sample: + * + * + */ +public class MaterialLoadingBar extends ImageView { + + private static int getPrimaryColor(@NonNull final Context context) { + final int colorAttr; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + colorAttr = android.R.attr.colorPrimary; + } else { + colorAttr = context.getResources().getIdentifier("colorPrimary", "attr", context.getPackageName()); + } + final TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(colorAttr, outValue, true); + return outValue.data; + } + + private MaterialProgressDrawable progressDrawable; + + public MaterialLoadingBar(@NonNull final Context context) { + this(context, null); + } + + public MaterialLoadingBar(@NonNull final Context context, @Nullable final AttributeSet attrs) { + this(context, attrs, R.attr.materialLoadingBarStyle); + } + + public MaterialLoadingBar(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) { + super(context, attrs, defStyleAttr); + + final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialLoadingBar, + defStyleAttr, + 0); + final int size = (int) typedArray.getDimension(R.styleable.MaterialLoadingBar_size, UiUtils.OfMetrics.dpToPixels(context, 48)); + final int color = typedArray.getColor(R.styleable.MaterialLoadingBar_color, getPrimaryColor(context)); + final float strokeWidth = typedArray.getDimension(R.styleable.MaterialLoadingBar_strokeWidth, + UiUtils.OfMetrics.dpToPixels(context, 4)); + typedArray.recycle(); + + progressDrawable = new MaterialProgressDrawable(context, size); + progressDrawable.setColor(color); + progressDrawable.setStrokeWidth(strokeWidth); + setScaleType(ScaleType.CENTER); + setImageDrawable(progressDrawable); + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + progressDrawable.start(); + } + + @Override + protected void onDetachedFromWindow() { + progressDrawable.stop(); + super.onDetachedFromWindow(); + } + +} \ No newline at end of file diff --git a/src/main/java/ru/touchin/roboswag/components/views/MaterialProgressDrawable.java b/src/main/java/ru/touchin/roboswag/components/views/MaterialProgressDrawable.java index 0702763..25242c6 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/MaterialProgressDrawable.java +++ b/src/main/java/ru/touchin/roboswag/components/views/MaterialProgressDrawable.java @@ -46,6 +46,7 @@ public class MaterialProgressDrawable extends Drawable implements Runnable, Anim private static final float DEFAULT_STROKE_WIDTH_DP = 4.5f; private static final Parameters DEFAULT_PARAMETERS = new Parameters(20, 270, 4, 12, 4, 8); + private final int size; @NonNull private final Paint paint; @NonNull @@ -58,14 +59,29 @@ public class MaterialProgressDrawable extends Drawable implements Runnable, Anim private boolean running; public MaterialProgressDrawable(@NonNull final Context context) { + this(context, -1); + } + + public MaterialProgressDrawable(@NonNull final Context context, final int size) { super(); + this.size = size; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(UiUtils.OfMetrics.dpToPixels(context, DEFAULT_STROKE_WIDTH_DP)); paint.setColor(Color.BLACK); } + @Override + public int getIntrinsicWidth() { + return size; + } + + @Override + public int getIntrinsicHeight() { + return size; + } + /** * Returns width of arc. * diff --git a/src/main/res/values/attrs.xml b/src/main/res/values/attrs.xml index b4e54dd..263a114 100644 --- a/src/main/res/values/attrs.xml +++ b/src/main/res/values/attrs.xml @@ -26,4 +26,11 @@ + + + + + + + \ No newline at end of file