Android 加入购物车动画

作了一个加入购物车动画,代码比较简单,先看下效果,可能因录制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

相关文章
相关标签/搜索