android一个弹出菜单的动画(一)

先上效果图:




先写Layout文件:

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

   <ImageView
        android:id="@+id/sat_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sat_main" 
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        /> 
        
   
   <ImageView 
    android:id="@+id/sat_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    />
   
    <ImageView 
    android:id="@+id/clone_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    />
   
    
   
</RelativeLayout>

这3个ImageView都在屏幕的底部,clone_item需要固定在球弹起的最高位置:

初始化这3个imageView:

  sat_main = (ImageView)findViewById(R.id.sat_main);
		final ImageView itemView = (ImageView)findViewById(R.id.sat_item);
		final ImageView cloneView = (ImageView)findViewById(R.id.clone_item);
		cloneView.setImageResource(R.drawable.searchable_web);
		itemView.setImageResource(R.drawable.searchable_web);
		itemView.setVisibility(View.GONE);


初始化cloneView的位置:

//这个是使cloneview固定在leftmargin x bottomMargin y的地方
		RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams) cloneView.getLayoutParams();
		layoutParams.bottomMargin = Math.abs(y);
		layoutParams.leftMargin = Math.abs(x);
		cloneView.setLayoutParams(layoutParams);



点击按钮时候,按钮本身会旋转:

<?xml version="1.0" encoding="utf-8"?>
<rotate	xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"    
	android:fromDegrees="0"
	android:toDegrees="-135"        
	android:pivotX="50%"
	android:pivotY="50%"
	android:duration="300" 
	android:fillAfter="true"
	android:fillEnabled="true"/>

用以下方法得到球的最终位置:x坐标是distance*cos(角度),y是distance*sin(角度)

	//取得distance的cos(degree)
	public static int getTranslateX(float degree, int distance) {
		return Double.valueOf(distance * Math.cos(Math.toRadians(degree))).intValue();
	}
	
	public static int getTranslateY(float degree, int distance){
        return Double.valueOf(-1 * distance * Math.sin(Math.toRadians(degree))).intValue();
    }
	 


然后球弹起的动画:

	public static Animation createItemOutAnimation(Context context, int index, long expandDuration, int x, int y){
    	
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
        long alphaDuration = 60;
        if(expandDuration < 60){
        	alphaDuration = expandDuration / 4;
        }
        alphaAnimation.setDuration(alphaDuration);
        alphaAnimation.setStartOffset(0);

        //x和y是球弹到最高点的坐标
        TranslateAnimation translate = new TranslateAnimation(0, x, 0, y);
         
        translate.setStartOffset(0);
        translate.setDuration(expandDuration); 
        //OvershootInterpolator:表示向前甩一定值后再回到原来位置。
        translate.setInterpolator(context, R.anim.sat_item_overshoot_interpolator);
        
        RotateAnimation rotate = new RotateAnimation(0f, 360f, 
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        
        //AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
        //DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程
        rotate.setInterpolator(context, R.anim.sat_item_out_rotate_interpolator);
        
        long duration = 100;
        if(expandDuration <= 150){
            duration = expandDuration / 3;
        }
        
        rotate.setDuration(expandDuration-duration);
        rotate.setStartOffset(duration);        
        
        AnimationSet animationSet = new AnimationSet(false);
        animationSet.setFillAfter(false);
        animationSet.setFillBefore(true);
        animationSet.setFillEnabled(true);
                
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(rotate);
        animationSet.addAnimation(translate);
        
        animationSet.setStartOffset(30*index);
        
        return animationSet;
    }


这个动画弹到最高点后,我们得使itemview gone掉,cloneview visible


代码的位置:http://download.csdn.net/detail/baidu_nod/7722621

android一个弹出菜单的动画(一),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。