1 关于常见的对话框,主要有:

常见的对话框,单选对话框,多选对话框,进度条对话框(转圈类型的),带进度条的对话框。

案例结构:

完成如下结构的案例,将所有的案例都测试一下:

2 编写MainActivity,代码如下:

package com.itheima.dialog;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.content.DialogInterface.OnMultiChoiceClickListener;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void click1(View view) {

// 对话框的创建器

AlertDialog.Builder builder = new Builder(this);

builder.setTitle("我是对话框");

builder.setMessage("对话框显示的内容");

// 设置点击确定按钮后制定的动作

builder.setPositiveButton("确定", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(getApplicationContext(), "确定被点击了", 0).show();

}

});

    builder.setNegativeButton("取消", new OnClickListener() {// 设置取消按钮

@Override

public void onClick(DialogInterface dialog, int which) {

// 什么都不写默认实现的就是关闭掉对话框

Toast.makeText(getApplicationContext(), "点击了取消按钮",

Toast.LENGTH_LONG).show();

}

});

builder.setCancelable(false);

builder.create().show();

}

/**

* 单选对话框

*

* @param view

*/

public void click2(View view) {

// 对话框的创建器

AlertDialog.Builder builder = new Builder(this);

builder.setTitle("请选择您的性别");

final String[] items = { "男", "女", "未知" };

//这里的1表示默认选中的是哪个,0:表示选中的是第一个

builder.setSingleChoiceItems(items, 1, new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(getApplicationContext(), "您的性别:" + items[which],

0).show();

dialog.dismiss();

}

});

builder.create().show();

}

/**

* 多选对话框

* @param view

*/

public void click3(View view) {

// 对话框的创建器

AlertDialog.Builder builder = new Builder(this);

builder.setTitle("请选择你最爱吃的水果");

final String[] items = { "苹果", "梨", "菠萝", "香蕉", "黄瓜" };

final boolean[] result = new boolean[] { true, false, true, false,false};

builder.setMultiChoiceItems(items, result,

new OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which,

boolean isChecked) {

Toast.makeText(getApplicationContext(),

items[which] + isChecked, 0).show();

result[which] = isChecked;

}

});

builder.setPositiveButton("提交", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i < result.length; i++) {

if (result[i]) {

sb.append(items[i] + ",");

}

}

Toast.makeText(getApplicationContext(),

"您选中了," + sb.toString(), 0).show();

}

});

// builder.create().show();

builder.show();

}

// 进度条对话框

public void click4(View view) {

ProgressDialog pd = new ProgressDialog(this);

pd.setTitle("提醒");

pd.setMessage("正在加载数据...请稍等。");

pd.show();

}

// 带进度的进度条对话框

public void click5(View view) {

final ProgressDialog pd = new ProgressDialog(this);

pd.setTitle("提醒");

pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pd.setMax(100);

pd.setMessage("正在加载数据...请稍等。");

pd.show();

new Thread() {

public void run() {

for (int i = 0; i < 100; i++) {

try {

Thread.sleep(40);

} catch (InterruptedException e) {

e.printStackTrace();

}

pd.setProgress(i);

}

pd.dismiss();

};

}.start();

}

}

==============================================================================

1 光传感器

编写布局文件activity_main.xml

<RelativeLayout   Android指南针,案例效果:

2 编写布局文件,代码如下(activity_main.xml):

<RelativeLayout 次,表示重复播放2,如果是

// -1(Animation.INFINITE)表示一直重复播放

aa.setRepeatCount(3);

// aa.setRepeatCount(Animation.INFINITE);

// 如果不指定这个值,默认是重复播放的。下面表示:透明-->不透明-->透明

aa.setRepeatMode(Animation.REVERSE);

// true:界面为动画完成之后的效果

aa.setFillAfter(true);

// 开始播放

iv.startAnimation(aa);

}

/**

* 位移动画

*

* @param view

*/

public void trans(View view) {

// 下面表示x轴从0.0f-->1.0f;0.0f-->1.0f

// android.view.animation.TranslateAnimation.TranslateAnimation(int

// fromXType, float fromXValue, int toXType, float toXValue, int

// fromYType, float fromYValue, int toYType, float toYValue)

TranslateAnimation ta = new TranslateAnimation(

Animation.RELATIVE_TO_PARENT, // 相对于父窗体

0.0f, // 如果是320宽度的模拟器。这里0.0f表示是是父窗体的0%

Animation.RELATIVE_TO_PARENT, // 还是相对于父窗体

1.0f, // 表示父亲的100%

Animation.RELATIVE_TO_PARENT, 0.0f,

Animation.RELATIVE_TO_PARENT, 1.0f);

ta.setDuration(2000); // 设置时间间隔

ta.setRepeatCount(-1); // -1表示重复的操作

// 倒叙播放

ta.setRepeatMode(Animation.REVERSE);

iv.startAnimation(ta);

}

