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

2、講一下思路,小球由加號(hào)位置運(yùn)動(dòng)到購(gòu)物車位置,首先得獲得這兩個(gè)點(diǎn)在整個(gè)屏幕中的坐標(biāo),然后分別計(jì)算這兩個(gè)點(diǎn)的橫縱坐標(biāo)的差值,再通過(guò)TranslateAnimation這個(gè)類設(shè)置小球在X、Y方向上的偏移量,最后通過(guò)AnimationSet這個(gè)類將這兩個(gè)動(dòng)畫放在一起執(zhí)行。這是小球運(yùn)動(dòng)的動(dòng)畫,還有就是購(gòu)物車變大縮小的動(dòng)畫。這個(gè)動(dòng)畫通過(guò)ObjectAnimator的ofFloat的方法設(shè)置縮放,要注意的是當(dāng)小球落下的時(shí)候,購(gòu)物車才開(kāi)始動(dòng)畫,所以要設(shè)置一下setStartDelay這個(gè)方法。
3、具體的代碼我就貼一下動(dòng)畫部分的代碼,如果想要這個(gè)Demo看下我最后貼出的Github的地址
@Override
public void setAnim(View view) {
// TODO Auto-generated method stub
int[] start_location = new int[2];// 一個(gè)整型數(shù)組用來(lái)存儲(chǔ)按鈕在屏幕的X,Y坐標(biāo)
view.getLocationInWindow(start_location);// 購(gòu)買按鈕在屏幕中的坐標(biāo)
buyImg = new ImageView(this);// 動(dòng)畫的小圓圈
buyImg.setImageResource(R.drawable.sign);// 設(shè)置buyImg的圖片
setAnim(buyImg, start_location);
}
/**
* hdh: 創(chuàng)建動(dò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:動(dòng)畫
*
* @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];// 存儲(chǔ)動(dòng)畫結(jié)束位置的X,Y坐標(biāo)
text_chart_num.getLocationInWindow(end_location);// 將購(gòu)物車的位置存儲(chǔ)起來(lái)
// 計(jì)算位移
int endX = end_location[0] - start_location[0];// 動(dòng)畫位移的X坐標(biāo)
int endY = end_location[1] - start_location[1];// 動(dòng)畫位移的y坐標(biāo)
TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());// 設(shè)置此動(dòng)畫的加速曲線。默認(rèn)為一個(gè)線性插值。
translateAnimationX.setRepeatCount(0);// 動(dòng)畫重復(fù)的次數(shù)
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);// 動(dòng)畫重復(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地址:點(diǎn)擊打開(kāi)鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android把商品添加到購(gòu)物車的動(dòng)畫效果(貝塞爾曲線)
- Android中貝塞爾曲線的繪制方法示例代碼
- android中貝塞爾曲線的應(yīng)用示例
- Android貝塞爾曲線初步學(xué)習(xí)第三課 Android實(shí)現(xiàn)添加至購(gòu)物車的運(yùn)動(dòng)軌跡
- Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例
- Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)
- Android實(shí)現(xiàn)購(gòu)物車添加物品的動(dòng)畫效果
- 詳解Android實(shí)現(xiàn)購(gòu)物車頁(yè)面及購(gòu)物車效果(點(diǎn)擊動(dòng)畫)
- Android實(shí)現(xiàn)添加商品到購(gòu)物車動(dòng)畫效果
- Android利用二階貝塞爾曲線實(shí)現(xiàn)添加購(gòu)物車動(dòng)畫詳解
相關(guān)文章
Java4Android開(kāi)發(fā)教程(二)hello world!
一般的開(kāi)發(fā)教程都是介紹完安裝配置開(kāi)發(fā)環(huán)境,緊接著來(lái)一篇hello world,算是國(guó)際慣例吧,我們當(dāng)然也不能免俗,哈哈,各位看官請(qǐng)看好了!2014-10-10
Android App中制作仿MIUI的Tab切換效果的實(shí)例分享
這篇文章主要介紹了Android App中制作仿MIUI的Tab切換效果的實(shí)例分享,實(shí)現(xiàn)具有跟隨手指滾動(dòng)而滾動(dòng)功能的ViewPagerIndicator,需要的朋友可以參考下2016-04-04
android 退出程序解決內(nèi)存釋放的問(wèn)題
做Android項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)問(wèn)題:當(dāng)應(yīng)用程序退出了,點(diǎn)擊"設(shè)置"查看應(yīng)用程序,界面顯示著可以點(diǎn)擊"強(qiáng)制關(guān)閉 由于這個(gè)問(wèn)題我發(fā)現(xiàn)了一個(gè)更加嚴(yán)重的問(wèn)題,那就是,在我應(yīng)用程序退出之后,系統(tǒng)并沒(méi)有釋放掉我應(yīng)用程序所占內(nèi)存2012-11-11
Android Studio 中運(yùn)行 groovy 程序的方法圖文詳解
這篇文章主要介紹了Android Studio 中 運(yùn)行 groovy 程序的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android星級(jí)評(píng)分條的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android星級(jí)評(píng)分條的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android自定義View播放Gif動(dòng)畫的示例
本篇文章主要介紹了Android自定義View播放Gif動(dòng)畫的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤
這篇文章主要為大家詳細(xì)介紹了android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題
最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開(kāi)發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下2015-05-05

