作了一個加入購物車動畫,代碼比較簡單,先看下效果,可能因錄製git圖片軟件問題,錄製的git圖片有些卡頓,實際效果流暢性仍是不錯的。java
代碼很簡單android
首先佈局文件:git
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView android:id="@+id/iv_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/pic"/> <ImageView android:id="@+id/iv_cart" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerInParent="true" android:layout_alignParentBottom="true" android:src="@mipmap/car"/> </RelativeLayout>
而後就是MainActivity.java文件了app
package com.iningke.demo; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.LinearLayout; import java.io.File; public class MainActivity extends Activity { ImageView img,iv_cart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.iv_pic); iv_cart = (ImageView)findViewById(R.id.iv_cart); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //縮放 Animation animation = new ScaleAnimation( 2f, 0.2f, 2f, 0.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(500); animation.setFillAfter(true); //位移 int[] startLocation = new int[2]; int[] endLocation = new int[2]; img.getLocationInWindow(startLocation); iv_cart.getLocationInWindow(endLocation); //獲取購物車圖標在屏幕位置 Log.e("==", "starX:" + startLocation[0] + "endX:" + endLocation[0] + "starY:" + startLocation[1] + "endY:" + startLocation[1]); Animation animationXy = new TranslateAnimation( startLocation[0],endLocation[0]-(iv_cart.getWidth()/3), startLocation[1],endLocation[1] - (iv_cart.getHeight()*2/3)); //正好讓圖片進入到購物車 animationXy.setDuration(1000); animationXy.setRepeatCount(0); AnimationSet set = new AnimationSet(false); set.addAnimation(animation); set.setFillAfter(true); set.addAnimation(animationXy); img.startAnimation(set); } }); } }
哈哈,效果就是這樣,想要下載源碼的朋友能夠點擊下載ide