編輯:Android 開發實例
本文參考一個國外開發者製作的啟動動畫效果,整理出其實作重點。整體思路是:先準備動畫用的圖片素材,再用 AnimationDrawable 播放逐幀動畫,最後透過自訂樣式做出淡入淡出的過場。
首先在 drawable 目錄下放入動畫所需的圖片,並建立一個描述逐幀動畫的 XML(例如 flag.xml),把各張圖片依序排列成動畫幀。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TheSplashLayout"
android:layout_gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SplashImageView"
android:layout_gravity="center"
>
</ImageView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
public class SplashScreen extends Activity {
/** 用來處理啟動畫面事件的執行緒 */
private Thread mSplashThread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); // 啟動畫面視圖
// 開始播放逐幀動畫
final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
final AnimationDrawable frameAnimation = (AnimationDrawable) splashImageView.getBackground();
splashImageView.post(new Runnable() {
public void run() {
frameAnimation.start();
}
});
final SplashScreen sPlashScreen = this;
// 等待啟動畫面事件的執行緒
mSplashThread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
wait(5000); // 等待一段時間,或觸控後提前結束
}
} catch (InterruptedException ex) {
}
finish();
// 執行下一個 Activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return false;
}
/** 處理啟動畫面的觸控事件 */
@Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}
為了讓過場更好看,在 values 目錄下新增樣式檔:
<resources>
<style name="Animations" parent="@android:Animation" />
<style name="Animations.SplashScreen">
<item name="android:windowEnterAnimation">@anim/appear</item>
<item name="android:windowExitAnimation">@anim/disappear</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowAnimationStyle">@style/Animations.SplashScreen</item>
</style>
</resources>
其中特別需要注意的是這段過場動畫的定義:
<style name="Animations" parent="@android:Animation" />
<style name="Animations.SplashScreen">
<item name="android:windowEnterAnimation">@anim/appear</item>
<item name="android:windowExitAnimation">@anim/disappear</item>
</style>
透過 windowEnterAnimation 與 windowExitAnimation 指定進出場動畫,再搭配透明主題,就能讓啟動畫面自然地淡入、淡出,營造出更有質感的開場體驗。
Android 登入畫面範例:帳號密碼與錯誤次數限制
登入畫面常用來驗證使用者身份。實作時通常需要兩個輸入欄位:使用者名稱與密碼,並加上登入按鈕。為了避免暴力嘗試,還可以限制錯誤次數。 建立輸入欄位 <EditText
Android 網絡編程:獲取網絡上的 JSON 數據實例
要獲取網絡上的 JSON 數據,首先需要服務器端提供相應支持。本文以一個新聞列表為例,完整演示從服務器端構建 JSON 接口到 Android 端解析展示的全流程。一、創
Android 實作:完整實現 QQ 表情的發送與接收
本文要完整說明如何在 Android 中實作 QQ 風格的表情發送與接收。在自己練習製作聊天應用時,需要用到表情功能,於是便模仿 QQ 表情做了一個,圖片資源完全取自 Q
Android MediaPlayer 多媒體播放教學
Android 提供了多種控制音訊與影片播放的方式,其中最常用的一種,就是透過名為 MediaPlayer 的類別。 Android 的 MediaPlayer 類別能存