Android: an animation of the pop-up menu (2)

Source: Internet
Author: User

Android: an animation of the pop-up menu (2)

If you create a pop-up control, you can add the view:

Write class SatelliteMenu extends FrameLayout

private void init(Context context, AttributeSet attrs, int defStyle) {inflate(context, R.layout.sat_main, this);imgMain = (ImageView) findViewById(R.id.sat_main);if(attrs != null){            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SatelliteMenu, defStyle, 0);satelliteDistance = typedArray.getDimensionPixelSize(R.styleable.SatelliteMenu_satelliteDistance, DEFAULT_SATELLITE_DISTANCE);totalSpacingDegree = typedArray.getFloat(R.styleable.SatelliteMenu_totalSpacingDegree, DEFAULT_TOTAL_SPACING_DEGREES);closeItemsOnClick = typedArray.getBoolean(R.styleable.SatelliteMenu_closeOnClick, DEFAULT_CLOSE_ON_CLICK);expandDuration = typedArray.getInt(R.styleable.SatelliteMenu_expandDuration, DEFAULT_EXPAND_DURATION);//float satelliteDistance = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 170, getResources().getDisplayMetrics());typedArray.recycle();}mainRotateLeft = SatelliteAnimationCreator.createMainButtonAnimation(context);mainRotateRight = SatelliteAnimationCreator.createMainButtonInverseAnimation(context);Animation.AnimationListener plusAnimationListener = new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {plusAnimationActive.set(false);}};mainRotateLeft.setAnimationListener(plusAnimationListener);mainRotateRight.setAnimationListener(plusAnimationListener);imgMain.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SatelliteMenu.this.onClick();}});internalItemClickListener = new InternalSatelliteOnClickListener(this);}

 
         
          
 
 
  
 

Then write the logic for adding an Item:

Public void addItems (List
 
  
Items) {menuItems. addAll (items); this. removeView (imgMain); TextView tmpView = new TextView (getContext (); tmpView. setLayoutParams (new FrameLayout. layoutParams (0, 0); float [] degrees = getDegrees (menuItems. size (); int index = 0; for (SatelliteMenuItem menuItem: menuItems) {int finalX = SatelliteAnimationCreator. getTranslateX (degrees [index], satelliteDistance); int finalY = SatelliteAnimationCreator. getTranslateY (degrees [index], satelliteDistance); ImageView itemView = (ImageView) LayoutInflater. from (getContext ()). inflate (R. layout. sat_item_cr, this, false); ImageView cloneView = (ImageView) LayoutInflater. from (getContext ()). inflate (R. layout. sat_item_cr, this, false); itemView. setTag (menuItem. getId (); cloneView. setVisibility (View. GONE); itemView. setVisibility (View. GONE); cloneView. setOnClickListener (internalItemClickListener); cloneView. setTag (Integer. valueOf (menuItem. getId (); FrameLayout. layoutParams layoutParams = getLayoutParams (cloneView); layoutParams. bottomMargin = Math. abs (finalY); layoutParams. leftMargin = Math. abs (finalX); cloneView. setLayoutParams (layoutParams );
  // Put cloneView at the end of the itemview animation.If (menuItem. getImgResourceId ()> 0) {itemView. setImageResource (menuItem. getImgResourceId (); cloneView. setImageResource (menuItem. getImgResourceId ();} else if (menuItem. getImgDrawable ()! = Null) {itemView. setImageDrawable (menuItem. getImgDrawable (); cloneView. setImageDrawable (menuItem. getImgDrawable ();} Animation itemOut = SatelliteAnimationCreator. createItemOutAnimation (getContext (), index, expandDuration, finalX, finalY); Animation itemIn = SatelliteAnimationCreator. createItemInAnimation (getContext (), index, expandDuration, finalX, finalY); Animation itemClick = SatelliteAnimationCreator. createItemClickAnimation (getContext (); menuItem. setView (itemView); menuItem. setCloneView (cloneView); menuItem. setInAnimation (itemIn); menuItem. setOutAnimation (itemOut); menuItem. setClickAnimation (itemClick); menuItem. setFinalX (finalX); menuItem. setFinalY (finalY); itemIn. setAnimationListener (new SatelliteAnimationListener (itemView, true, viewToItemMap); itemOut. setAnimationListener (new SatelliteAnimationListener (itemView, false, viewToItemMap); itemClick. setAnimationListener (new SatelliteItemClickAnimationListener (this, menuItem. getId (); this. addView (itemView); this. addView (cloneView); viewToItemMap. put (itemView, menuItem); viewToItemMap. put (cloneView, menuItem); index ++;} this. addView (imgMain );}
 

Listener:

private static class SatelliteAnimationListener implements Animation.AnimationListener {private WeakReference
 
   viewRef;private boolean isInAnimation;private Map
  
    viewToItemMap;public SatelliteAnimationListener(View view, boolean isIn, Map
   
     viewToItemMap) {this.viewRef = new WeakReference
    
     (view);this.isInAnimation = isIn;this.viewToItemMap = viewToItemMap;}@Overridepublic void onAnimationStart(Animation animation) {if (viewRef != null) {View view = viewRef.get();if (view != null) {SatelliteMenuItem menuItem = viewToItemMap.get(view);if (isInAnimation) {menuItem.getView().setVisibility(View.VISIBLE);menuItem.getCloneView().setVisibility(View.GONE);} else {menuItem.getCloneView().setVisibility(View.GONE);menuItem.getView().setVisibility(View.VISIBLE);}}}}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (viewRef != null) {View view = viewRef.get();if (view != null) {SatelliteMenuItem menuItem = viewToItemMap.get(view);if (isInAnimation) {menuItem.getView().setVisibility(View.GONE);menuItem.getCloneView().setVisibility(View.GONE);} else {menuItem.getCloneView().setVisibility(View.VISIBLE);menuItem.getView().setVisibility(View.GONE);}}}}}
    
   
  
 

OnMeature must be rewritten:

private void recalculateMeasureDiff() {int itemWidth = 0;if (menuItems.size() > 0) {itemWidth = menuItems.get(0).getView().getWidth();}measureDiff = Float.valueOf(satelliteDistance * 0.2f).intValue()+ itemWidth;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);recalculateMeasureDiff();int totalHeight = imgMain.getHeight() + satelliteDistance + measureDiff;int totalWidth = imgMain.getWidth() + satelliteDistance + measureDiff;System.out.println("====totalWidth="+totalWidth+"height="+totalHeight+"measureDiff="+measureDiff+"imgMain.getWidth()="+imgMain.getWidth());setMeasuredDimension(totalWidth, totalHeight);}

Save and restore activity status:

@Overrideprotected Parcelable onSaveInstanceState() {Parcelable superState = super.onSaveInstanceState();SavedState ss = new SavedState(superState);ss.rotated = rotated;ss.totalSpacingDegree = totalSpacingDegree;ss.satelliteDistance = satelliteDistance;ss.measureDiff = measureDiff;ss.expandDuration = expandDuration;ss.closeItemsOnClick = closeItemsOnClick;return ss;}@Overrideprotected void onRestoreInstanceState(Parcelable state) {SavedState ss = (SavedState) state;rotated = ss.rotated;totalSpacingDegree = ss.totalSpacingDegree;satelliteDistance = ss.satelliteDistance;measureDiff = ss.measureDiff;expandDuration = ss.expandDuration;closeItemsOnClick = ss.closeItemsOnClick;super.onRestoreInstanceState(ss.getSuperState());}static class SavedState extends BaseSavedState {boolean rotated;private float totalSpacingDegree;private int satelliteDistance;private int measureDiff;private int expandDuration;private boolean closeItemsOnClick;SavedState(Parcelable superState) {super(superState);}public SavedState(Parcel in) {super(in);rotated = Boolean.valueOf(in.readString());totalSpacingDegree = in.readFloat();satelliteDistance = in.readInt();measureDiff = in.readInt();expandDuration = in.readInt();closeItemsOnClick = Boolean.valueOf(in.readString());}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel out, int flags) {out.writeString(Boolean.toString(rotated));out.writeFloat(totalSpacingDegree);out.writeInt(satelliteDistance);out.writeInt(measureDiff);out.writeInt(expandDuration);out.writeString(Boolean.toString(closeItemsOnClick));}public static final Parcelable.Creator
 
   CREATOR = new Parcelable.Creator
  
   () {public SavedState createFromParcel(Parcel in) {return new SavedState(in);}public SavedState[] newArray(int size) {return new SavedState[size];}};}
  
 


:




Code: http://download.csdn.net/detail/baidu_nod/7731115

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.