Objective
Previously wrote a android-implementation of the bottom pop-up popupwindow and let the background gradually darkened, introduce the use of handler dynamic change background transparency to achieve dimming effect. Now add a way to use animations to achieve the same effect.
valueanimator and interpolator
Today's protagonist is these two, about the Valueanimator and Interpolator (interpolation) of the concept of your own to add, here is mainly about how to use us here (because I also do not understand (cover the face)).
Effect
Not much different from the previous, just to demonstrate dimming, brightening the process ↓
Code
Animutil.java
/** * Animation Tool class * Updatelistener: During the animation process by adding this listener back and forth data * Endlistener: At the end of the animation through this listener to do some processing/public class Animutil {Priv
Ate valueanimator valueanimator;
Private Updatelistener Updatelistener;
Private Endlistener Endlistener;
Private long duration;
private float start;
private float end;
Private Interpolator interpolator = new Linearinterpolator ();
Public Animutil () {duration = 1000;//default animation often 1s start = 0.0f;
end = 1.0f; Interpolator = new Linearinterpolator ()//constant interpolation} public void setduration (int timelength) {duration = Timelen
Gth
public void Setvalueanimator (float start, float end, long duration) {This.start = start;
This.end = end;
this.duration = Duration;
public void Setinterpolator (Interpolator interpolator) {this.interpolator = Interpolator;
public void Startanimator () {if (valueanimator!= null) {valueanimator = null;
} valueanimator = Valueanimator.offloat (start, end); Valueanimator.setduration (duration);
Valueanimator.setinterpolator (Interpolator); Valueanimator.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void onanimation
Update (Valueanimator valueanimator) {if (Updatelistener = null) {return;
Float cur = (float) valueanimator.getanimatedvalue ();
Updatelistener.progress (cur);
}
}); Valueanimator.addlistener (New Animator.animatorlistener () {@Override public void Onanimationstart (animator
Imator) {} @Override public void Onanimationend (animator animator) {if (Endlistener = null) {
Return
} endlistener.endupdate (animator); @Override public void Onanimationcancel (animator animator) {} @Override public void onanimation
Repeat (animator animator) {}});
Valueanimator.start (); } public void Addupdatelistener (Updatelistener updatelistenER) {this.updatelistener = Updatelistener;
public void Addendlistner (Endlistener endlistener) {this.endlistener = Endlistener;
public interface Endlistener {void endupdate (animator animator);
public interface Updatelistener {void Progress (float progress);
}
}
Mainactivity.java
public class Mainactivity extends Appcompatactivity {private Animutil animutil;
Private float Bgalpha = 1f;
Private Boolean bright = false;
Popupwindow Popupwindow;
Button button;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Animutil = new Animutil ();
Button = (button) Findviewbyid (R.ID.BTN); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {Bott
Omwindow (button);
Togglebright ();
}
}); The private void Togglebright () {//Three parameters are: Long when the start value ends) The value of the entire animation callback comes from the 0.5f--1f animutil.setvalueanimator (0.5f,
1f, 350);
Animutil.addupdatelistener (New Animutil.updatelistener () {@Override public void Progress (float progress) { Here the system calculates the value of each callback based on the above three values, and we change the transparency based on this value Bgalpha = bright?
Progress: (1.5f-progress);//Three mesh operation, should be quite understood. BackgrounDalpha (Bgalpha)//change the background here so that you do not have to go through the handler to refresh.
}
});
Animutil.addendlistner (New Animutil.endlistener () {@Override public void endupdate (animator animator) {
At the end of an animation, the flip state bright =!bright;
}
});
Animutil.startanimator (); /*** * This method is used to change the transparency of the background to achieve "dimming" effect/private void Backgroundalpha (float bgalpha) {Windowmanager.layoutpar
AMS LP = GetWindow (). GetAttributes (); Lp.alpha = Bgalpha;
0.0-1.0 GetWindow (). SetAttributes (LP);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_DIM_BEHIND);
} void Bottomwindow (view view) {if (Popupwindow!= null && popupwindow.isshowing ()) {return;
} linearlayout layout = (linearlayout) getlayoutinflater (). Inflate (r.layout.window_popup, NULL); Popupwindow = new Popupwindow (layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CON
TENT);
When clicking on the blank, Hide Pop window popupwindow.setfocusable (true); Popupwindow.setbackgrounddrawable (New bitmapdrawable ());
Popupwindow.setanimationstyle (R.style.popupwindow);
int[] location = new INT[2];
View.getlocationonscreen (location); Popupwindow.showatlocation (view, Gravity.left |
Gravity.bottom, 0,-location[1]);
Popupwindow.setondismisslistener (New Popupwindow.ondismisslistener () {@Override public void Ondismiss () {
Togglebright ();
}
});
}
}
The code is simple and the comments are written.
Summary:
Compared with the previous way to write in the handler method, this feeling code is more concise, easier to use in many places, is a landfills bar, haha. If this article buried another pit, please correct me, thank you!
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.