// 缩放动画

public void scale(View view) {

ScaleAnimation sa = new ScaleAnimation(0.1f, // 缩放的时候最开始的比例

2.0f, // 上面这两个参数x周表示从0.1倍到2倍

0.1f, 2.0f, // y轴从0.1-->2.0倍

Animation.RELATIVE_TO_SELF, // 后面4个参数的组合表示从自己中心点开始缩小放大

0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

sa.setDuration(2000); // 设置时间间隔

sa.setRepeatCount(1); // -1表示重复的操作

// 倒叙播放

sa.setRepeatMode(Animation.REVERSE);

iv.startAnimation(sa);

}

// 旋转动画

public void rotate(View view) {

RotateAnimation ra = new RotateAnimation(

0, // 开始的角度

360, // 旋转的解读

Animation.RELATIVE_TO_SELF,

0.0f,

Animation.RELATIVE_TO_SELF,

0.0f);

ra.setDuration(2000);

ra.setRepeatCount(1);

ra.setRepeatMode(Animation.REVERSE);

iv.startAnimation(ra);

}

//动画组合(包含多种动画)

public void set(View view) {

AnimationSet set = new AnimationSet(false);

TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f,

Animation.RELATIVE_TO_PARENT, 0.5f,

Animation.RELATIVE_TO_PARENT, -0.5f,

Animation.RELATIVE_TO_PARENT, 0.5f);

ta.setDuration(2000);

ta.setRepeatCount(1);

ta.setRepeatMode(Animation.REVERSE);

ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF,

0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

sa.setDuration(2000);

sa.setRepeatCount(1);

sa.setRepeatMode(Animation.REVERSE);

RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,

0.0f, Animation.RELATIVE_TO_SELF, 0.0f);

ra.setDuration(2000);

ra.setRepeatCount(1);

ra.setRepeatMode(Animation.REVERSE);

set.addAnimation(ra);

//set.addAnimation(ta);

set.addAnimation(sa);

iv.startAnimation(set);

}

}

=============================================================================

1 除了通过代码的方式制作补间动画之外,还可以通过xml的方式制作补间动画。

案例:

2 下面通过如下结构的代码编写出上面的案例:

3 编写的布局文件activity_main.xml如下:

<RelativeLayout 秒

android:repeatCount="1"      重复1次

android:repeatMode="reverse"

-->

<translate 毫秒 -->

<item

android:drawable="@drawable/girl_1"

android:duration="200"/>

<item

android:drawable="@drawable/girl_2"

android:duration="200"/>

<item

android:drawable="@drawable/girl_3"

android:duration="200"/>

<item

android:drawable="@drawable/girl_4"

android:duration="200"/>

<item

android:drawable="@drawable/girl_5"

android:duration="200"/>

<item

android:drawable="@drawable/girl_6"

android:duration="400"/>

<item

android:drawable="@drawable/girl_7"

android:duration="400"/>

<item

android:drawable="@drawable/girl_6"

android:duration="400"/>

<item

android:drawable="@drawable/girl_7"

android:duration="400"/>

<item

android:drawable="@drawable/girl_6"

android:duration="400"/>

<item

android:drawable="@drawable/girl_7"

android:duration="400"/>

<item

android:drawable="@drawable/girl_8"

android:duration="200"/>

<item

android:drawable="@drawable/girl_9"

android:duration="200"/>

<item

android:drawable="@drawable/girl_10"

android:duration="200"/>

<item

android:drawable="@drawable/girl_11"

android:duration="200"/>

</animation-list>

2 编写MainActivity,代码如下:

package com.itheima.frameanimation;

import android.app.Activity;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

import android.view.MotionEvent;

import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView iv;

private AnimationDrawable mAnimationDrawable;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

iv = (ImageView) findViewById(R.id.iv);

// 把xml文件的动画资源设置为iv背景

iv.setBackgroundResource(R.drawable.girl);

// 获取设置的动画资源。 执行可能需要花费一定的时间

mAnimationDrawable = (AnimationDrawable) iv.getBackground();

}

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

