編輯:關於 Android 程式設計
下載動畫常出現在下載需求較多的 App 中,比如遊戲下載平台、應用市場等。
先看看效果圖:

實作
整個思路是:先把要飛行的圖示(logo)轉成一張 bitmap,建立一個獨立的動畫層把它加進去,再用 X、Y 兩段位移動畫組合成一條拋物線軌跡,飛向目標下載按鈕。
private void startAnim() { //以bitmap創建new ImageView iv.setDrawingCacheEnabled(true); Bitmap bitmap = iv.getDrawingCache(); ImageView logo = new ImageView(this); logo.setScaleType(ImageView.ScaleType.FIT_XY); logo.setImageBitmap(bitmap); int[] startLocation = new int[2]; iv.getLocationInWindow(startLocation); end.getLocationInWindow(location_download); setAnim(logo, startLocation, location_download); }設定動畫
X 方向用 LinearInterpolator(等速)、Y 方向用 AccelerateInterpolator(加速),兩者疊加就會呈現先水平、再墜落的拋物線感覺:
private void setAnim(final ImageView logo, int[] startLocation,int[] location_download) { ViewGroup animMaskLayout = createAnimLayout(); animMaskLayout.addView(logo);// 把動畫小球添加到動畫層 // 計算位移 final View view = addViewToAnimLayout(logo, startLocation); // 動畫位移的X坐標 int endY = location_download[1] - startLocation[1]; // 動畫位移的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); AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(2000);// 動畫的執行時間 view.startAnimation(set); // 動畫監聽事件 set.setAnimationListener(new Animation.AnimationListener() { // 動畫的開始 @Override public void onAnimationStart(Animation animation) { logo.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { } // 動畫的結束 @Override public void onAnimationEnd(Animation animation) { logo.setVisibility(View.GONE); } }); } 建立動畫父層
在 DecorView 上疊一個透明的全螢幕 LinearLayout 當作動畫層,讓 logo 能自由飛越整個畫面:
private ViewGroup createAnimLayout() { ViewGroup rootView = (ViewGroup) 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; } 設定動畫層的版面參數
透過 leftMargin、topMargin 把 logo 定位到起始座標:
private static View addViewToAnimLayout(final View view, int[] location) { int x = location[0]; int y = location[1]; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(80, 80); lp.leftMargin = x; lp.topMargin = y; view.setLayoutParams(lp); return view; } 程式碼就到這裡,整體看起來並不難,動手試試看吧。
以上就是本文的全部內容,希望對大家的學習有所幫助。
Android 直播推流學習筆記:逐段拆解 spydroid 的 RTSP 實現
第一部:先看整體結構 本文以 spydroid 的源碼為主線,一步步梳理 Android 端 RTSP 推流的實現方式。spydroid 採用 RTSP 協議,目前支援
U-Boot 移植實戰:三星 S5PV210 板級移植全流程
前言 U-Boot 移植是嵌入式學習中的一項核心工程。本文以三星 S5PV210 開發板為例,記錄了從源碼獲取、時鐘與 DDR 配置修改,到編譯燒錄與問題排查的完整過程,
Android 結合 zbar 二維碼掃描:的解決中文亂碼與掃描區域自訂
本文是上一篇 zbar 二維碼掃描的延伸,針對網友提出的兩個痛點做了修復:一是掃描框過去只是「假框」,實際上仍對全屏圖做解析;二是掃描中文時出現亂碼。以下分享修復後的成果
從零開始:用 Android Studio 創建並跑通你的第一個項目
一、創建 HelloWorld 項目 幾乎所有編程語言的第一個程序都是 Hello World,這個傳統從上世紀 70 年代流傳至今,早已成為編程界的經典儀式。這次我們就