1. Hello World
基本的顯示 Android API Demos學習(1) – Hello World
2. Save & Restore State
保存和恢復UI狀態。 Android API Demos學習(2) – Save & Restore State
3. Persistent State
永久保存用戶偏好。 Android API Demos學習(3) – Persistent State
4. Receive Result
啟動其他activity,並且從子activity中接收返回數據。 Android API Demos學習(4) – Receive Result
5. Forwarding
啟動另一個Activity的時候,把當前Activity移出保存Activity的歷史堆棧,就是說,按BACK鍵的時候不會再返回前一個Activity。
實現很簡單,就是啟動其他Activity的時候,用finish()結束當前Activity。
public void onClick(View v)
{
// Here we start the next activity, and then call finish()
// so that our own will stop running and be removed from the
// history stack.
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
finish();
}
6. Redirection
利用保存的數據判斷啟動哪個Activity。
本例中判斷是否保存瞭數據,如果沒有保存就進入RedirectGetter中,有的話顯示在RedirectMain中。
if (!loadPrefs()) {
Intent intent = new Intent(this, RedirectGetter.class);
startActivityForResult(intent, INIT_TEXT_REQUEST);
}
7. Translucent
顯示半透明的背景。 www.aiwalls.com
先看AndroidManifest.xml中的定義:
<activity android:name=".app.TranslucentActivity"
android:label="@string/activity_translucent"
android:theme="@style/Theme.Translucent">
這裡調用瞭Theme.Translucent主題,這個主題在/res/values/styles.xml中定義:
<style name="Theme.Translucent" parent="android:style/Theme.Translucent">
<item name="android:windowBackground">@drawable/translucent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
android:windowBackground在/res/values/colors.xml中被設置為#e0000000,前面的e0是設置透明度的。
8. TranslucentBlur
帶特效的半透明背景。
<style name="Theme.Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
<item name="android:windowBackground">@drawable/transparent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
android:windowAnimationStyle設置跳轉動畫效果。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
上面這句實現瞭模糊效果。
9. Dialog Activity
把Activity的主題設為Dialog,讓Activity看起來像一個對話框。
<activity android:name=".app.DialogActivity"
android:label="@string/activity_dialog"
android:theme="@android:style/Theme.Dialog">
另外可以設置對話框上面的圖標,如下:
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.ic_dialog_alert);
10. Custom Title
自定義標題欄。
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.custom_title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
其中requestWindowFeature是激活擴展的窗口屬性,這裡設置的Window.FEATURE_CUSTOM_TITLE是自定義標題。
getWindow().setFeatureInt定義標題的樣式。如下:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/screen"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/custom_title_left" />
<TextView android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/custom_title_right" />
</RelativeLayout>
定義瞭相對型的佈局,把title分為左右兩個部分。
11. Animation
自定義兩個Activity切換時的動畫。
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, Controls1.class));
// Supply a custom animation. This one will just fade the new
// activity on top. Note that we need to also supply an animation
// (here just doing nothing for the same amount of time) for the
// old activity to prevent it from going away too soon.
overridePendingTransition(R.anim.fade, R.anim.hold);
}
overridePendingTransition (int enterAnim, int exitAnim)必須定義在StartActivity(Intent)或是 Activity.finish()之後來定義兩個Activity切換時的動畫,enterAnim 為新Activity出現時動畫效果,exitAnim則定義瞭當前Activity退出時動畫效果。
a. 先看fade.xml和hold.xml:
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
<translate xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="0"
android:duration="@android:integer/config_longAnimTime" />
<alpha>定義透明度變化的動畫,<translate>定義平移動畫。
android:interpolator定義動畫的變化速率,可以是加速,減速,重復。
android:duration定義動畫時間。
b. 再看zoom_enter.xml和zoom_exit.xml
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
<set>是動畫類型的容器。 www.aiwalls.com
<scale>為縮放動畫。還有一個動畫是<rotate>,也就是旋轉動畫。
12. Screen Orientation
不同的屏幕方位。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Android中一共有12種方位,註釋中有它們的含義:
final static int mOrientationValues[] = new int[] {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, //默認,未定義
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, //橫向,寬大於高
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, //縱向,高大於寬
ActivityInfo.SCREEN_ORIENTATION_USER, //用戶當前首選方向
ActivityInfo.SCREEN_ORIENTATION_BEHIND, //和後臺堆棧中的activity方向相同
ActivityInfo.SCREEN_ORIENTATION_SENSOR, //根據物理傳感器方向自動變換
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR, //不根據傳感器變換
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE, //根據感應器橫向顯示
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT, //根據感應器縱向顯示
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE, //倒轉的橫向顯示,就是轉180度後。
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT, //倒轉的縱向顯示
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR, //四個方向旋轉支持
};
13. Custom Dialog
自定義對話框。
<activity android:name=".app.CustomDialogActivity"
android:label="@string/activity_custom_dialog"
android:theme="@style/Theme.CustomDialog">
在/res/values/styles.xml中定義瞭Theme.CustonDialog:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
</style>
在/drawable/filled_box.xml中的定義瞭背景:
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
<shape>默認是方形的,solid為填充的顏色,stroke是定義畫筆的寬和顏色,corners定義拐角圓弧半徑,padding是內邊距。
14. Reorder On Launch
給Activity的運行堆棧重新排序。
本例中有4個activity,用ABCD代替,如果按照順序啟動的話,堆棧為ABCD,D表示頂部,如果在D中啟動B的話,設置下面選項時:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
然後啟動B,堆棧會變成ACDB,把B移動到瞭堆頂。在沒用設置的情況下,會新啟動一個B。
15. Wallpaper
用桌面墻紙做背景。
<activity android:name=".app.WallpaperActivity"
android:label="@string/activity_wallpaper"
android:theme="@style/Theme.Wallpaper">
在/res/values/styles.xml中定義瞭Theme.Wallpaper:
<style name="Theme.Wallpaper" parent="android:style/Theme.Wallpaper">
<item name="android:colorForeground">#fff</item>
</style>
16. Set Wallpaper
設置墻紙。
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);
取得當前的墻紙,並且顯示到當前的imageView中。
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
imageView.setImageDrawable(wallpaperDrawable);
imageView.invalidate();
}
});
隨即取得一個顏色,然後以一種過濾方式組合墻紙,並且在imageView中預覽效果。
wallpaperManager.setBitmap(imageView.getDrawingCache());
getDrawingCache從imageView中取出緩存的位圖,然後設置成當前的墻紙。
摘自 小何才露尖尖角