com.angelatech.yeyelive.view.PeriscopeLayout.java Source code

Java tutorial

Introduction

Here is the source code for com.angelatech.yeyelive.view.PeriscopeLayout.java

Source

/*
 * Copyright (C) 2015, ??
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.angelatech.yeyelive.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.angelatech.yeyelive.R;

import java.util.Random;

public class PeriscopeLayout extends RelativeLayout {
    private Interpolator line = new LinearInterpolator();//
    private Interpolator acc = new AccelerateInterpolator();//
    private Interpolator dce = new DecelerateInterpolator();//?
    private Interpolator accdec = new AccelerateDecelerateInterpolator();//??
    private Interpolator[] interpolators;

    private int mHeight;
    private int mWidth;
    private LayoutParams lp;
    private Drawable[] drawables;
    private Random random = new Random();
    private int dHeight;
    private int dWidth;

    public PeriscopeLayout(Context context) {
        super(context);
        init(context);
    }

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

    public PeriscopeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PeriscopeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context mContext) {
        //?
        drawables = new Drawable[7];
        Drawable love_0 = ContextCompat.getDrawable(mContext, R.drawable.love_heart1);
        Drawable love_1 = ContextCompat.getDrawable(mContext, R.drawable.love_heart2);
        Drawable love_2 = ContextCompat.getDrawable(mContext, R.drawable.love_heart3);
        Drawable love_3 = ContextCompat.getDrawable(mContext, R.drawable.love_heart4);
        Drawable love_4 = ContextCompat.getDrawable(mContext, R.drawable.love_heart5);
        Drawable love_5 = ContextCompat.getDrawable(mContext, R.drawable.love_heart6);
        Drawable love_6 = ContextCompat.getDrawable(mContext, R.drawable.love_heart7);

        drawables[0] = love_0;
        drawables[1] = love_1;
        drawables[2] = love_2;
        drawables[3] = love_3;
        drawables[4] = love_4;
        drawables[5] = love_5;
        drawables[6] = love_6;
        //? ??
        //? 3?,??
        dHeight = love_0.getIntrinsicHeight();
        dWidth = love_0.getIntrinsicWidth();

        //  
        lp = new LayoutParams(dWidth, dHeight);
        lp.addRule(CENTER_HORIZONTAL, TRUE);//TRUE ?? ?true
        lp.addRule(ALIGN_PARENT_BOTTOM, TRUE);

        // ??
        interpolators = new Interpolator[4];
        interpolators[0] = line;
        interpolators[1] = acc;
        interpolators[2] = dce;
        interpolators[3] = accdec;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    public void addHeart() {
        ImageView imageView = new ImageView(getContext());
        //?
        imageView.setImageDrawable(drawables[random.nextInt(7)]);
        imageView.setLayoutParams(lp);
        addView(imageView);
        Animator set = getAnimator(imageView);
        set.addListener(new AnimEndListener(imageView));
        set.start();
    }

    private Animator getAnimator(View target) {
        AnimatorSet set = getEnterAnimtor(target);
        ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);
        AnimatorSet finalSet = new AnimatorSet();
        finalSet.playSequentially(set);
        finalSet.playSequentially(set, bezierValueAnimator);
        finalSet.setInterpolator(interpolators[random.nextInt(4)]);
        finalSet.setTarget(target);
        return finalSet;
    }

    private AnimatorSet getEnterAnimtor(final View target) {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);
        AnimatorSet enter = new AnimatorSet();
        enter.setDuration(500);
        enter.setInterpolator(new LinearInterpolator());
        enter.playTogether(alpha, scaleX, scaleY);
        enter.setTarget(target);
        return enter;
    }

    private ValueAnimator getBezierValueAnimator(View target) {
        //??- - 
        BezierEvaluator evaluator = new BezierEvaluator(getPointF(2), getPointF(1));
        // ?   
        ValueAnimator animator = ValueAnimator.ofObject(evaluator,
                new PointF((mWidth - dWidth) / 2, mHeight - dHeight), new PointF(random.nextInt(getWidth()), 0));
        animator.addUpdateListener(new BezierListenr(target));
        animator.setTarget(target);
        animator.setDuration(1500);
        return animator;
    }

    /**
     * ? 
     *
     */
    private PointF getPointF(int scale) {
        try {
            PointF pointF = new PointF();
            pointF.x = random.nextInt((mWidth - 100));//?100  x, ??~~
            //?Y ? ,Y??   ?
            pointF.y = random.nextInt((mHeight - 100)) / scale;
            return pointF;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private class BezierListenr implements ValueAnimator.AnimatorUpdateListener {
        private View target;

        public BezierListenr(View target) {
            this.target = target;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            try {
                //???x y view ??
                PointF pointF = (PointF) animation.getAnimatedValue();
                target.setX(pointF.x);
                target.setY(pointF.y);
                // ?alpha
                target.setAlpha(1 - animation.getAnimatedFraction());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private class AnimEndListener extends AnimatorListenerAdapter {
        private View target;

        public AnimEndListener(View target) {
            this.target = target;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //??add ?view????,view??remove
            removeView((target));
        }
    }
}