android138 360 小火箭

package com.itheima52.rocket; import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView; /*
* 主界面开启一个服务
startService(new Intent(this, RocketService.class));
finish();
stopService(new Intent(this, RocketService.class));
finish();
*/
public class RocketService extends Service { private WindowManager.LayoutParams params;
private int winWidth;
private int winHeight;
private WindowManager mWM;
private View view; private int startX;
private int startY; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate(); mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);//WindowManager可以在手机屏幕上显示,而不用依赖于某个activity布局文件。 // 获取屏幕宽高
winWidth = mWM.getDefaultDisplay().getWidth();
winHeight = mWM.getDefaultDisplay().getHeight(); params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;// 电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。
params.gravity = Gravity.LEFT + Gravity.TOP;// 将重心位置设置为左上方,
// 也就是(0,0)从左上方开始,而不是默认的重心位置
params.setTitle("Toast"); view = View.inflate(this, R.layout.rocket, null);// 初始化火箭布局 // 初始化火箭帧动画
ImageView ivRocket = (ImageView) view.findViewById(R.id.iv_rocket);
/*iv_rocket.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_rocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/desktop_rocket_launch_1" /> 火箭图片
</LinearLayout>*/ //火箭冒火帧动画,火苗一直闪烁,
ivRocket.setBackgroundResource(R.drawable.anim_rocket);
/*
anim_rocket.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
2张图片切换,200毫秒,
<item
android:drawable="@drawable/desktop_rocket_launch_1"
android:duration="200"/>
<item
android:drawable="@drawable/desktop_rocket_launch_2"
android:duration="200"/>
</animation-list>
*/
AnimationDrawable anim = (AnimationDrawable) ivRocket.getBackground();
anim.start(); mWM.addView(view, params); view.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 初始化起点坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int endX = (int) event.getRawX();
int endY = (int) event.getRawY(); // 计算移动偏移量
int dx = endX - startX;
int dy = endY - startY; // 更新浮窗位置,WindowManager里面只能用params来移动
params.x += dx;
params.y += dy; // 防止坐标偏离屏幕
if (params.x < 0) {
params.x = 0;
}
if (params.y < 0) {
params.y = 0;
} // 防止坐标偏离屏幕
if (params.x > winWidth - view.getWidth()) {
params.x = winWidth - view.getWidth();
}
if (params.y > winHeight - view.getHeight()) {
params.y = winHeight - view.getHeight();
}
// System.out.println("x:" + params.x + ";y:" + params.y);
mWM.updateViewLayout(view, params); // 重新初始化起点坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
if (params.x > 100 && params.x < 250 && params.y > winHeight - 120) {
System.out.println("发射火箭!!!");
sendRocket(); // 启动烟雾效果,启动一个Activity,盖住下面的Activity,使得下面的不能点击。
Intent intent = new Intent(RocketService.this,BackgroundActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 通过Service启动Activity,Service不用栈,因此要先启动一个栈来存放activity
startActivity(intent);
}
break; default:
break;
}
return true;
}
});
} private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 更新火箭位置
int y = msg.arg1;
params.y = y;
mWM.updateViewLayout(view, params);
};
}; /**
* 发射火箭
*/
protected void sendRocket() {
// 设置火箭居中发射
params.x = winWidth / 2 - view.getWidth() / 2;
mWM.updateViewLayout(view, params); new Thread() {
@Override
public void run() {
int pos = 380;// 移动总距离
for (int i = 0; i <= 10; i++) {//移动10次,就移动到顶部了。
// 等待一段时间再更新位置,用于控制火箭速度
try {
Thread.sleep(50);//主线程睡眠会让主线程很卡,因此要用子线城。
} catch (InterruptedException e) {
e.printStackTrace();
}
int y = pos - 38 * i;
Message msg = Message.obtain();//不能在子线城更新界面
msg.arg1 = y;
mHandler.sendMessage(msg);
}
}
}.start();
} @Override
public void onDestroy() {
super.onDestroy();
if (mWM != null && view != null) {
mWM.removeView(view);
view = null;
}
}
}
package com.itheima52.rocket; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
/**
* 烟雾背景
*/
public class BackgroundActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_bg);
/*
activity_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/desktop_smoke_m" /> 底下的大烟图片
<ImageView
android:id="@+id/iv_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/iv_bottom"
android:layout_alignParentLeft="true"
android:src="@drawable/desktop_smoke_t" /> 中间的细长的烟
</RelativeLayout>*/
ImageView ivTop = (ImageView) findViewById(R.id.iv_top);
ImageView ivBottom = (ImageView) findViewById(R.id.iv_bottom); // 渐变动画
AlphaAnimation anim = new AlphaAnimation(0, 1);
anim.setDuration(1000);
anim.setFillAfter(true);// 动画结束后保持状态 // 运行动画
ivTop.startAnimation(anim);
ivBottom.startAnimation(anim); new Handler().postDelayed(new Runnable() {//Handler可以发message也可以发Runnable
@Override
public void run() {
finish();
}
}, 1000);// 延时1秒后结束activity,不能直接调用finish,因为动画要执行1秒钟。
}
}

