Android对话框和帧动画
Android对话框
在一个例子中展示四种对话框。
设置四个按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt_dialog1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框" />
<Button
android:id="@+id/bt_dialog2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" />
<Button
android:id="@+id/bt_dialog3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" />
<Button
android:id="@+id/bt_dialog4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条对话框" />
</LinearLayout>
分别是普通对话框、单选对话框、多选对话框、进度条对话框
package com.example.stylethemetest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button btDialog1 = (Button) findViewById(R.id.bt_dialog1);
Button btDialog2 = (Button) findViewById(R.id.bt_dialog2);
Button btDialog3 = (Button) findViewById(R.id.bt_dialog3);
Button btDialog4 = (Button) findViewById(R.id.bt_dialog4);
btDialog1.setOnClickListener(this);
btDialog2.setOnClickListener(this);
btDialog3.setOnClickListener(this);
btDialog4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 1. 普通对话框
case R.id.bt_dialog1:
AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(mContext);
alertDialog1.setTitle("注意");
alertDialog1.setMessage("真的要删除吗?");
alertDialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.show();
break;
// 2. 单选对话框
case R.id.bt_dialog2:
final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(mContext);
alertDialog2.setTitle("选择学历");
final String[] edu = {"小学", "初中", "高中", "本科", "研究生", "博士", "其他"};
// 选择默认选中,-1表示不选中
alertDialog2.setSingleChoiceItems(edu, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String eduLevel = edu[which];
Toast.makeText(mContext, eduLevel+which, Toast.LENGTH_SHORT).show();
}
});
alertDialog2.setPositiveButton("选择", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO:处理确定逻辑
}
});
alertDialog2.show();
break;
// 3. 多选对话框
case R.id.bt_dialog3:
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(mContext);
alertDialog3.setTitle("选择你喜欢吃的水果");
final String[] items = {"榴莲", "苹果", "葡萄", "香蕉", "黄瓜", "火龙果", "荔枝"};
// bool值和上面的条目对应,true表示默认选中
final boolean[] checkedItems = {false, true, false, true, false,
false, false};
alertDialog3.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 下标和是否被选中
Toast.makeText(mContext, which+ " " +isChecked, Toast.LENGTH_SHORT).show();
}
});
alertDialog3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder fruits = new StringBuilder();
for (int i = 0; i < items.length; i++) {
if (checkedItems[i]) {
// 就证明是选中的
String fruit = items[i];
fruits.append(fruit).append(" ");
}
}
Toast.makeText(mContext, fruits, Toast.LENGTH_SHORT).show();
}
});
// show里面调用了create
alertDialog3.show();
break;
// 4. 进度条对话框
case R.id.bt_dialog4:
final ProgressDialog progressDialog = new ProgressDialog(mContext);
// 默认STYLE_SPINNER,小圆圈选装。可选水平进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("加载");
progressDialog.setMessage("Loading...");
// false表示用户可不能通过按下back键或者对话框外的地方退出,默认是true。
progressDialog.setCancelable(true);
progressDialog.show();
// 进入条相关组件可以在子线程更新UI
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 101; i++) {
// 与Thread.sleep不同的是,他不会抛出interruptedException;
SystemClock.sleep(50);
progressDialog.setProgress(i);
}
// cancel也调用了dismiss,不过还可可响应setOnCancelListener
// hide只是隐藏,没有dismiss掉
progressDialog.dismiss();
}
}).start();
break;
}
}
}

上图是普通对话框

上图是单选对话框

上图是多选对话框

