手记

安卓写一个球弹出的动画

效果图:


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="@android:color/white"
                android:orientation="vertical">

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


    <ImageView
        android:id="@+id/sat_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        />

    <ImageView
        android:id="@+id/clone_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        />


</RelativeLayout>

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

球弹出去的动画:

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);

       
       TranslateAnimation translate = new TranslateAnimation(0, x, 0, y);
        
       translate.setStartOffset(0);
       translate.setDuration(expandDuration); 
       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);
       

       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;
   }

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

sat_main.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
      itemView.setVisibility(View.VISIBLE);
      sat_main.startAnimation(mainRotateLeft);
      itemView.startAnimation(itemOut);
   }
});
 mainRotateLeft = AnimationUtils.loadAnimation(context, R.anim.sat_main_rotate_left);
<?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"/>

代码在:https://github.com/nickgao1986/StepSport

0人推荐
随时随地看视频
慕课网APP