日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

仿扇形菜單動畫效果

系統 1693 0
記得在eoe上有人發過,但代碼質量不好。我重寫了一下,抽成了控件。但沒有經過各種控件的相容性測試,如果和其他控件的相容性不好,就直接在activity中寫代碼吧,應該差不多的。
我用的是平板,所以效果還行,不知道手機如何。

仿扇形菜單動畫效果

仿扇形菜單動畫效果
代碼:
    
package com.ql.view;

import android.R.anim;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.ql.app.R;

public class AnimButtons extends RelativeLayout{

	private Context context;
	private int leftMargin=0,bottomMargin=0;
	private final int buttonWidth=58;//圖片寬高
	private final int r=180;//半徑
	private final int maxTimeSpent=200;//最長動畫耗時
	private final int minTimeSpent=80;//最短動畫耗時
	private int intervalTimeSpent;//每相鄰2個的時間間隔
	private Button[] btns;
	private Button btn_menu;
	private RelativeLayout.LayoutParams params;
	private boolean isOpen = false;//是否菜單打開狀態
	private float angle;//每個按鈕之間的夾角
	public AnimButtons(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context=context;
	}
	public AnimButtons(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context=context;
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		View view=LayoutInflater.from(context).inflate(R.layout.anim_buttons, this);
		
		initButtons(view);
		
	}

	private void initButtons(View view){
		// TODO Auto-generated method stub
		//6個按鈕,具體視情況而定
		btns=new Button[6];
		btns[0] = (Button) view.findViewById(R.id.btn_camera);
		btns[1] = (Button) view.findViewById(R.id.btn_with);
		btns[2] = (Button) view.findViewById(R.id.btn_place);
		btns[3] = (Button) view.findViewById(R.id.btn_music);
		btns[4] = (Button) view.findViewById(R.id.btn_thought);
		btns[5] = (Button) view.findViewById(R.id.btn_sleep);
		btn_menu = (Button) view.findViewById(R.id.btn_menu);
		
		leftMargin=((RelativeLayout.LayoutParams)(btn_menu.getLayoutParams())).leftMargin;
		bottomMargin=((RelativeLayout.LayoutParams)(btn_menu.getLayoutParams())).bottomMargin;
		
		for(int i=0;i<btns.length;i++){
			btns[i].setLayoutParams(btn_menu.getLayoutParams());//初始化的時候按鈕都重合
			btns[i].setTag(String.valueOf(i));
			btns[i].setOnClickListener(clickListener);
		}
		
		intervalTimeSpent=(maxTimeSpent-minTimeSpent)/btns.length;//20
		angle=(float)Math.PI/(2*(btns.length-1));
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		final int bottomMargins=this.getMeasuredHeight()-buttonWidth-bottomMargin;
//		Log.i("tag", "bottomMargins====="+bottomMargins);
		btn_menu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub					
				if(!isOpen){
					isOpen = true;
//					btn_menu.startAnimation(animRotate(-45.0f, 0.5f, 0.45f));
					for(int i=0;i<btns.length;i++){
						float xLenth=(float)(r*Math.sin(i*angle));
						float yLenth=(float)(r*Math.cos(i*angle));
//						Log.i("tag", "xLenth======"+xLenth+",yLenth======"+yLenth);
						btns[i].startAnimation(animTranslate(xLenth, -yLenth, leftMargin+(int)xLenth, bottomMargins - (int)yLenth, btns[i], minTimeSpent+i*intervalTimeSpent));
					}
					
				}
				else{					
					isOpen = false;
//					btn_menu.startAnimation(animRotate(90.0f, 0.5f, 0.45f));
					for(int i=0;i<btns.length;i++){
						float xLenth=(float)(r*Math.sin(i*angle));
						float yLenth=(float)(r*Math.cos(i*angle));
//						Log.i("tag", "xLenth======"+xLenth+",yLenth======"+yLenth);
						btns[i].startAnimation(animTranslate(-xLenth, yLenth, leftMargin, bottomMargins, btns[i], maxTimeSpent-i*intervalTimeSpent));
					}
				}
					
			}
		});
		
	}
	private Animation animScale(float toX, float toY){
		// TODO Auto-generated method stub
		Animation animation = new ScaleAnimation(1.0f, toX, 1.0f, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		animation.setInterpolator(context, anim.accelerate_decelerate_interpolator);
		animation.setDuration(400);
		animation.setFillAfter(false);
		return animation;
		
	}
	
	private Animation animRotate(float toDegrees, float pivotXValue, float pivotYValue){
		// TODO Auto-generated method stub
		final Animation animation = new RotateAnimation(0, toDegrees, Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF, pivotYValue);
		animation.setAnimationListener(new AnimationListener(){
			
			@Override
			public void onAnimationStart(Animation animation){
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation){
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation){
				// TODO Auto-generated method stub
				animation.setFillAfter(true);
			}
		});
		return animation;
	}
	
	
	private Animation animTranslate(float toX, float toY, final int lastX, final int lastY,
			final Button button, long durationMillis){
		// TODO Auto-generated method stub
		Animation animation = new TranslateAnimation(0, toX, 0, toY);				
		animation.setAnimationListener(new AnimationListener(){
						
			@Override
			public void onAnimationStart(Animation animation){
				// TODO Auto-generated method stub
								
			}
						
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
							
			}
						
			@Override
			public void onAnimationEnd(Animation animation){
				// TODO Auto-generated method stub
				params = new RelativeLayout.LayoutParams(0, 0);
				params.height = buttonWidth;
				params.width = buttonWidth;											
				params.setMargins(lastX, lastY, 0, 0);
				button.setLayoutParams(params);
				button.clearAnimation();
						
			}
		});																								
		animation.setDuration(durationMillis);
		return animation;
	}
	
	View.OnClickListener clickListener=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int selectedItem=Integer.parseInt((String)v.getTag());
			for(int i=0;i<btns.length;i++){
				if(i==selectedItem){
					btns[i].startAnimation(animScale(2.0f, 2.0f));
				}else{
					btns[i].startAnimation(animScale(0.0f, 0.0f));
				}
			}
			if(onButtonClickListener!=null){
				onButtonClickListener.onButtonClick(v, selectedItem);
			}
		}
		
	};
	
	public boolean isOpen(){
		return isOpen;
	}
	
	private OnButtonClickListener onButtonClickListener;
	public interface OnButtonClickListener{
		void onButtonClick(View v,int id);
	}
	public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener){
		this.onButtonClickListener=onButtonClickListener;
	}
}

  

