Android自訂控制項3--優酷菜單執行動畫,android3--

來源:互聯網
上載者:User

Android自訂控制項3--優酷菜單執行動畫,android3--

在上篇文章中實現了優酷菜單的布局,本文接著實現動畫功能

本文地址:http://www.cnblogs.com/wuyudong/p/5914901.html,轉載請註明源地址。

建立動畫工具類AnimationUtils.java,代碼如下:

package com.wuyudong.youkumenu.utils;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.RelativeLayout;public class AnimationUtils {    // 旋轉出去的動畫    public static void rotateOutAnim(RelativeLayout layout, long delay) {        RotateAnimation ra = new RotateAnimation(                0f, -180f, //開始,結束的角度, 逆時針                Animation.RELATIVE_TO_SELF, 0.5f, //相對的x座標點(指定旋轉中心x值)                Animation.RELATIVE_TO_SELF, 1.0f); //相對的y座標點(指定旋轉中心y值)        ra.setDuration(500);        ra.setFillAfter(true);        ra.setStartOffset(delay);        layout.startAnimation(ra);            }        // 旋轉進來的動畫    public static void rotateInAnim(RelativeLayout layout) {        RotateAnimation ra = new RotateAnimation(                -180f, 0f,  //開始,結束的角度, 順時針                Animation.RELATIVE_TO_SELF, 0.5f, //相對的x座標點(指定旋轉中心x值)                Animation.RELATIVE_TO_SELF, 1.0f); //相對的y座標點(指定旋轉中心y值)        ra.setDuration(500);        ra.setFillAfter(true);        layout.startAnimation(ra);            }}

接著編寫邏輯部分代碼:

package com.wuyudong.youkumenu;import com.wuyudong.youkumenu.utils.AnimationUtils;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.RelativeLayout;public class MainActivity extends Activity implements OnClickListener {    private RelativeLayout rl_level1;    private RelativeLayout rl_level2;    private RelativeLayout rl_level3;    private boolean islevel1Display = true;    private boolean islevel2Display = true;    private boolean islevel3Display = true;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 初始化控制項        initViews();    }    private void initViews() {        // 添加點擊事件        findViewById(R.id.ib_home).setOnClickListener(this);        findViewById(R.id.ib_menu).setOnClickListener(this);        rl_level1 = (RelativeLayout) findViewById(R.id.rl_level1);        rl_level2 = (RelativeLayout) findViewById(R.id.rl_level2);        rl_level3 = (RelativeLayout) findViewById(R.id.rl_level3);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.ib_home:            if (islevel2Display) {                long delay = 200;                // 如果當前三級菜單已經顯示,先轉出去                if (islevel3Display) {                    AnimationUtils.rotateOutAnim(rl_level3, 0);                    islevel3Display = false;                    delay += 200;                }                 //如果當前二級菜單已經顯示,轉出去                AnimationUtils.rotateOutAnim(rl_level2, delay);                            } else {                //如果當前二級菜單沒有顯示,轉出來                AnimationUtils.rotateInAnim(rl_level2);            }            islevel2Display = !islevel2Display;            break;        case R.id.ib_menu:            if (islevel3Display) {                // 如果當前三級菜單已經顯示,轉出去                AnimationUtils.rotateOutAnim(rl_level3, 0);            } else {                AnimationUtils.rotateInAnim(rl_level3);            }            islevel3Display = !islevel3Display;            break;        default:            break;        }    }}

基本實現菜單的旋轉功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.