Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫效果
Android上的熱火鍋煮蘿卜蔬菜的Loading動(dòng)畫效果。 這是一個(gè)鍋煮蘿卜的Loading動(dòng)畫,效果仿照自之前IOS上看到的一個(gè)效果,覺得挺有意思,就移植過來了,在此完成了Dialog的樣式,方便使用者作為LoadingView去使用。
關(guān)鍵性代碼:
package yellow5a5.demo.boilingloadingview.View; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.drawable.ClipDrawable; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.RelativeLayout; import java.util.Timer; import java.util.TimerTask; import yellow5a5.demo.boilingloadingview.R; /** * Created by Weiwu on 16/1/2. */ public class BoilingPanView extends RelativeLayout { private View mView; private ClipDrawable mWaterDrawable; private WaterView mWaterView; private FlameView mFlameView; private View mPea1; private View mPea2; private ImageView mPotato; private ImageView mCarrot; private ImageView mCoverView; private Animation mLeftInAnim; private Animation mRightInAnim; private boolean isRightRotate = true; private ValueAnimator mCoverAnim; private BoilingAnimListener mBoilingAnimListener; public interface BoilingAnimListener { //初始動(dòng)畫結(jié)束監(jiān)聽 void onFirstAnimEnd(); } public void setBoilingAnimListener(BoilingAnimListener l) { this.mBoilingAnimListener = l; } private Handler mHandle = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what == 0X0000) { mWaterDrawable.setLevel(mWaterDrawable.getLevel() + 800); } return false; } }); public BoilingPanView(Context context) { this(context, null); } public BoilingPanView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BoilingPanView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mView = LayoutInflater.from(context).inflate(R.layout.boiling_pan, this, true); initView(); initStartAnim(); initCoverAnim(); } private void initView() { mWaterView = (WaterView) mView.findViewById(R.id.img_water); mFlameView = (FlameView) mView.findViewById(R.id.flame); mCoverView = (ImageView) mView.findViewById(R.id.img_cover); mPea1 = mView.findViewById(R.id.img_pea1); mPea2 = mView.findViewById(R.id.img_pea2); mPotato = (ImageView) mView.findViewById(R.id.img_potato); mCarrot = (ImageView) mView.findViewById(R.id.img_carrot); mWaterDrawable = (ClipDrawable) mWaterView.getDrawable(); } private void initStartAnim() { mLeftInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.left_in_anim); mRightInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.right_in_anim); } /* 抖動(dòng)的蓋子 */ private void initCoverAnim() { mCoverAnim = ValueAnimator.ofFloat(0f, 1f, 0f).setDuration(800); mCoverAnim.setRepeatMode(Animation.REVERSE); mCoverAnim.setRepeatCount(-1); mCoverAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); if (isRightRotate) { mCoverView.setRotation(value * 5); } else { mCoverView.setRotation(-value * 5); } mCoverView.setTranslationY(-value * TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics())); } }); mCoverAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); isRightRotate = !isRightRotate; } }); } /* 開始啟動(dòng)的動(dòng)畫 */ public void beginFirstInAnim() { mPea1.setVisibility(VISIBLE); mPea2.setVisibility(VISIBLE); mPotato.setVisibility(VISIBLE); mCarrot.setVisibility(VISIBLE); mCoverView.setVisibility(VISIBLE); mPea1.startAnimation(mLeftInAnim); mPea2.startAnimation(mLeftInAnim); mPotato.startAnimation(mLeftInAnim); mCarrot.startAnimation(mRightInAnim); mCoverView.startAnimation(mRightInAnim); mRightInAnim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (mBoilingAnimListener != null) { //這里是為了給外部留有操作的空間 mBoilingAnimListener.onFirstAnimEnd(); } else { beginBoilingAnim(); } } @Override public void onAnimationRepeat(Animation animation) { } }); } /* 開始加水燃火動(dòng)畫 */ public void beginBoilingAnim() { final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { mHandle.sendEmptyMessage(0X0000); if (mWaterDrawable.getLevel() >= 10000) { timer.cancel(); } } }, 0, 50); mFlameView.startFlaming(); mCoverAnim.start(); } /* 重置動(dòng)畫 */ public void resetAnim() { mWaterDrawable.setLevel(0); mWaterView.resetBubbleAnim(); mFlameView.stopFlaming(); mPea1.setVisibility(INVISIBLE); mPea2.setVisibility(INVISIBLE); mPotato.setVisibility(INVISIBLE); mCarrot.setVisibility(INVISIBLE); mCoverView.setVisibility(INVISIBLE); } }
希望本文所述對大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android基于AlarmManager實(shí)現(xiàn)用戶在線心跳功能示例
這篇文章主要介紹了Android基于AlarmManager實(shí)現(xiàn)用戶在線心跳功能,結(jié)合檢測用戶在線功能實(shí)例形式分析了AlarmManager全局定時(shí)器的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-10-10Android中執(zhí)行java命令的方法及java代碼執(zhí)行并解析shell命令
這篇文章給大家介紹Android中執(zhí)行java命令的方法及java代碼執(zhí)行并解析shell命令,需要的朋友一起學(xué)習(xí)2015-11-11React-Native中使用驗(yàn)證碼倒計(jì)時(shí)的按鈕實(shí)例代碼
這篇文章主要介紹了React-Native中使用驗(yàn)證碼倒計(jì)時(shí)的按鈕實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-04-04Android 中build.prop 文件與 getprop 命令
這篇文章主要介紹了Android 中build.prop 文件與 getprop 命令的相關(guān)資料,需要的朋友可以參考下2017-06-06Android仿天天動(dòng)聽歌曲自動(dòng)滾動(dòng)view
這篇文章主要為大家詳細(xì)介紹了Android仿天天動(dòng)聽歌曲自動(dòng)滾動(dòng)view的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一)
本文給大家介紹Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一),對android網(wǎng)絡(luò)狀態(tài)監(jiān)聽相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例
這篇文章主要為大家詳細(xì)介紹了GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android BottomNavigationView與Fragment重建與重疊問題解決方法探索
這篇文章主要介紹了Android BottomNavigationView與Fragment重建與重疊問題解決,總的來說這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過程。希望通過這道題能給你帶來一種解題優(yōu)化的思路2023-01-01Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對這兩種方法進(jìn)行了詳細(xì)的對比,對于開始學(xué)習(xí)Android的朋友使用這兩種方法是個(gè)很好的資料,需要的朋友可以參考下2016-11-11