展会信息港展会大全

Android API Demos学习 - Activity部分
来源:互联网   发布日期:2016-01-14 10:52:59   浏览:1464次  

导读:1.Hello World基本的显示 Android API Demos学习(1) - Hello World2.匀愀瘀攀 & Restore State保存和恢复UI状态。 ...

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中取出缓存的位图,然后设置成当前的墙纸。

摘自 小何才露尖尖角

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港