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)物車才開始動(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)擊打開鏈接
以上就是本文的全部?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)文章
Android編程實(shí)現(xiàn)夜間模式的方法小結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)夜間模式的方法,涉及Android亮度調(diào)節(jié)、自定義Theme、遮罩模式等相關(guān)使用技巧,需要的朋友可以參考下2018-01-01
Android編程實(shí)現(xiàn)將應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法,分析了Android程序安裝的相關(guān)屬性設(shè)置技巧及注意事項(xiàng),需要的朋友可以參考下2015-12-12
Android sdutio配置Zxing進(jìn)行掃碼功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Android sdutio配置Zxing進(jìn)行掃碼功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-05-05
Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問(wèn)題解決方法
這篇文章主要介紹了Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問(wèn)題解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
這篇文章主要介紹了Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法,涉及Android中xml文件布局的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android?Hilt?Retrofit?Paging3使用實(shí)例
這篇文章主要介紹了Android?Hilt依賴注入的使用,首先,某個(gè)類的成員變量稱為依賴,如若此變量想要實(shí)例化引用其類的方法,可以通過(guò)構(gòu)造函數(shù)傳參或者通過(guò)某個(gè)方法獲取對(duì)象,此等通過(guò)外部方法獲取對(duì)象實(shí)例的稱為依賴注入2023-01-01

