標籤:安卓
開源中國安卓用戶端源碼學習(一)
準備學習安卓開發, 看到網上有人推薦開源中國安卓用戶端的源碼, 說裡麵包含了大部分技術, 於是準備好好研究研究. 特開通此系列部落格來記錄學習過程. 由於是在學習, 經驗不足, 裡面肯定有很多不對的地方, 望大家指正.
到這裡下載源碼包,開發環境為Linux下Eclipse,匯入源碼後有可能會出現android.webkit.CacheManager找不到的錯誤, 原因是這個類在4.0以上版本的SDK被刪除了, 只要下載4.0版本的SDK使用即可. 由於google被牆, 使用SDK管理器可能無法下載, 可以在網上直接下載4.0的SDK, 將檔案夾名字改為android-15, 放到android-sdk的platforms目錄下.
首先是漸層的啟動介面.
定位到程式入口, /oschina-android-app/src/net/oschina/app/AppStart.java, AppStart為啟動類, onCreate方法為入口方法,
final View view = View.inflate(this, R.layout.start, null); //由layout檔案夾下的start.xml檔案定義啟動介面視圖setContentView(view); // 設定activity顯示的視圖
start.xml 檔案內容如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/app_start_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:background="@drawable/start_background"> // 漸層使用的圖片, 但這裡的start_background是個xml檔案, 以更精確的控製圖片</LinearLayout>
start_background.xml內容如下
<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/welcome" // 啟動圖片, welcome.png android:scaleType="fitStart"/> // 縮放類型
利用AlphaAnimatio類來實現啟動介面的漸層效果
AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f); // 漸層透明度範圍 aa.setDuration(2000); // 期間 view.startAnimation(aa); // 啟動漸層 aa.setAnimationListener(new AnimationListener() // 監聽事件, 設定漸層開始, 重複, 結束時的處理 { @Override public void onAnimationEnd(Animation arg0) { redirectTo(); // 漸層結束後進入到主介面 } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) {} });
下面的redirectTo函數, 其功能就是新開一個activity, 在其中開啟主介面, 並且結束當前activity
private void redirectTo(){ Intent intent = new Intent(this, Main.class); // Main 是一個繼承了Activity的類 startActivity(intent); // 在新的activity中開啟主介面 finish(); // 結束當前activity }
這個類中另外兩個函數check和getTime都不重要, 略過.
tips: 修改啟動介面的圖片資源後, 要在eclipse的project-clean中清理一下項目緩衝, 要不然可能無法即時顯示修改後的介面.
開源中國安卓用戶端源碼學習(一) 漸層啟動介面