上图是进度条对话框
Android帧动画初步认识
其实就是加载一系列的图片资源
- 首先放入一系列图片放入res/drawable中,我放入了drawable-xxhdpi。
- 在上述文件夹下新建一个xml,如下。
android:oneshot="false"表示循环一次后还接着循环。true的话就是循环一次停止在最后一帧。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/a" android:duration="500" />
<item android:drawable="@drawable/b" android:duration="500" />
<item android:drawable="@drawable/c" android:duration="500" />
<item android:drawable="@drawable/d" android:duration="500" />
<item android:drawable="@drawable/e" android:duration="500" />
</animation-list>
主界面就放一个ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.animationtest.MainActivity">
<ImageView
android:id="@+id/iv_animation"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
MainActivity
package com.example.animationtest;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView rocketImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rocketImage = (ImageView) findViewById(R.id.iv_animation);
// [1]找到ImageView控件 用来显示动画效果
rocketImage = (ImageView) findViewById(R.id.iv_animation);
// [2]设置背景资源, 传入的是xml
rocketImage.setBackgroundResource(R.drawable.my_animation);
// [3]获取AnimationDrawable 类型
AnimationDrawable ad = (AnimationDrawable) rocketImage.getBackground();
// [4]开始执行动画
ad.start();
}
}
结果如下图片所示,其实是动态的。截图成静态了。




by @sunhaiyu
2017.5.18
Android对话框和帧动画的更多相关文章
- Android简单逐帧动画Frame的实现(三)
android之动画(三)通过AnimationDrawable控制逐帧动画 android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...
- Android简单逐帧动画Frame的实现(二)
Android简单逐帧动画Frame的实现 Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...
- Android中的帧动画与补间动画的使用
前言 在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现. Android提供了3种类型的动画: 补间动画:补间动画能够应用于View,让你能够定义一 ...
- Java乔晓松-android中的帧动画FrameByFrame
先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...
- Android动画系列之帧动画和补间动画
原文首发于微信公众号:jzman-blog,欢迎关注交流! Android 提供三种动画:帧动画.补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复 ...
- Android--逐帧动画FrameAnimation
前言 开门见山,本篇博客讲解一下如何在Android平台下播放一个逐帧动画.逐帧动画在Android下可以通过代码和XML文件两种方式定义,本篇博客都将讲到,最后将以一个简单的Demo来演示两种方式定 ...
- Android基础笔记(十)- 帧动画、补间动画具体解释、对话框
帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...
- Android动画效果之Frame Animation(逐帧动画)
前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...
- android 帧动画,补间动画,属性动画的简单总结
帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...
随机推荐
- .net 4.0 中的特性总结(三):垃圾回收
1.内存基础知识 每个进程都有其自己单独的虚拟地址空间. 同一台计算机上的所有进程共享相同的物理内存,如果有页文件,则也共享页文件. 默认情况下,32 位计算机上的每个进程都具有 2 GB 的用户模式 ...
- 大话Python正则表达式
python的正则表达式模块re import re match_object=re.compile(r"") result=re.match(match_object," ...
- MVC之前-ASP.NET初始化流程分析1
Asp.net Mvc是当前使用比较多的web框架,也是比较先进的框架.我打算根据自己的实际项目经验以及相关的源码和一些使用Asp.net Mvc的优秀项目(主要是orchard)来说一说自己对于As ...
- 【面经】腾讯和YY实习生面试总结
[前言] 之前的四月份和五月份各面试了腾讯和YY的暑假实习,腾讯的失败了,YY的成功了.面试中我总会遇到自己不懂的,所幸的是不懂的越来越少,自己也一步一脚印得攻克自己不懂的.此时六月份的我再回顾起来, ...
- js函数验证方式:验证是否是数字,支持小数,负数
验证 datatype="/^\d+(\.\d+)?$/" validatform验证是否是数字 支持小数点 datatype="d" 貌似支持小数 js函数验 ...
- asp.net使用qq邮箱发送邮件
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- 【LeetCode】168. Excel Sheet Column Title
题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...
- Openfire的web插件开发
概要 Openfire不仅支持普通插件开发,还支持完整的web插件开发,这次就web插件开发做一个小的实例,本文主要讲解如何加入Servlet和Jsp页面,基本插件的开发请参照上一篇文章. 准备 系统 ...
- 序列、视图、索引(面试看这个就GO了)
oracle内置对象 序列.视图.索引 序列 create sequence aaa start with 1; 使用 视图 创建好之后 然后直接用 就OK了 有了视图可以代替子查询,使得sql简洁 ...
- Apache+Tomcat实现动静分离
完成Tomcat集群搭建后,我们只需修改两.三处即可实现动静分离. 1.将原来httpd.conf中JkMount的路由规则都放入conf/extra/httpd-urimap.conf中: /*=l ...