添加购物车动画

本文详细介绍了如何在用户点击添加购物车按钮时,通过精心设计的动画增强用户体验。内容涵盖点击事件处理、动画原理及实现步骤,帮助开发者创建出流畅、吸引人的交互效果。
摘要由CSDN通过智能技术生成

响应点击事件

private void clickAction() {
		int[] start_location = new int[2];// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
		vginfo_btn_ok.getLocationInWindow(start_location);// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
		// 这是在界面上跑的小图片
		ImageView buyImg = new ImageView(OtherActivity.this);// buyImg是动画的图片,我的是一个小球(R.drawable.sign)
		buyImg.setImageResource(R.drawable.shopcartani);// 设置buyImg的图片
		setAnim(buyImg, start_location);// 开始执行动画
	}
动画核心代码
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坐标
		gwc.getLocationInWindow(end_location);// shopCart是那个购物车gwc

		// 计算位移
		int endX = end_location[0]-start_location[0] ;// 动画位移的X坐标
		int endY = end_location[1] - start_location[1]+gwc.getHeight()/2;// 动画位移的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);
		// 通过TranslateAnimation(float fromXDelta, float toXDelta, float
		// fromYDelta, float toYDelta) 来定义动画
		// float fromXDelta 动画开始的点离当前View X坐标上的差值
		// float toXDelta 动画结束的点离当前View X坐标上的差值
		// float fromYDelta 动画开始的点离当前View Y坐标上的差值
		// float toYDelta 动画开始的点离当前View Y坐标上的差值

		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) {
			}
			// 动画的结束
			@Override
			public void onAnimationEnd(Animation animation) {
				v.setVisibility(View.GONE);
				sp.edit().putString("count", count+"").commit();
				if(callBackNum!=null){
					Log.d("ff", count+"");
					callBackNum.getNum(count);
				}
				vginfo_sl.setText(count+"");
			}
		});
	}
创建动画层
/**
	 * @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;
	}
添加view到动画层
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;
	}
	
项目所涉及的代码段

package com.example.aa;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OtherActivity extends Activity implements OnClickListener {

	private RelativeLayout vginfo_back;
	private TextView gwc, vginfo_sl;
	private Button  vginfo_btn_ok;
	private SharedPreferences sp;
	private ViewGroup anim_mask_layout;// 动画层
	private int count=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sp=this.getSharedPreferences("num", MODE_PRIVATE);
		
		vginfo_back = (RelativeLayout) findViewById(R.id.ym_top_back);
		gwc = (TextView) findViewById(R.id.ym_top_name);
		vginfo_sl = (TextView) findViewById(R.id.ym_top_sl);
		vginfo_btn_ok = (Button) findViewById(R.id.vginfo_btn_ok);
		
		vginfo_back.setOnClickListener(this);
		vginfo_btn_ok.setOnClickListener(this);

		vginfo_sl.setText(sp.getString("count", null));
		count=sp.getString("count", null)==null?0:Integer.valueOf(sp.getString("count", null));
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.ym_top_back:
			finish();
			break;
		case R.id.vginfo_btn_ok:
			count++;
			clickAction();
			break;
		default:
			break;
		}
	}

	private void clickAction() {
		int[] start_location = new int[2];// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
		vginfo_btn_ok.getLocationInWindow(start_location);// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
		// 这是在界面上跑的小图片
		ImageView buyImg = new ImageView(OtherActivity.this);// buyImg是动画的图片,我的是一个小球(R.drawable.sign)
		buyImg.setImageResource(R.drawable.shopcartani);// 设置buyImg的图片
		setAnim(buyImg, start_location);// 开始执行动画
	}

	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坐标
		gwc.getLocationInWindow(end_location);// shopCart是那个购物车gwc

		// 计算位移
		int endX = end_location[0]-start_location[0] ;// 动画位移的X坐标
		int endY = end_location[1] - start_location[1]+gwc.getHeight()/2;// 动画位移的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);
		// 通过TranslateAnimation(float fromXDelta, float toXDelta, float
		// fromYDelta, float toYDelta) 来定义动画
		// float fromXDelta 动画开始的点离当前View X坐标上的差值
		// float toXDelta 动画结束的点离当前View X坐标上的差值
		// float fromYDelta 动画开始的点离当前View Y坐标上的差值
		// float toYDelta 动画开始的点离当前View Y坐标上的差值

		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) {
			}
			// 动画的结束
			@Override
			public void onAnimationEnd(Animation animation) {
				v.setVisibility(View.GONE);
				sp.edit().putString("count", count+"").commit();
				if(callBackNum!=null){
					Log.d("ff", count+"");
					callBackNum.getNum(count);
				}
				vginfo_sl.setText(count+"");
			}
		});
	}
	
	/**
	 * @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;
	}
	
	
	private CallBackNum callBackNum;
	
	public CallBackNum getCallBackNum() {
		return callBackNum;
	}
	public void setCallBackNum(CallBackNum callBackNum) {
		this.callBackNum = callBackNum;
	}
	public interface CallBackNum{
		public int getNum(int count);
	}
}

效果图



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
js加入购物车抛物线动画购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入的购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车! CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值