Because the project needs a floating round button in the bottom right corner, and by adding a user-friendly experience, click on the button and start to turn, again click and stop the rotation,
Implementation method:
First, add a Fabimagebutton.java class file
Package Com.example.test;import Android.annotation.suppresslint;import Android.content.context;import Android.content.res.resources;import Android.content.res.typedarray;import Android.graphics.color;import Android.graphics.lineargradient;import Android.graphics.shader;import Android.graphics.drawable.drawable;import Android.graphics.drawable.layerdrawable;import Android.graphics.drawable.shapedrawable;import Android.graphics.drawable.statelistdrawable;import Android.graphics.drawable.shapes.ovalshape;import Android.util.attributeset;import Android.view.animation.animation;import android.view.animation.AnimationUtils; Import Android.view.animation.linearinterpolator;import Android.widget.imagebutton;public class FABImageButton Extends imagebutton{private Context content; Private Animation Operatinganim; Private Boolean isanimplay = false; private int bgColor; private int bgcolorpressed; Public Fabimagebutton (Context context) {super (context); this.content = context;init (null);} Public Fabimagebutton (context context, AttributeSet Attrs) {Super (context, attrs); this.content = Context;init (attrs);} Public Fabimagebutton (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); This.content = Context;init (attrs);} @SuppressLint ("Newapi") public Fabimagebutton (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {Super (context, Attrs, defstyleattr, defstyleres); This.content = context; Init (attrs); }/** * Play/Pause Rotation animation * */public void Playanim () {if (!isanimplay) {startanimation (Operatinganim); Isanimpl ay = true; }else{clearanimation (); isanimplay = false; }} private drawable Createbutton (int color) {OvalShape oshape = new OvalShape (); shapedrawable sd = new shapedrawable (oshape); Setwillnotdraw (FALSE); Sd.getpaint (). SetColor (color); Shapedrawable SD1 = new shapedrawable (oshape); Sd1.setshAderfactory (New Shapedrawable.shaderfactory () {@Override public Shader resize (int width, int height {lineargradient lg = new LinearGradient (0,0,0, height, new int[] { Color.White, Color.gray, Color.dkgray, Color.Black}, NULL, Shader.TileMode.REPEAT); return LG; } }); Layerdrawable ld = new layerdrawable (new drawable[] {SD1, SD}); Ld.setlayerinset (0, 0, 0, 0, 0); return LD; } @SuppressLint ("Newapi") private void init (AttributeSet attrset) {//Load animation Operatinganim = Animationutils . loadanimation (content, R.anim.tip); Linearinterpolator lin = new Linearinterpolator (); Operatinganim.setinterpolator (Lin); Resources.theme Theme = Content.gettheme (); TypedArray arr = Theme.obtainstyledattributes (atTrset, R.styleable.fab, 0, 0); try {setbgcolor (Arr.getcolor (R.styleable.fab_bg_color, Color.Black)); Setbgcolorpressed (Arr.getcolor (r.styleable.fab_bg_color_pressed, Color.gray)); Statelistdrawable SLD = new statelistdrawable (); Sld.addstate (new int[] {Android. R.attr.state_pressed}, Createbutton (bgcolorpressed)); Sld.addstate (new int[] {}, Createbutton (BgColor)); SetBackground (SLD); } catch (Throwable t) {} finally {arr.recycle (); }} public void Setbgcolor (int color) {this.bgcolor = color; } public void setbgcolorpressed (int color) {this.bgcolorpressed = color; }}
2. Under the Values folder, define a resource file named Stylable.xml.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "FAB" > <!- -Background Color-- <attr name= "Bg_color" format= "color|reference"/> <attr name= "Bg_color_ Pressed "format=" color|reference "/> </declare-styleable></resources>
3. Create a rotated animation effect set up a Anim folder under the Res folder and create an XML file named Tip.xml under the folder
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:duration= "+" //rotational speed android:fromdegrees= "0" android:pivotx= "50%" android:pivoty= "50%" android:repeatcount= "-1" android:todegrees= "359"/></set>
4.layout inside Add Fabimagebutton
<com.example.test.fabimagebuttonandroid:id= "@+id/fabbtn" android:layout_width= "30DP" android:layout_ height= "30DP" android:src= "@android:d rawable/ic_input_add" android:layout_alignparentbottom= "true" android:layout_alignparentright= "true" android:layout_marginright= "16DP" android:layout_ Marginbottom= "16DP" android:tint= "@android: Color/white"/>
5.MainActivity file
public class Mainactivity extends Activity {Fabimagebutton fabbtn; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ListView LV = (ListView) Findviewbyid (R.ID.LV1); string[] data = new STRING[100]; for (int i=0; i < data.length; i++) data[i] = "floating button" + i; arrayadapter<string> AADPT = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, data); Lv.setadapter (AADPT); FABBTN = (Fabimagebutton) Findviewbyid (R.ID.FABBTN); Fabbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {fabbtn. Playanim ();}} );} }
End...
Android-click to rotate the custom bottom right corner of the floating Fabimagebutton button