Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WheelProgressView.java « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21a521fd13eb59d21bb2673f91cabf0fc0a3bf5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.mapswithme.maps.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.appcompat.graphics.drawable.DrawableWrapper;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.mapswithme.maps.R;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.ThemeUtils;

/**
 * Draws progress wheel, consisting of circle with background and 'stop' button in the center of the circle.
 */
public class WheelProgressView extends AppCompatImageView
{
  private static final int DEFAULT_THICKNESS = 4;

  private int mProgress;
  private int mRadius;
  private Paint mFgPaint;
  private Paint mBgPaint;
  private int mStrokeWidth;
  private final RectF mProgressRect = new RectF(); // main rect for progress wheel
  private final RectF mCenterRect = new RectF(); // rect for stop button
  private final Point mCenter = new Point(); // center of rect
  private boolean mIsInit;
  private boolean mIsPending;
  private Drawable mCenterDrawable;
  private AnimationDrawable mPendingDrawable;

  public WheelProgressView(Context context)
  {
    super(context);
    init(context, null);
  }

  public WheelProgressView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    init(context, attrs);
  }

  public WheelProgressView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs)
  {
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WheelProgressView, 0, 0);
    mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.WheelProgressView_wheelThickness, DEFAULT_THICKNESS);
    final int progressColor = typedArray.getColor(R.styleable.WheelProgressView_wheelProgressColor, Color.WHITE);
    final int secondaryColor = typedArray.getColor(R.styleable.WheelProgressView_wheelSecondaryColor, Color.GRAY);
    mCenterDrawable = typedArray.getDrawable(R.styleable.WheelProgressView_centerDrawable);
    if (mCenterDrawable == null)
      mCenterDrawable = makeCenterDrawable(context);

    typedArray.recycle();

    mPendingDrawable = (AnimationDrawable) getResources().getDrawable(ThemeUtils.getResource(getContext(), R.attr.wheelPendingAnimation));
    Graphics.tint(mPendingDrawable, progressColor);

    mBgPaint = new Paint();
    mBgPaint.setColor(secondaryColor);
    mBgPaint.setStrokeWidth(mStrokeWidth);
    mBgPaint.setStyle(Paint.Style.STROKE);
    mBgPaint.setAntiAlias(true);

    mFgPaint = new Paint();
    mFgPaint.setColor(progressColor);
    mFgPaint.setStrokeWidth(mStrokeWidth);
    mFgPaint.setStyle(Paint.Style.STROKE);
    mFgPaint.setAntiAlias(true);
  }

  @NonNull
  private static Drawable makeCenterDrawable(@NonNull Context context)
  {
    Drawable normalDrawable = context.getResources().getDrawable(R.drawable.ic_close_spinner);
    Drawable wrapped = DrawableCompat.wrap(normalDrawable);
    DrawableCompat.setTint(wrapped.mutate(), ThemeUtils.getColor(context, R.attr.iconTint));
    return normalDrawable;
  }

  public void setProgress(int progress)
  {
    mProgress = progress;
    invalidate();
  }

  public int getProgress()
  {
    return mProgress;
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh)
  {
    final int left = getPaddingLeft();
    final int top = getPaddingTop();
    final int right = w - getPaddingRight();
    final int bottom = h - getPaddingBottom();
    final int width = right - left;
    final int height = bottom - top;

    mRadius = (Math.min(width, height) - mStrokeWidth) / 2;
    mCenter.set(left + width / 2, top + height / 2);
    mProgressRect.set(mCenter.x - mRadius, mCenter.y - mRadius, mCenter.x + mRadius, mCenter.y + mRadius);

    Drawable d = ((mCenterDrawable instanceof DrawableWrapper) ? ((DrawableWrapper) mCenterDrawable)
        .getWrappedDrawable() : mCenterDrawable);
    if (d instanceof BitmapDrawable)
    {
      Bitmap bmp = ((BitmapDrawable)d).getBitmap();
      int halfw = bmp.getWidth() / 2;
      int halfh = bmp.getHeight() / 2;
      mCenterDrawable.setBounds(mCenter.x - halfw, mCenter.y - halfh, mCenter.x + halfw, mCenter.y + halfh);
    }
    else
      mCenterRect.set(mProgressRect);

    mIsInit = true;
  }

  @Override
  protected void onDraw(Canvas canvas)
  {
    if (mIsInit)
    {
      super.onDraw(canvas);

      if (!mIsPending)
      {
        canvas.drawCircle(mCenter.x, mCenter.y, mRadius, mBgPaint);
        canvas.drawArc(mProgressRect, -90, 360 * mProgress / 100, false, mFgPaint);
      }

      mCenterDrawable.draw(canvas);
    }
  }

  public void setPending(boolean pending)
  {
    mIsPending = pending;
    if (mIsPending)
    {
      mPendingDrawable.start();
      setImageDrawable(mPendingDrawable);
    }
    else
    {
      setImageDrawable(null);
      mPendingDrawable.stop();
    }

    invalidate();
  }

  public boolean isPending()
  {
    return mIsPending;
  }
}