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.2cto.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="http://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="http://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="http://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="http://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="http://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.2cto.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="http://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中取出緩衝的位元影像,然後設定成當前的牆紙。
摘自 小何才露尖尖角