Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. public class CloudyContainer extends LinearLayout implements ValueAnimator.AnimatorUpdateListener {
  2.  
  3.     private static final int DEFAULT_DIRECTION = Gravity.TOP;
  4.     private static final float MAX_SHIFT_PERCENT = 0.1f;
  5.     private static final long ANIMATION_DURATION = 50 * 1000;
  6.  
  7.     private Drawable mCloudsDrawable;
  8.     private float mShiftPercentX;
  9.  
  10.     private boolean mIsAnimated;
  11.     private int mGravity;
  12.  
  13.     private Rect mInvalidateBounds = new Rect();
  14.  
  15.     private ValueAnimator mAnimator = ValueAnimator.ofFloat(0, MAX_SHIFT_PERCENT, -MAX_SHIFT_PERCENT, 0);
  16.  
  17.     public CloudyContainer(Context context) {
  18.         this(context, null);
  19.     }
  20.  
  21.     public CloudyContainer(Context context, @Nullable AttributeSet attrs) {
  22.         this(context, attrs, 0);
  23.     }
  24.  
  25.     public CloudyContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  26.         super(context, attrs, defStyleAttr);
  27.         initAnimators();
  28.         initAttrs(attrs, defStyleAttr);
  29.         setWillNotDraw(false);
  30.         mCloudsDrawable = ContextCompat.getDrawable(context, R.drawable.img_clouds);
  31.     }
  32.  
  33.     private void initAttrs(AttributeSet attrs, int defStyle) {
  34.         if (attrs == null) return;
  35.  
  36.         TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CloudyContainer, defStyle, 0);
  37.         mGravity = a.getInt(R.styleable.CloudyContainer_cc_gravity, DEFAULT_DIRECTION);
  38.         mIsAnimated = a.getBoolean(R.styleable.CloudyContainer_cc_animated, false);
  39.  
  40.         a.recycle();
  41.     }
  42.  
  43.     private void setBounds(Drawable drawable, Canvas canvas) {
  44.         mInvalidateBounds.top = 0;
  45.         mInvalidateBounds.left = 0;
  46.         mInvalidateBounds.bottom = canvas.getHeight();
  47.         mInvalidateBounds.right = canvas.getWidth();
  48.  
  49.         Rect bounds = drawable.getBounds();
  50.         switch (mGravity) {
  51.             case Gravity.TOP:
  52.                 bounds.top = 0;
  53.                 break;
  54.             case Gravity.CENTER:
  55.                 bounds.top = canvas.getHeight() / 2 - drawable.getIntrinsicHeight() / 2;
  56.                 break;
  57.             case Gravity.BOTTOM:
  58.                 bounds.top = canvas.getHeight() - drawable.getIntrinsicHeight();
  59.         }
  60.  
  61.         bounds.left = (canvas.getWidth() - drawable.getIntrinsicWidth());
  62.         bounds.right = bounds.left + drawable.getIntrinsicWidth();
  63.         bounds.bottom = bounds.top + drawable.getIntrinsicHeight();
  64.         drawable.setBounds(bounds);
  65.     }
  66.  
  67.     private void initAnimators() {
  68.         mAnimator.addUpdateListener(this);
  69.         mAnimator.setDuration(ANIMATION_DURATION);
  70.         mAnimator.setRepeatCount(ValueAnimator.INFINITE);
  71.         mAnimator.setInterpolator(new LinearInterpolator());
  72.     }
  73.  
  74.     private void startAnimation() {
  75.         mAnimator.start();
  76.     }
  77.  
  78.     @Override
  79.     protected void onDraw(Canvas canvas) {
  80.         super.onDraw(canvas);
  81.         setBounds(mCloudsDrawable, canvas);
  82.  
  83.         canvas.save();
  84.         canvas.translate(mShiftPercentX * (float) canvas.getWidth(), 0);
  85.         mCloudsDrawable.draw(canvas);
  86.         canvas.restore();
  87.  
  88.         if (!mAnimator.isRunning() && mIsAnimated) {
  89.             startAnimation();
  90.         }
  91.     }
  92.  
  93.     @Override
  94.     public void onAnimationUpdate(ValueAnimator animation) {
  95.         mShiftPercentX = (float) animation.getAnimatedValue();
  96.         invalidate(mInvalidateBounds);
  97.     }
  98.  
  99.     interface Gravity {
  100.         int TOP = 0;
  101.         int CENTER = 1;
  102.         int BOTTOM = 2;
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement