天天看點

android 動畫 圖檔從指定位置飛到指定位置

private ViewGroup anim_mask_layout;//動畫層
private ImageView sendFeather;// 這是在界面上跑的小圖檔
           
</pre><pre name="code" class="java">//點選開始動畫
           
public void sendFeather(View view) {</span>
           
int[] start_location = new int[2];// 一個整型數組,用來存儲按鈕的在螢幕的X、Y坐标
        view.getLocationInWindow(start_location);// 這是擷取購買按鈕的在螢幕的X、Y坐标(這也是動畫開始的坐标)
           
<span style="white-space:pre">	</span>sendFeather = new ImageView(context);// 動畫的圖檔,(R.drawable.live_send_feather)
<span style="white-space:pre">	</span>sendFeather.setImageResource(R.drawable.live_send_feather);// 設定圖檔
<span style="white-space:pre">	</span>etAnim(sendFeather, start_location);// 開始執行動畫</span>
           
}
           
</pre><pre name="code" class="java">private void setAnim(final View v, int[] start_location) {
		anim_mask_layout = null;
		anim_mask_layout = createAnimLayout();
		anim_mask_layout.addView(v);//把動畫小球添加到動畫層
		final View view = addViewToAnimLayout(anim_mask_layout, v,
				start_location);
		int[] end_location = new int[2];// 這是用來存儲動畫結束位置的X、Y坐标
		activity_live_feather.getLocationInWindow(end_location);// shopCart是那個購物車

		// 計算位移
		int endX = 0 - start_location[0] + 40;// 動畫位移的X坐标
		int endY = end_location[1] - start_location[1];// 動畫位移的y坐标
		TranslateAnimation translateAnimationX = new TranslateAnimation(0,
				endX, 0, 0);
		translateAnimationX.setInterpolator(new LinearInterpolator());
		translateAnimationX.setRepeatCount(0);// 動畫重複執行的次數
		translateAnimationX.setFillAfter(true);

		TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
				0, endY);
		translateAnimationY.setInterpolator(new AccelerateInterpolator());
		translateAnimationY.setRepeatCount(0);// 動畫重複執行的次數
		translateAnimationX.setFillAfter(true);

		AnimationSet set = new AnimationSet(false);
		set.setFillAfter(false);
		set.addAnimation(translateAnimationY);
		set.addAnimation(translateAnimationX);
		set.setDuration(800);// 動畫的執行時間
		view.startAnimation(set);
		// 動畫監聽事件
		set.setAnimationListener(new AnimationListener() {
			// 動畫的開始
			@Override
			public void onAnimationStart(Animation animation) {
				v.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}

			// 動畫的結束
			@Override
			public void onAnimationEnd(Animation animation) {
				v.setVisibility(View.GONE);
				
			}
		});

	}
	
	/**
	 * @Description: 建立動畫層
	 * @param
	 * @return void
	 * @throws
	 */
	private ViewGroup createAnimLayout() {
		ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
		LinearLayout animLayout = new LinearLayout(this);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		animLayout.setLayoutParams(lp);
		animLayout.setId(Integer.MAX_VALUE);
		animLayout.setBackgroundResource(android.R.color.transparent);
		rootView.addView(animLayout);
		return animLayout;
	}
	
	private View addViewToAnimLayout(final ViewGroup vg, final View view,
			int[] location) {
		int x = location[0];
		int y = location[1];
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.leftMargin = x;
		lp.topMargin = y;
		view.setLayoutParams(lp);
		return view;
	}