Android使用動畫動態(tài)添加商品進購物車
本文實例為大家分享了Android添加商品進購物車的具體代碼,供大家參考,具體內(nèi)容如下
1、首先展示下效果圖

2、講一下思路,小球由加號位置運動到購物車位置,首先得獲得這兩個點在整個屏幕中的坐標,然后分別計算這兩個點的橫縱坐標的差值,再通過TranslateAnimation這個類設(shè)置小球在X、Y方向上的偏移量,最后通過AnimationSet這個類將這兩個動畫放在一起執(zhí)行。這是小球運動的動畫,還有就是購物車變大縮小的動畫。這個動畫通過ObjectAnimator的ofFloat的方法設(shè)置縮放,要注意的是當小球落下的時候,購物車才開始動畫,所以要設(shè)置一下setStartDelay這個方法。
3、具體的代碼我就貼一下動畫部分的代碼,如果想要這個Demo看下我最后貼出的Github的地址
@Override
public void setAnim(View view) {
// TODO Auto-generated method stub
int[] start_location = new int[2];// 一個整型數(shù)組用來存儲按鈕在屏幕的X,Y坐標
view.getLocationInWindow(start_location);// 購買按鈕在屏幕中的坐標
buyImg = new ImageView(this);// 動畫的小圓圈
buyImg.setImageResource(R.drawable.sign);// 設(shè)置buyImg的圖片
setAnim(buyImg, start_location);
}
/**
* hdh: 創(chuàng)建動畫層
*
* @return
*/
private ViewGroup createAnimLayout() {
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();// 獲得Window界面的最頂層
LinearLayout animLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
//animLayout.setId();
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}
/**
* hdh:
*
* @param vp
* @param view
* @param location
* @return
*/
private View addViewToAnimLayout(final ViewGroup vp, 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;
}
/**
* hdh:動畫
*
* @param v
* @param start_location
*/
private void setAnim(final View v, int[] start_location) {
anim_mask_layout = null;
anim_mask_layout = createAnimLayout();
anim_mask_layout.addView(v);
View view = addViewToAnimLayout(anim_mask_layout, v, start_location);
int[] end_location = new int[2];// 存儲動畫結(jié)束位置的X,Y坐標
text_chart_num.getLocationInWindow(end_location);// 將購物車的位置存儲起來
// 計算位移
int endX = end_location[0] - start_location[0];// 動畫位移的X坐標
int endY = end_location[1] - start_location[1];// 動畫位移的y坐標
TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());// 設(shè)置此動畫的加速曲線。默認為一個線性插值。
translateAnimationX.setRepeatCount(0);// 動畫重復(fù)的次數(shù)
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);// 動畫重復(fù)次數(shù)
translateAnimationY.setFillAfter(true);
AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
set.addAnimation(translateAnimationX);
set.addAnimation(translateAnimationY);
set.setDuration(1000);
view.startAnimation(set);
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
v.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
v.setVisibility(View.GONE);
}
});
ObjectAnimator anim = ObjectAnimator//
.ofFloat(view, "scale", 1.0F, 1.5F, 1.0f)//
.setDuration(500);//
anim.setStartDelay(1000);
anim.start();
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float cVal = (Float) animation.getAnimatedValue();
image_chart.setScaleX(cVal);
image_chart.setScaleY(cVal);
text_chart_num.setScaleX(cVal);
text_chart_num.setScaleY(cVal);
}
});
}
4、GitHub地址:點擊打開鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- Android把商品添加到購物車的動畫效果(貝塞爾曲線)
- Android中貝塞爾曲線的繪制方法示例代碼
- android中貝塞爾曲線的應(yīng)用示例
- Android貝塞爾曲線初步學(xué)習第三課 Android實現(xiàn)添加至購物車的運動軌跡
- Android 利用三階貝塞爾曲線繪制運動軌跡的示例
- Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實例詳解)
- Android實現(xiàn)購物車添加物品的動畫效果
- 詳解Android實現(xiàn)購物車頁面及購物車效果(點擊動畫)
- Android實現(xiàn)添加商品到購物車動畫效果
- Android利用二階貝塞爾曲線實現(xiàn)添加購物車動畫詳解
相關(guān)文章
Java4Android開發(fā)教程(二)hello world!
一般的開發(fā)教程都是介紹完安裝配置開發(fā)環(huán)境,緊接著來一篇hello world,算是國際慣例吧,我們當然也不能免俗,哈哈,各位看官請看好了!2014-10-10
Android App中制作仿MIUI的Tab切換效果的實例分享
這篇文章主要介紹了Android App中制作仿MIUI的Tab切換效果的實例分享,實現(xiàn)具有跟隨手指滾動而滾動功能的ViewPagerIndicator,需要的朋友可以參考下2016-04-04
Android Studio 中運行 groovy 程序的方法圖文詳解
這篇文章主要介紹了Android Studio 中 運行 groovy 程序的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
android自定義組件實現(xiàn)儀表計數(shù)盤
這篇文章主要為大家詳細介紹了android自定義組件實現(xiàn)儀表計數(shù)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