布局anim_buttons.xml:
    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
	android:layout_height="fill_parent" 
	android:background="#FFF"
	>
	<Button android:id="@+id/btn_sleep"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_sleep"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_thought"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_thought"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_music"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_music"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_place"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_place"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_with"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_with"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_camera"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:background="@drawable/composer_camera"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		/>

	<Button android:id="@+id/btn_menu"
		android:layout_width="58dip" 
		android:layout_height="58dip"
		android:background="@drawable/friends_delete"
		android:layout_alignParentLeft="true"
		android:layout_alignParentBottom="true"
		android:layout_marginLeft="10dip"
		android:layout_marginBottom="10dip"
	/>
</RelativeLayout>

  


用法:
    
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        AnimButtons animButtons=(AnimButtons)findViewById(R.id.animButtons);
        animButtons.setOnButtonClickListener(new AnimButtons.OnButtonClickListener() {
			
			@Override
			public void onButtonClick(View v, int id) {
				// TODO Auto-generated method stub
				Log.i("tag", "id============="+id);
			}
		});
        
    }

  

    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
<!--  layout_width,layout_height最好是fill_parent參數 -->
	<com.ql.view.AnimButtons
	    android:id="@+id/animButtons"
	    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
	    />
</LinearLayout>

  


網上又找到一個相同效果的。呵呵。
http://www.cnblogs.com/mudoot/archive/2012/01/19/path_composer_menu.html

最后一個:
仿扇形菜單動畫效果

可定制的圓形的浮動菜單Android控件:CircularFloatingActionMenu
https://github.com/oguzbilgener/CircularFloatingActionMenu

仿扇形菜單動畫效果


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 衡阳市| 牟定县| 景泰县| 民和| 白玉县| 津市市| 进贤县| 鄱阳县| 兴安县| 龙岩市| 江永县| 平遥县| 司法| 五寨县| 图们市| 青州市| 枣庄市| 姜堰市| 宜兰市| 新乡市| 浦东新区| 文登市| 周口市| 虎林市| 乐亭县| 金堂县| 阿图什市| 新建县| 定襄县| 东平县| 桦甸市| 恩施市| 乐陵市| 株洲县| 兴山县| 郧西县| 东辽县| 汽车| 阜南县| 盖州市| 五峰|