Android API Demos学习 - Activity部分
3140 点击·0 回帖
![]() | ![]() | |
![]() | 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.atcpu.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="Androidtyle/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">@Androidtyle/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="@Androidtyle/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.atcpu.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="Androidtyle/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="Androidtyle/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中取出缓存的位图,然后设置成当前的墙纸。 摘自 小何才露尖尖角 | |
![]() | ![]() |