Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CloudyContainer extends LinearLayout implements ValueAnimator.AnimatorUpdateListener {
- private static final int DEFAULT_DIRECTION = Gravity.TOP;
- private static final float MAX_SHIFT_PERCENT = 0.1f;
- private static final long ANIMATION_DURATION = 50 * 1000;
- private Drawable mCloudsDrawable;
- private float mShiftPercentX;
- private boolean mIsAnimated;
- private int mGravity;
- private Rect mInvalidateBounds = new Rect();
- private ValueAnimator mAnimator = ValueAnimator.ofFloat(0, MAX_SHIFT_PERCENT, -MAX_SHIFT_PERCENT, 0);
- public CloudyContainer(Context context) {
- this(context, null);
- }
- public CloudyContainer(Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public CloudyContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- initAnimators();
- initAttrs(attrs, defStyleAttr);
- setWillNotDraw(false);
- mCloudsDrawable = ContextCompat.getDrawable(context, R.drawable.img_clouds);
- }
- private void initAttrs(AttributeSet attrs, int defStyle) {
- if (attrs == null) return;
- TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CloudyContainer, defStyle, 0);
- mGravity = a.getInt(R.styleable.CloudyContainer_cc_gravity, DEFAULT_DIRECTION);
- mIsAnimated = a.getBoolean(R.styleable.CloudyContainer_cc_animated, false);
- a.recycle();
- }
- private void setBounds(Drawable drawable, Canvas canvas) {
- mInvalidateBounds.top = 0;
- mInvalidateBounds.left = 0;
- mInvalidateBounds.bottom = canvas.getHeight();
- mInvalidateBounds.right = canvas.getWidth();
- Rect bounds = drawable.getBounds();
- switch (mGravity) {
- case Gravity.TOP:
- bounds.top = 0;
- break;
- case Gravity.CENTER:
- bounds.top = canvas.getHeight() / 2 - drawable.getIntrinsicHeight() / 2;
- break;
- case Gravity.BOTTOM:
- bounds.top = canvas.getHeight() - drawable.getIntrinsicHeight();
- }
- bounds.left = (canvas.getWidth() - drawable.getIntrinsicWidth());
- bounds.right = bounds.left + drawable.getIntrinsicWidth();
- bounds.bottom = bounds.top + drawable.getIntrinsicHeight();
- drawable.setBounds(bounds);
- }
- private void initAnimators() {
- mAnimator.addUpdateListener(this);
- mAnimator.setDuration(ANIMATION_DURATION);
- mAnimator.setRepeatCount(ValueAnimator.INFINITE);
- mAnimator.setInterpolator(new LinearInterpolator());
- }
- private void startAnimation() {
- mAnimator.start();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- setBounds(mCloudsDrawable, canvas);
- canvas.save();
- canvas.translate(mShiftPercentX * (float) canvas.getWidth(), 0);
- mCloudsDrawable.draw(canvas);
- canvas.restore();
- if (!mAnimator.isRunning() && mIsAnimated) {
- startAnimation();
- }
- }
- @Override
- public void onAnimationUpdate(ValueAnimator animation) {
- mShiftPercentX = (float) animation.getAnimatedValue();
- invalidate(mInvalidateBounds);
- }
- interface Gravity {
- int TOP = 0;
- int CENTER = 1;
- int BOTTOM = 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement