用Xamarin 实现园友的 :Android浮动小球与开机自启动

原文: 用Xamarin 实现园友的 :Android浮动小球与开机自启动

前两天看园子里有筒子写了个 Android浮动小球与开机自启动  , 感受这种被 360 玩烂的功能原来是如此的简单啊。。。html

我不会 Java, 固然也不懂如何 用 ADT 开发 Android App.java

可是我看过几天 Xamarin 啊,C# 我仍是会的。。android

用 Xamarin 作Android , 只要对着 java 源码, 我仍是能翻译一份出来的。git

我第一时间安装了 VS2015 , 挺好的,自带的 Android 模拟器, 比那个第三方的 Genymotion 模拟器快不少github

不过 Xamarin 依然是收费的, 可是在镇上,你们都知道,哈哈。app

先上个图给你们看看我翻译的结果:ide

照例,源码:post

https://github.com/gruan01/Xamarin-Example/tree/master/FloatBallthis

 

Xamarin 声明一个 Service 要这样写:url

1 namespace FloatBall {
2     [Service]
3     [IntentFilter(new string[] { "Xamarin.BallService" })]
4     public class BallService : Service, View.IOnTouchListener, View.IOnClickListener {

后面那两个接口不是必须的。

IntentFilter 也不是必须的。

 

运行这个 Service ,要在 MainActivity 里这样写:

 1 protected override void OnCreate(Bundle bundle) {
 2             base.OnCreate(bundle);
 3            。。。
 4            。。。
 5 
 6             this.Finish();
 7 
 8             //var intent = new Intent(Application.Context, typeof(BallService));
 9             var intent = new Intent("Xamarin.BallService");
10             this.StartService(intent);
11         }

 

在 Service 里, 若是想获取到 WindowManager 要这样写:

1 private IWindowManager WindowManager {
2             get {
3                 //不是简单的类型转换,必定要用 JavaCast
4                 return this.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
5             }
6         }

注意,不是简单的强制类型转换, 必定要用 JavaCast

 

给 WindowManager 添加一个子视图

这个真是有点造蛋啊。一开始没有设置 LayoutParameter 的 Type

param.Type = WindowManagerTypes.Phone;

一直报这个错:

android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application

搜了好多网页,说的基本上都同样, 只是不适用我这个场景。

后来仔细看了一下园友的文章,发现少了 param.Type 这个参数。。。,加上就OK了!

我能说这是个奇葩吗?

 

FrameLayout & Animation

不明白为何Animation 应到 FrameLayout 上,为何一点做用都不起。

应用到 ImageView 上效果立马就出来了。

不知道是我写法不对,仍是错觉。

 

最后请教一下:

如图所示,我对 FrameLayout 作了 ShapeDrawable。

其实我是想显示出来一个圆型的图片出来。。

请问该如何实现?

 

谢谢围观。

相关文章
相关标签/搜索