QQ控件点赞效果

QQ空间点赞效果
逻辑非常简单
public class GoodButton extends LinearLayout {

    private View view = null;
    private Activity mContext;
    private RelativeLayout mRelativeLayout;
    private int[] imgs = {
            R.mipmap.good_true,
            R.mipmap.hart_true,
            R.mipmap.star_true
    };
    private RadioButton mRadioButton;


    public RadioButton getmRadioButton() {
        return mRadioButton;
    }


    public GoodButton(Context context) {
        this(context, null);
    }

    public GoodButton(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public GoodButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = (Activity) context;

        View view = View.inflate(context, R.layout.customgoodbutton, this);

        mRadioButton = view.findViewById(R.id.customgoodbuttonbtn);
        mRelativeLayout = view.findViewById(R.id.customgoodbuttonbtnRelativeLayout);


        mRadioButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doAnima();
            }
        });


        


    }


    private void doAnima() {


        final Window window= mContext.getWindow();

        Random random = new Random();
        int i = random.nextInt(3);
        Random random3 = new Random();
        int i3 = random3.nextInt(100);
        int i3a = i % 2 == 0 ? i3 : -(i3);


        view = new ImageView(mContext);


        ((ImageView) view).setImageResource(imgs[i]);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width =this.getWidth();
        layoutParams.height=this.getHeight();
        window.addContentView(view, layoutParams);

        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationY", this.getY()+this.getHeight(), -1000, -1000);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "translationX", this.getX()+this.getWidth(), this.getX()+this.getWidth()+i3a, this.getX()+this.getWidth()+i3a+i3a);

        animator3.setDuration(400);
        animator1.setDuration(2000);

        AnimatorSet set = new AnimatorSet();
        set.play(animator3).with(animator1);
        set.start();


        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view = null;
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });



    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mRelativeLayout = null;
    }

}

你可能感兴趣的:(QQ控件点赞效果)