android 利用屬性動畫實現酷炫的圓形菜單

來源:互聯網
上載者:User

android 利用屬性動畫實現酷炫的圓形菜單

廢話不哆嗦,直接上代碼,反正也差不多沒人看,就自己記錄下咯

 

 

package com.example.testroundmenu;import android.animation.ObjectAnimator;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.RotateAnimation;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;public class Main extends Activity {//下標分別是從下到上,從左至右private RelativeLayout rl_1, rl_2, rl_3;private ImageView iv_rl1_home, iv_rl2_1, iv_rl2_2, iv_rl2_3, iv_rl3_1,iv_rl3_2, iv_rl3_3, iv_rl3_4, iv_rl3_5, iv_rl3_6, iv_rl3_7;private boolean isMidleMenuShow = true;private boolean isOutMenuShow = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);initView();}private void initView() {MyListener listener = new MyListener();// 初始化控制項,簡單起見,並沒有直接在xml中聲明點擊函數rl_1 = (RelativeLayout) findViewById(R.id.rl_1);rl_2 = (RelativeLayout) findViewById(R.id.rl_2);rl_3 = (RelativeLayout) findViewById(R.id.rl_3);iv_rl1_home = (ImageView) findViewById(R.id.iv_home);iv_rl2_1 = (ImageView) findViewById(R.id.iv_search);iv_rl2_2 = (ImageView) findViewById(R.id.iv_menu);iv_rl2_3 = (ImageView) findViewById(R.id.iv_myyouku);iv_rl3_1 = (ImageView) findViewById(R.id.iv_out_1);iv_rl3_2 = (ImageView) findViewById(R.id.iv_out_2);iv_rl3_3 = (ImageView) findViewById(R.id.iv_out_3);iv_rl3_4 = (ImageView) findViewById(R.id.iv_out_4);iv_rl3_5 = (ImageView) findViewById(R.id.iv_out_5);iv_rl3_6 = (ImageView) findViewById(R.id.iv_out_6);iv_rl3_7 = (ImageView) findViewById(R.id.iv_out_7);// 添加點擊監聽iv_rl1_home.setOnClickListener(listener);iv_rl2_1.setOnClickListener(listener);iv_rl2_2.setOnClickListener(listener);iv_rl2_3.setOnClickListener(listener);iv_rl3_1.setOnClickListener(listener);iv_rl3_2.setOnClickListener(listener);iv_rl3_3.setOnClickListener(listener);iv_rl3_4.setOnClickListener(listener);iv_rl3_5.setOnClickListener(listener);iv_rl3_6.setOnClickListener(listener);iv_rl3_7.setOnClickListener(listener);}/** * @author kk_imgod imageview 的監聽函數 */public class MyListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.iv_home:if (isMidleMenuShow) {isMidleMenuShow = false;animatrHiden(rl_2, 500);//如果最外層的菜單還在顯示的話,那就一起隱藏if(isOutMenuShow) {animatrHiden(rl_3, 1000);isOutMenuShow = false;} } else {isMidleMenuShow = true;animatrShow(rl_2, 500);}showToast(HOME);break;case R.id.iv_menu:showToast(我的菜單);if(isOutMenuShow) {animatrHiden(rl_3, 1000);isOutMenuShow = false;} else {animatrShow(rl_3, 1000);isOutMenuShow = true;}break;case R.id.iv_search:showToast(我的搜尋);break;case R.id.iv_myyouku:showToast(我的優酷);break;case R.id.iv_out_1:showToast(第一個);break;case R.id.iv_out_2:showToast(第二個);break;case R.id.iv_out_3:showToast(第三個);break;case R.id.iv_out_4:showToast(第四個);break;case R.id.iv_out_5:showToast(第五個);break;case R.id.iv_out_6:showToast(第六個);break;case R.id.iv_out_7:showToast(第七個);break;default:break;}}}/** * @param text *            顯示多士的文本 */public void showToast(String text) {Toast.makeText(Main.this, text, Toast.LENGTH_SHORT).show();}/** * 讓視圖動畫顯示出來,180-360度 */public void animatrShow(View view, long durction) {ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,rotation, 180f, 360f );//設定旋轉點view.setPivotX(view.getWidth() / 2);view.setPivotY(view.getHeight());objectAnimator.setDuration(durction).start();}/** * 讓視圖動畫隱藏起來 0-180度 */public void animatrHiden(View view, long durction) {ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,rotation, 0f, 180f);//設定動畫的旋轉點view.setPivotX(view.getWidth() / 2);view.setPivotY(view.getHeight());objectAnimator.setDuration(durction).start();}//下面的這種隱藏和顯示動畫的效果是傳統動畫,沒有互動效果/** * @param view 0-180讓視圖隱藏 */public static void startAnimout(RelativeLayout view) {// TODO Auto-generated method stubRotateAnimation rotateAnimation = new RotateAnimation(0, 180,view.getWidth() / 2, view.getHeight());rotateAnimation.setDuration(500);rotateAnimation.setFillAfter(true);view.startAnimation(rotateAnimation);}/** * @param view 180-360讓視圖出現 */public static void startAnimin(RelativeLayout view) {// TODO Auto-generated method stubRotateAnimation rotateAnimation = new RotateAnimation(180, 360,view.getWidth() / 2, view.getHeight());rotateAnimation.setDuration(500);rotateAnimation.setFillAfter(true);view.startAnimation(rotateAnimation);}}
關鍵性代碼:設定旋轉點

 

 

//設定旋轉點view.setPivotX(view.getWidth() / 2);view.setPivotY(view.getHeight());
 

 

聯繫我們

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