[android] add: FlatProgressView.

This commit is contained in:
a.marchuk 2015-07-24 19:37:31 +03:00 committed by Alex Zolotarev
parent 2ce9cde011
commit 2f3b3e6728
6 changed files with 176 additions and 6 deletions

View file

@ -127,7 +127,7 @@
android:layout_height="@dimen/progress_wheel_height"
android:layout_marginTop="@dimen/margin_half_plus"
android:background="?clickableBackground"
wheel:progressColor="@color/base_green"
wheel:wheelColor="@color/base_green"
wheel:secondaryColor="@color/downloader_progress_bg"
wheel:wheelThickness="@dimen/margin_quarter"/>
</LinearLayout>

View file

@ -28,7 +28,7 @@
android:layout_marginRight="@dimen/progress_wheel_width_neg"
android:paddingLeft="@dimen/margin_quarter"
android:paddingRight="@dimen/margin_quarter"
wheel:progressColor="@color/downloader_blue"
wheel:wheelColor="@color/downloader_blue"
wheel:secondaryColor="@color/downloader_progress_bg"
wheel:wheelThickness="@dimen/margin_eighth"/>
@ -49,7 +49,7 @@
android:layout_centerVertical="true"
android:paddingLeft="@dimen/margin_quarter"
android:paddingRight="@dimen/margin_quarter"
wheel:progressColor="@color/downloader_blue"
wheel:wheelColor="@color/downloader_blue"
wheel:secondaryColor="@color/downloader_progress_bg"
wheel:wheelThickness="@dimen/margin_eighth"/>

View file

@ -132,7 +132,7 @@
android:layout_height="@dimen/progress_wheel_height"
android:layout_marginTop="@dimen/margin_half_plus"
android:background="?clickableBackground"
wheel:progressColor="@color/base_green"
wheel:wheelColor="@color/base_green"
wheel:secondaryColor="@color/downloader_progress_bg"
wheel:wheelThickness="@dimen/margin_quarter"/>
</LinearLayout>

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="WheelProgressView">
<attr name="progressColor" format="color"/>
<attr name="wheelColor" format="color"/>
<attr name="secondaryColor" format="color"/>
<attr name="wheelThickness" format="dimension"/>
<attr name="clickableBackground" format="reference"/>
@ -16,6 +16,14 @@
</attr>
</declare-styleable>
<declare-styleable name="FlatProgressView">
<attr name="progressColor" format="color"/>
<attr name="progressThickness" format="dimension"/>
<attr name="headColor" format="color"/>
<attr name="headRadius" format="dimension"/>
<attr name="progress" format="integer"/>
</declare-styleable>
<!-- 7 inch tablet -->
<bool name="isSmallTablet">false</bool>
<!-- 10 inch tablet -->

View file

@ -0,0 +1,162 @@
package com.mapswithme.maps.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.mapswithme.maps.R;
public class FlatProgressView extends View
{
private int mThickness;
private int mHeadRadius;
private int mProgress;
private final Paint mProgressPaint = new Paint();
private final Paint mHeadPaint = new Paint();
private final RectF mHeadRect = new RectF();
private boolean mReady;
public FlatProgressView(Context context)
{
super(context);
init(null, 0);
}
public FlatProgressView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs, 0);
}
public FlatProgressView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
private void init(AttributeSet attrs, int defStyleAttr)
{
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.FlatProgressView, defStyleAttr, 0);
setThickness(ta.getDimensionPixelSize(R.styleable.FlatProgressView_progressThickness, 1));
setHeadRadius(ta.getDimensionPixelSize(R.styleable.FlatProgressView_headRadius, 4));
setProgress(ta.getInteger(R.styleable.FlatProgressView_progress, 0));
int color = ta.getColor(R.styleable.FlatProgressView_progressColor, Color.BLUE);
mProgressPaint.setColor(color);
color = ta.getColor(R.styleable.FlatProgressView_headColor, Color.BLUE);
mHeadPaint.setColor(color);
ta.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),
Math.max(mThickness, mHeadRadius * 2) + getPaddingTop() + getPaddingBottom());
}
@Override
protected void onDraw(Canvas canvas)
{
mReady = true;
canvas.save();
int intWidth = getWidth() - getPaddingLeft() - getPaddingRight();
canvas.translate(getPaddingLeft(), getPaddingTop());
int progressWidth = (intWidth * mProgress / 100);
if (progressWidth > 0)
{
int top = ((mHeadRadius * 2) > mThickness ? (mHeadRadius - mThickness / 2) : 0);
canvas.drawRect(0, top, progressWidth, top + mThickness, mProgressPaint);
}
if (mHeadRadius > 0)
{
int top = ((mHeadRadius * 2) > mThickness ? 0 : (mThickness / 2 - mHeadRadius));
canvas.translate(progressWidth - mHeadRadius, top);
canvas.drawOval(mHeadRect, mHeadPaint);
}
canvas.restore();
}
public int getProgress()
{
return mProgress;
}
public void setProgress(int progress)
{
if (mProgress == progress)
return;
if (progress < 0 || progress > 100)
throw new IllegalArgumentException("Progress must be within interval [0..100]");
mProgress = progress;
if (mReady)
invalidate();
}
public int getThickness()
{
return mThickness;
}
public void setThickness(int thickness)
{
if (thickness == mThickness)
return;
mThickness = thickness;
if (mReady)
invalidate();
}
public int getHeadRadius()
{
return mHeadRadius;
}
public void setHeadRadius(int headRadius)
{
if (headRadius == mHeadRadius)
return;
mHeadRadius = headRadius;
mHeadRect.set(0.0f, 0.0f, mHeadRadius * 2, mHeadRadius * 2);
if (mReady)
invalidate();
}
public void setProgressColor(int color)
{
if (color == mProgressPaint.getColor())
return;
mProgressPaint.setColor(color);
if (mReady)
invalidate();
}
public void setHeadColor(int color)
{
if (color == mHeadPaint.getColor())
return;
mHeadPaint.setColor(color);
if (mReady)
invalidate();
}
}

View file

@ -53,7 +53,7 @@ public class WheelProgressView extends View
{
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WheelProgressView, 0, 0);
mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.WheelProgressView_wheelThickness, DEFAULT_THICKNESS);
final int color = typedArray.getColor(R.styleable.WheelProgressView_progressColor, getResources().getColor(R.color.downloader_progress_bg));
final int color = typedArray.getColor(R.styleable.WheelProgressView_wheelColor, getResources().getColor(R.color.downloader_progress_bg));
final int secondaryColor = typedArray.getColor(R.styleable.WheelProgressView_secondaryColor, getResources().getColor(R.color.text_green));
typedArray.recycle();