標籤:彈齣動畫
先上:
先寫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"?><rotatexmlns: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