/** * This function updates the dots view and the circle view with the respective sizes based on the * size of the icon being used. */ private void setEffectsViewSize() { if (iconSize != 0) { dotsView.setSize( (int) (iconSize * animationScaleFactor), (int) (iconSize * animationScaleFactor)); circleView.setSize(iconSize, iconSize); } }
/** * Does all the initial setup of the button such as retrieving all the attributes that were set in * xml and inflating the like button's view and initial state. * * @param context * @param attrs * @param defStyle */ private void init(Context context, AttributeSet attrs, int defStyle) { LayoutInflater.from(getContext()).inflate(R.layout.likeview, this, true); icon = (ImageView) findViewById(R.id.icon); dotsView = (DotsView) findViewById(R.id.dots); circleView = (CircleView) findViewById(R.id.circle); final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LikeButton, defStyle, 0); iconSize = array.getDimensionPixelSize(R.styleable.LikeButton_icon_size, -1); if (iconSize == -1) iconSize = 40; String iconType = array.getString(R.styleable.LikeButton_icon_type); likeDrawable = array.getDrawable(R.styleable.LikeButton_like_drawable); if (likeDrawable != null) setLikeDrawable(likeDrawable); unLikeDrawable = array.getDrawable(R.styleable.LikeButton_unlike_drawable); if (unLikeDrawable != null) setUnlikeDrawable(unLikeDrawable); if (iconType != null) if (!iconType.isEmpty()) currentIcon = parseIconType(iconType); circleStartColor = array.getColor(R.styleable.LikeButton_circle_start_color, 0); if (circleStartColor != 0) circleView.setStartColor(circleStartColor); circleEndColor = array.getColor(R.styleable.LikeButton_circle_end_color, 0); if (circleEndColor != 0) circleView.setEndColor(circleEndColor); dotPrimaryColor = array.getColor(R.styleable.LikeButton_dots_primary_color, 0); dotSecondaryColor = array.getColor(R.styleable.LikeButton_dots_secondary_color, 0); if (dotPrimaryColor != 0 && dotSecondaryColor != 0) { dotsView.setColors(dotPrimaryColor, dotSecondaryColor); } if (likeDrawable == null && unLikeDrawable == null) { if (currentIcon != null) { setLikeDrawableRes(currentIcon.getOnIconResourceId()); setUnlikeDrawableRes(currentIcon.getOffIconResourceId()); } else { currentIcon = parseIconType(IconType.Heart); setLikeDrawableRes(currentIcon.getOnIconResourceId()); setUnlikeDrawableRes(currentIcon.getOffIconResourceId()); } } setEnabled(array.getBoolean(R.styleable.LikeButton_enabled, true)); Boolean status = array.getBoolean(R.styleable.LikeButton_liked, false); setAnimationScaleFactor(array.getFloat(R.styleable.LikeButton_anim_scale_factor, 3)); setLiked(status); setOnClickListener(this); array.recycle(); }
/** * This set sets the colours that are used for the little dots that will be exploding once the * like button is clicked. * * @param primaryColor * @param secondaryColor */ public void setExplodingDotColorsRes(@ColorRes int primaryColor, @ColorRes int secondaryColor) { dotsView.setColors( ContextCompat.getColor(getContext(), primaryColor), ContextCompat.getColor(getContext(), secondaryColor)); }
/** * This triggers the entire functionality of the button such as icon changes, animations, * listeners etc. * * @param v */ @Override public void onClick(View v) { if (!isEnabled) return; if (isChecked) return; isChecked = !isChecked; icon.setImageDrawable(isChecked ? likeDrawable : unLikeDrawable); if (likeListener != null) { if (isChecked) { likeListener.liked(this); } else { likeListener.unLiked(this); } } if (animatorSet != null) { animatorSet.cancel(); } if (isChecked) { icon.animate().cancel(); icon.setScaleX(0); icon.setScaleY(0); circleView.setInnerCircleRadiusProgress(0); circleView.setOuterCircleRadiusProgress(0); dotsView.setCurrentProgress(0); animatorSet = new AnimatorSet(); ObjectAnimator outerCircleAnimator = ObjectAnimator.ofFloat(circleView, CircleView.OUTER_CIRCLE_RADIUS_PROGRESS, 0.1f, 1f); outerCircleAnimator.setDuration(250); outerCircleAnimator.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator innerCircleAnimator = ObjectAnimator.ofFloat(circleView, CircleView.INNER_CIRCLE_RADIUS_PROGRESS, 0.1f, 1f); innerCircleAnimator.setDuration(200); innerCircleAnimator.setStartDelay(200); innerCircleAnimator.setInterpolator(DECCELERATE_INTERPOLATOR); ObjectAnimator starScaleYAnimator = ObjectAnimator.ofFloat(icon, ImageView.SCALE_Y, 0.2f, 1f); starScaleYAnimator.setDuration(350); starScaleYAnimator.setStartDelay(250); starScaleYAnimator.setInterpolator(OVERSHOOT_INTERPOLATOR); ObjectAnimator starScaleXAnimator = ObjectAnimator.ofFloat(icon, ImageView.SCALE_X, 0.2f, 1f); starScaleXAnimator.setDuration(350); starScaleXAnimator.setStartDelay(250); starScaleXAnimator.setInterpolator(OVERSHOOT_INTERPOLATOR); ObjectAnimator dotsAnimator = ObjectAnimator.ofFloat(dotsView, DotsView.DOTS_PROGRESS, 0, 1f); dotsAnimator.setDuration(900); dotsAnimator.setStartDelay(50); dotsAnimator.setInterpolator(ACCELERATE_DECELERATE_INTERPOLATOR); animatorSet.playTogether( outerCircleAnimator, innerCircleAnimator, starScaleYAnimator, starScaleXAnimator, dotsAnimator); animatorSet.addListener( new AnimatorListenerAdapter() { @Override public void onAnimationCancel(Animator animation) { circleView.setInnerCircleRadiusProgress(0); circleView.setOuterCircleRadiusProgress(0); dotsView.setCurrentProgress(0); icon.setScaleX(1); icon.setScaleY(1); } }); animatorSet.start(); } }