activity的启动模式:
1.Standard:启动A,然后A启动B,B再启动A。
2.SingleTask:首先启动A,A启动B,B启动C,C启动A的时候把B,C干掉,也就是栈里面只能有一个A.
3.SingleTop:启动A,A启动B,B启动C,C再启动C时不会创建新的C而是用原来的C,因为顶上只能有一个C.
4.SingleInstance:只有一个A,不如许别的进来.
android138 360 小火箭的更多相关文章
- android桌面小火箭升空动画
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceS ...
- Android简易实战教程--第十话《模仿腾讯手机助手小火箭发射详解》
之前对系统自带的土司的源码做了简要分析,见博客:点击打开链接 这一篇给一个小案例,自定义土司,模拟腾讯卫士的小火箭发射.如果想要迅速看懂代码,建议先去看一下上篇介绍点击打开链接 首先,定义一个服务,在 ...
- Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略
Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略 事实上写这个也是因为自己实际在项目中用到了才会去研究已经写文章,对于View的移动,其实说实话,已经有很多文章 ...
- Android仿腾讯手机管家实现桌面悬浮窗小火箭发射的动画效果
功能分析: 1.小火箭游离在activity之外,不依附于任何activity,不管activity是否开启,不影响小火箭的代码逻辑,所以小火箭的代码逻辑是要写在服务中: 2.小火箭挂载在手机窗体之上 ...
- 用jquery实现小火箭到页面顶部的效果
恩,不知道之前在哪看过一个页面效果就是如果页面被滑动了就出现一个小火箭,点击这个小火箭就可以慢慢回到页面顶部,闲的没事,自己搞了一下 需要引入jquery 代码和布局都很简单 <!DOCTYPE ...
- 第50天:scrollTo小火箭返回顶部
scrollTo(x,y)//可把内容滚动到指定的坐标scrollTo(xpos,ypos)//x,y值必需 1.固定导航栏 <!DOCTYPE html> <html lang=& ...
- Androd自己定义控件(三)飞翔的小火箭
在前面的自己定义控件概述中已经跟大家分享了Android开发其中自己定义控件的种类. 今天跟大家分享一个非主流的组合控件. 我们在开发其中,难免须要在不同的场合中反复使用一些控件的组合.而Java的最 ...
- 自制window下core animation引擎 - demo第二弹 - 仿QQ电脑管家加速小火箭
一年前想写一个像cocoa那样,可以方便层动画开发的引擎,写着写着又逆向它的QuartzCore.framework,也就是CoreAnimation的底层,已经大半年没有搞windows这个引擎.大 ...
- HTML——动画效果回到顶层(小火箭)
一.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
随机推荐
- 【九度OJ】题目1009-二叉搜索树
题目 思路 构建二叉搜索树,并保存先序遍历和中序遍历的序列在samplePreOrder,sampleInOrder 每遇到一个新的序列,构建一棵二叉搜索树,保存先序遍历和中序遍历的序列testPre ...
- spoj 839 Optimal Marks(二进制位,最小割)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...
- LyX转Word
写毕业论文是一件非常繁锁的事情,一大堆的图片.公式都要往上贴,有时弄不好就把编号搞错了,有时可能没注意,一不小心字体格式.版面格式又全乱了.怎么办?--其实这只是在word环境下才会有的烦恼. 对于w ...
- CentOS搭建LAMP环境
最近准备安装roundcube,需要先搭建一个 LAMP 运行环境,从网上搜索了一下,有不少资料.自己也按部就班安装了一遍,把过程整理了下来. LAMP 是Linux, Apache, MySQL, ...
- adb常用命令介绍
adb connect 命令格式:adb connect <host>[:<port>] 作用:connect to a device via TCP/IP,Port 5555 ...
- wireshark筛选器汇总
抓取指定IP地址的数据流: 如果你的抓包环境下有很多主机正在通讯,可以考虑使用所观察主机的IP地址来进行过滤.以下为IP地址抓包过滤示例: host 10.3.1.1:抓取发到/来自10.3.1.1的 ...
- MYSQL event_scheduler
一.概述 事件调度器是在 MySQL 5.1 中新增的另一个特色功能,可以作为定时任务调度器,取代部分原先只能用操作系统任务调度器才能完成的定时功>能.例如,Linux 中的 crontabe ...
- Struts2在Action中访问WEB资源
什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...
- 【转】深受开发者喜爱的10大Core Data工具和开源库
http://www.cocoachina.com/ios/20150902/13304.html 在iOS和OSX应用程序中存储和查询数据,Core Data是一个很好的选择.它不仅可以减少内存使用 ...
- Linux下MongoDB备份脚本
#!/bin/bash today=`date +%Y%m%d` mongodump -h localhost -d salary -o /home/chzhao/mongobackup/$today ...