1. package dev.hienlt.my;
  2.  
  3. import android.animation.ValueAnimator;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.PorterDuffXfermode;
  10. import android.graphics.RectF;
  11. import android.support.annotation.Nullable;
  12. import android.util.AttributeSet;
  13. import android.view.View;
  14.  
  15. /**
  16.  * Created by hien.le on 21/11/2018.
  17.  */
  18.  
  19. public class overlaylayout extends view implements ValueAnimator.AnimatorUpdateListener {
  20.     private static final string TAG = overlaylayout.class.getSimpleName();
  21.     private paint paint;
  22.     private paint paintLine;
  23.     private RectF rectF;
  24.     private int overlayWidth;
  25.     private int overlayHeight;
  26.     private ValueAnimator valueAnimator;
  27.     private float lineY;
  28.  
  29.     public overlaylayout(context context) {
  30.         super(context);
  31.         init();
  32.     }
  33.  
  34.     public overlaylayout(context context, @Nullable attributeset attrs) {
  35.         super(context, attrs);
  36.         init();
  37.     }
  38.  
  39.     @Override
  40.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  41.         super.onSizeChanged(w, h, oldw, oldh);
  42.         overlayWidth = (int) (w * 0.7f);
  43.         overlayHeight = (int) (overlayWidth * (10 / 16f));
  44.         float left = (w - overlayWidth) / 2f;
  45.         float top = (h - overlayHeight) / 2f;
  46.         rectF.set(left, top, left + overlayWidth, top + overlayHeight);
  47.         if (valueAnimator != null) {
  48.             valueAnimator.setFloatValues(top, top + overlayHeight);
  49.             valueAnimator.start();
  50.         }
  51.     }
  52.  
  53.     private void init() {
  54.         setLayerType(view.LAYER_TYPE_HARDWARE, null);
  55.         paint = new paint();
  56.         paint.setColor(color.WHITE);
  57.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  58.  
  59.         paintLine = new paint();
  60.         paintLine.setColor(color.WHITE);
  61.         paintLine.setStyle(paint.style.STROKE);
  62.         paintLine.setStrokeWidth(4);
  63.         paintLine.setAntiAlias(true);
  64.  
  65.         rectF = new RectF();
  66.         valueAnimator = new ValueAnimator();
  67.         valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
  68.         valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
  69.         valueAnimator.setDuration(3000);
  70.         valueAnimator.addUpdateListener(this);
  71.     }
  72.  
  73.     @Override
  74.     protected void onDraw(canvas canvas) {
  75.         super.onDraw(canvas);
  76.         canvas.drawColor(color.argb(170, 0, 0, 0));
  77.         canvas.drawRect(rectF, paint);
  78.  
  79.         canvas.drawLine(rectF.left, lineY, rectF.right, lineY, paintLine);
  80.     }
  81.  
  82.     @Override
  83.     protected void onDetachedFromWindow() {
  84.         super.onDetachedFromWindow();
  85.         if (valueAnimator != null) {
  86.             valueAnimator.cancel();
  87.         }
  88.     }
  89.  
  90.     @Override
  91.     public void onAnimationUpdate(ValueAnimator animation) {
  92.         lineY = (float) animation.getAnimatedValue();
  93.         invalidate();
  94.     }
  95. }