mAnimationDrawable.start();

return true;

}

return super.onTouchEvent(event);

}

}



21_Android中常见对话框,光传感器,通过重力感应器编写出指南针应用,帧动画,通过Jav代码的方式编写补间动画,通过XML的方式编写补间动画的更多相关文章

  1. android中常见对话框之一AlertDialog

    在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...

  2. Android开发中常见的设计模式(二)——Builder模式

    了解了单例模式,接下来介绍另一个常见的模式--Builder模式. 那么什么是Builder模式呢.通过搜索,会发现大部分网上的定义都是 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建 ...

  3. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  4. 云服务器 ECS Linux 系统中常见的日志文件介绍

    云服务器 ECS Linux 系统中,日志文件是非常重要的文件,它们记录了很多系统中重要的事.Linux 系统中常见日志文件概述如下: /var/log/cron可以在 cron 文件中检查 cron ...

  5. 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能

    package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...

  6. Java开发中常见的危险信号(中)

    本文来源于我在InfoQ中文站原创的文章,原文地址是:http://www.infoq.com/cn/news/2013/12/common-red-flags-in-java-1 Dustin Ma ...

  7. shell脚本中常见的一些特殊符号和作用详解

    这篇文章主要介绍了shell脚本中常见的一些特殊符号和它的作用详解,总结的很简洁,容易看懂,需要的朋友可以参考下   在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我 ...

  8. 对开发中常见的内存泄露,GDI泄露进行检测

    对开发中常见的内存泄露,GDI泄露进行检测 一.GDI泄露检测方法: 在软件测试阶段,可以通过procexp.exe 工具,或是通过任务管理器中选择GDI对象来查看软件GDI的对象是使用情况. 注意点 ...

  9. java项目中常见的异常及处理

    Java开发中常见异常及处理方法 1.JAVA异常 异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API ...

随机推荐

  1. XCode使用技巧

    XCode使用技巧 自动生成get.set方法 @property 用法 #import <Foundation/Foundation.h> @interface People : NSO ...

  2. mybatis insert 返回主键

    分享牛,分享牛原创.ssm整合的时候,我们操作mybatis insert 的时候,需要返回插入的主键,因为主键是自增的,这个时候怎么办呢?很简单看一下下面的代码示例: 1.1.1. 代码定义 pub ...

  3. Spring之MVC模块

    Spring MVC的Controller用于处理用户的请求.Controller相当于Struts 1里的Action,他们的实现机制.运行原理都类似 Controller是个接口,一般直接继承Ab ...

  4. SpriteKit给游戏弹跳角色添加一个高度标示器

    这是一个类似于跳跃涂鸦的小游戏,主角不断吃能量球得到跳跃能量向更高的地方跳跃,如果图中碰到黑洞就挂了- 在游戏调试过程中如果能实时知道主角的高度就好了,这将有助于程序猿动态的判断游戏胜败逻辑. 你可以 ...

  5. makefile的命令包定义及使用

    下面以\build\core\product.mk下面的内容为例介绍: define _find-android-products-files $(shell test -d device & ...

  6. Impala中的代码生成技术

    Cloudera Impala是一种为Hadoop生态系统打造的开源MPP(massive parallel processing)数据库,它主要为分析型查询负载而设计,而非OLTP.Impala能最 ...

  7. 【OpenGL】详解第一个OpenGL程序

    写在前面 OpenGL能做的事情太多了!很多程序也看起来很复杂.很多人感觉OpenGL晦涩难懂,原因大多是被OpenGL里面各种语句搞得头大,一会gen一下,一会bind一下,一会又active一下. ...

  8. Linux系统编程---守护进程

    守护进程是什么?就是在后台运行的进程. 那么如何创建守护进程呢? 1. 创建孤儿进程 2. setsid() 创建进程会话 3. 重定向标准输入, 标准输出 4. chdir, 改当当前进程的工作目录 ...

  9. HTML5中 HTML表单和PHP环境搭建及与PHP交互 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 知识点概括:HTML表单/PHP环境搭建/表单提交数据与PHP交互 第一部分:HTML表单 <!DOCTYP ...

  10. Cassandra使用pycassa批量导入数据

    本周接手了一个Cassandra系统的维护工作,有一项是需要将应用方的数据导入我们维护的Cassandra集群,并且为应用方提供HTTP的方式访问服务.这是我第一次接触KV系统,原来只是走马观花似的看 ...