|
20 | 20 | import android.content.res.ColorStateList; |
21 | 21 | import android.content.res.Resources; |
22 | 22 | import android.content.res.TypedArray; |
| 23 | +import android.graphics.Bitmap; |
| 24 | +import android.graphics.BitmapFactory; |
23 | 25 | import android.graphics.Color; |
24 | 26 | import android.graphics.Typeface; |
| 27 | +import android.graphics.drawable.BitmapDrawable; |
25 | 28 | import android.graphics.drawable.Drawable; |
26 | 29 | import android.graphics.drawable.GradientDrawable; |
27 | 30 | import android.os.Build; |
28 | 31 | import android.support.annotation.ColorRes; |
| 32 | +import android.support.annotation.DrawableRes; |
29 | 33 | import android.support.annotation.NonNull; |
30 | 34 | import android.support.v7.widget.RecyclerView; |
31 | 35 | import android.util.AttributeSet; |
@@ -168,6 +172,13 @@ public int getDuration() { |
168 | 172 | private int mCornerRadius; |
169 | 173 | private ColorStateList mTextColor; |
170 | 174 | private Typeface mTypeface = Typeface.DEFAULT; |
| 175 | + |
| 176 | + private Drawable iconClose; |
| 177 | + private Drawable iconOpen; |
| 178 | + private boolean isDoubleIconSet; |
| 179 | + private boolean isOneIconSet; |
| 180 | + |
| 181 | + private OnClickListener clickListener; |
171 | 182 | /** |
172 | 183 | * |
173 | 184 | * @param context |
@@ -209,13 +220,25 @@ private void init(Context context) { |
209 | 220 | @Override |
210 | 221 | public void onClick(View view) { |
211 | 222 | if(menuAnim && mArcLayout.isAnimDone()){ |
| 223 | + if(clickListener != null){ |
| 224 | + clickListener.onClick(fabMenu); |
| 225 | + } |
212 | 226 | ViewAnim.shrinkExpandAnimation(fabMenu); |
| 227 | + |
213 | 228 | if(isMenuClicked){ |
214 | 229 | isMenuClicked = false; |
215 | | - ViewAnim.rotateAnimation(mIcon, false); |
| 230 | + if(!isDoubleIconSet && !isOneIconSet){ |
| 231 | + ViewAnim.rotateAnimation(mIcon, false); |
| 232 | + }else if(isDoubleIconSet && !isOneIconSet){ |
| 233 | + fabMenu.setIcon(iconClose, true); |
| 234 | + } |
216 | 235 | }else{ |
217 | 236 | isMenuClicked = true; |
218 | | - ViewAnim.rotateAnimation(mIcon, true); |
| 237 | + if(!isDoubleIconSet && !isOneIconSet){ |
| 238 | + ViewAnim.rotateAnimation(mIcon, true); |
| 239 | + }else if(isDoubleIconSet && !isOneIconSet){ |
| 240 | + fabMenu.setIcon(iconOpen, true); |
| 241 | + } |
219 | 242 | } |
220 | 243 | } |
221 | 244 | if(mArcLayout.isAnimDone()){ |
@@ -420,7 +443,11 @@ public void run() { |
420 | 443 | } |
421 | 444 | if(isMenuClicked){ |
422 | 445 | isMenuClicked = false; |
423 | | - ViewAnim.rotateAnimation(mIcon, false); |
| 446 | + if(!isDoubleIconSet && !isOneIconSet){ |
| 447 | + ViewAnim.rotateAnimation(mIcon, false); |
| 448 | + }else if(isDoubleIconSet && !isOneIconSet){ |
| 449 | + fabMenu.setIcon(iconClose, true); |
| 450 | + } |
424 | 451 | } |
425 | 452 | mArcLayout.invalidate(); |
426 | 453 | mArcLayout.setExpandDone(false); |
@@ -586,6 +613,9 @@ public boolean isClose(){ |
586 | 613 | return false; |
587 | 614 | } |
588 | 615 |
|
| 616 | + public void setOnClickListener(OnClickListener listener){ |
| 617 | + clickListener = listener; |
| 618 | + } |
589 | 619 | /** |
590 | 620 | * |
591 | 621 | * @return menu status, true = open, false = close |
@@ -641,8 +671,58 @@ public void setChildSize(int size) { |
641 | 671 |
|
642 | 672 | /** |
643 | 673 | * |
644 | | - * @param colorResId |
645 | 674 | */ |
| 675 | + public void setDefaultIcon(){ |
| 676 | + isDoubleIconSet = false; |
| 677 | + isOneIconSet = true; |
| 678 | + mIcon.setVisibility(VISIBLE); |
| 679 | + } |
| 680 | + |
| 681 | + public void setIcon(@DrawableRes int iconClose, @DrawableRes int iconOpen) { |
| 682 | + try{ |
| 683 | + Bitmap b = new BitmapFactory().decodeResource(getResources(), iconClose); |
| 684 | + Drawable c1 = new BitmapDrawable(getResources(), b); |
| 685 | + b = new BitmapFactory().decodeResource(getResources(), iconOpen); |
| 686 | + Drawable c2 = new BitmapDrawable(getResources(), b); |
| 687 | + setIcon(c1, c2); |
| 688 | + }catch (Exception e){ |
| 689 | + e.printStackTrace(); |
| 690 | + isDoubleIconSet = false; |
| 691 | + } |
| 692 | + } |
| 693 | + |
| 694 | + public void setIcon(Drawable iconClose, Drawable iconOpen) { |
| 695 | + if(iconClose != null && iconOpen != null){ |
| 696 | + this.iconClose = iconClose; |
| 697 | + this.iconOpen = iconOpen; |
| 698 | + fabMenu.setIcon(this.iconClose); |
| 699 | + mIcon.setVisibility(GONE); |
| 700 | + isDoubleIconSet = true; |
| 701 | + isOneIconSet = false; |
| 702 | + } |
| 703 | + } |
| 704 | + |
| 705 | + public void setIcon(@DrawableRes int iconClose) { |
| 706 | + try{ |
| 707 | + Bitmap b = new BitmapFactory().decodeResource(getResources(), iconClose); |
| 708 | + Drawable c = new BitmapDrawable(getResources(), b); |
| 709 | + setIcon(c); |
| 710 | + }catch (Exception e){ |
| 711 | + e.printStackTrace(); |
| 712 | + isOneIconSet = false; |
| 713 | + } |
| 714 | + } |
| 715 | + |
| 716 | + public void setIcon(Drawable iconClose) { |
| 717 | + if(iconClose != null){ |
| 718 | + this.iconClose = iconClose; |
| 719 | + fabMenu.setIcon(this.iconClose); |
| 720 | + mIcon.setVisibility(GONE); |
| 721 | + isDoubleIconSet = false; |
| 722 | + isOneIconSet = true; |
| 723 | + } |
| 724 | + } |
| 725 | + |
646 | 726 | public void setColorNormalResId(@ColorRes int colorResId) { |
647 | 727 | setColorNormal(getColor(colorResId)); |
648 | 728 | } |
|
0 commit comments