安卓中級教程(7):pathbutton中的animation.java研究

來源:互聯網
上載者:User

標籤:

src/geniuz/myPathbutton/myAnimations.javapackage geniuz.myPathbutton;import java.util.ArrayList;import java.util.List;import com.nineoldandroids.animation.Animator;import com.nineoldandroids.animation.Animator.AnimatorListener;import com.nineoldandroids.view.ViewPropertyAnimator;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnticipateInterpolator;import android.view.animation.OvershootInterpolator;import android.view.animation.RotateAnimation;import android.view.animation.TranslateAnimation;import android.widget.LinearLayout;import android.widget.RelativeLayout.LayoutParams;import android.widget.RelativeLayout;import static com.nineoldandroids.view.ViewPropertyAnimator.animate;public class myAnimations {public final int R; // 半徑public static byte RIGHTBOTTOM = 1, CENTERBOTTOM = 2, LEFTBOTTOM = 3,LEFTCENTER = 4, LEFTTOP = 5, CENTERTOP = 6, RIGHTTOP = 7,RIGHTCENTER = 8;private int pc; // 位置代號private ViewGroup clayout; // 父laoyoutprivate final int amount; // 有幾多個按鈕private double fullangle = 180.0;// 在幾大嘅角度內排佈private byte xOri = 1, yOri = 1; // x、y值嘅方向,即系向上還是向下private boolean isOpen = false;// 記錄是已經開啟還是關閉private List<ViewPropertyAnimator> viewAnimators = new ArrayList<ViewPropertyAnimator>();/** * 構造函數 *  * @param comlayout *            包裹彈出按鈕嘅layout * @param poscode *            位置代號,分別對應RIGHTBOTTOM、CENTERBOTTOM、LEFTBOTTOM、LEFTCENTER、 *            LEFTTOP、CENTERTOP、RIGHTTOP、RIGHTCENTER * @param radius *            半徑 */public myAnimations(ViewGroup comlayout, int poscode, int radius) {this.pc = poscode;this.clayout = comlayout;this.amount = clayout.getChildCount();this.R = radius;// 初始化動畫,每個view對應一個animatorfor (int i = 0; i < amount; i++) {View childAt = clayout.getChildAt(i);ViewPropertyAnimator anim = animate(childAt);viewAnimators.add(anim);}if (poscode == RIGHTBOTTOM) { // 右下角fullangle = 90;xOri = -1;yOri = -1;} else if (poscode == CENTERBOTTOM) {// 中下fullangle = 180;xOri = -1;yOri = -1;} else if (poscode == LEFTBOTTOM) { // 左下角fullangle = 90;xOri = 1;yOri = -1;} else if (poscode == LEFTCENTER) { // 左中fullangle = 180;xOri = 1;yOri = -1;} else if (poscode == LEFTTOP) { // 左上方fullangle = 90;xOri = 1;yOri = 1;} else if (poscode == CENTERTOP) { // 中上fullangle = 180;xOri = -1;yOri = 1;} else if (poscode == RIGHTTOP) { // 右上方fullangle = 90;xOri = -1;yOri = 1;} else if (poscode == RIGHTCENTER) { // 右中fullangle = 180;xOri = -1;yOri = -1;}}private class AnimListener implements AnimatorListener {private View target;public AnimListener(View _target) {target = _target;}@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {if (!isOpen) {target.setVisibility(View.INVISIBLE);}}@Overridepublic void onAnimationCancel(Animator animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animator animation) {// TODO Auto-generated method stub}}/** * 彈幾個按鈕出嚟 *  * @param durationMillis *            用幾多時間 */public void startAnimationsIn(int durationMillis) {isOpen = true;for (int i = 0; i < clayout.getChildCount(); i++) {final LinearLayout inoutimagebutton = (LinearLayout) clayout.getChildAt(i);double offangle = fullangle / (amount - 1);final double deltaY, deltaX;if (pc == LEFTCENTER || pc == RIGHTCENTER) {deltaX = Math.sin(offangle * i * Math.PI / 180) * R;deltaY = Math.cos(offangle * i * Math.PI / 180) * R;} else {deltaY = Math.sin(offangle * i * Math.PI / 180) * R;deltaX = Math.cos(offangle * i * Math.PI / 180) * R;}ViewPropertyAnimator viewPropertyAnimator = viewAnimators.get(i);viewPropertyAnimator.setListener(null);inoutimagebutton.setVisibility(View.VISIBLE);viewPropertyAnimator.x((float) (inoutimagebutton.getLeft() + xOri * deltaX)).y((float) (inoutimagebutton.getTop() + yOri * deltaY));}}/** * 收埋幾個按鈕入去 *  * @param durationMillis *            用幾多時間 */public void startAnimationsOut(int durationMillis) {isOpen = false;for (int i = 0; i < clayout.getChildCount(); i++) {final LinearLayout inoutimagebutton = (LinearLayout) clayout.getChildAt(i);ViewPropertyAnimator viewPropertyAnimator = viewAnimators.get(i);viewPropertyAnimator.setListener(null);viewPropertyAnimator.x((float) inoutimagebutton.getLeft()).y((float) inoutimagebutton.getTop());viewPropertyAnimator.setListener(new AnimListener(inoutimagebutton));}}/** * 獲取位置代碼(其實貌似都冇乜用) */public int getPosCode() {return this.pc;}/** * 自轉函數(原本就有嘅靜態函數,未實體化都可以調用) *  * @param fromDegrees *            從幾多度 * @param toDegrees *            到幾多度 * @param durationMillis *            轉幾耐 */public static Animation getRotateAnimation(float fromDegrees,float toDegrees, int durationMillis) {RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);rotate.setDuration(durationMillis);rotate.setFillAfter(true);return rotate;}} Desktop version

  

安卓中級教程(7):pathbutton中的animation.java研究

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.