Created
August 12, 2019 00:23
-
-
Save dmitry-bystrov/6e49363654ba92096422f821c390bee8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Кастомное вью для реализации индикатора загрузки в виде тени, покрывающей изображение и уменьшающейся | |
* сверху вниз по мере увеличения прогресса | |
*/ | |
public final class RoundedRectProgress extends View { | |
private static final String DEF_COLOR = "#b2000000"; | |
private static final int DEF_CORNER = 10; | |
private int cornerSize; | |
private Paint shadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private Paint transparentPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private RectF shadowRect = new RectF(); | |
private RectF transparentRect = new RectF(); | |
private float progress = 0; | |
public RoundedRectProgress(Context context) { | |
super(context); | |
init(context); | |
} | |
public RoundedRectProgress(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(context); | |
} | |
public RoundedRectProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context); | |
} | |
private void init(Context context) { | |
final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); | |
shadowPaint.setColor(Color.parseColor(DEF_COLOR)); | |
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
cornerSize = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEF_CORNER, metrics)); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
final int width = MeasureSpec.getSize(widthMeasureSpec); | |
final int height = MeasureSpec.getSize(heightMeasureSpec); | |
shadowRect.set(0, 0, width, height); | |
setLayerType(LAYER_TYPE_SOFTWARE, null); | |
} | |
/** | |
* Метод для установки значения прогресса загрузки | |
* | |
* @param progress значение от 0.0f до 1.0f | |
*/ | |
public void setProgress(float progress) { | |
this.progress = progress; | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
transparentRect.set(0, 0, shadowRect.right, shadowRect.bottom * progress); | |
canvas.drawRoundRect(shadowRect, cornerSize, cornerSize, shadowPaint); | |
canvas.drawRect(transparentRect, transparentPaint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment