/** * 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(); }
/** * Sets one of the three icons that are bundled with the library. * * @param currentIconType */ public void setIcon(IconType currentIconType) { currentIcon = parseIconType(currentIconType); setLikeDrawableRes(currentIcon.getOnIconResourceId()); setUnlikeDrawableRes(currentIcon.getOffIconResourceId()); }