Android浮动小球与开机自启动

  看着手机上的360浮动小球,不评价其具体的功能与实用性,至少在UI设计与交互方面是个不小的创新。

Android浮动小球与开机自启动

  如图片左上角所示,球中还会显示当前手机的运行状况,向下拉动还会有弹射来达到加速、清理等目的。

  那好,先来实现一个类似的小球(仅限于形状,功能你懂得)。

  查阅了相关资料,整个界面除了小球以外,其他部分均是做透明处理。

  1、由于用到了CompatModeWrapper,所以需要在AndroidManifest.xml中添加以下权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

  要想在应用中被启动为一个Activity或Service,需要注册,这里是Service:

<service android:name=".Service1" />

  2、界面透明化处理,整体代码如下:

package com.XXX.autostart;

import android.view.View;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class Service1 extends Service
{
    LinearLayout mFloatLayout;
    WindowManager.LayoutParams wmParams;
    WindowManager mWindowManager;

ImageButton mFloatView;

@Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();

createFloatView();
    }

@Override
    public IBinder onBind(Intent intent)
    {
        // TODO Auto-generated method stub
        return null;
    }

private void createFloatView()
    {
        wmParams = new WindowManager.LayoutParams();

mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);

wmParams.type = LayoutParams.TYPE_PHONE;
        wmParams.format = PixelFormat.RGBA_8888;

wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;

wmParams.gravity = Gravity.LEFT | Gravity.TOP;

wmParams.x = 0;
        wmParams.y = 0;

wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

LayoutInflater inflater = LayoutInflater.from(getApplication());
        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);

mWindowManager.addView(mFloatLayout, wmParams);

mFloatView = (ImageButton)mFloatLayout.findViewById(R.id.float_id);

mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


        mFloatView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;

wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;

mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                return false;
            }
        });

mFloatView.setOnClickListener(new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mFloatView.setVisibility(View.INVISIBLE);

Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        mFloatView.setVisibility(View.VISIBLE);
                    }
                }, 3000);

}
        });
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ce1094637d1a8c4ef319a029edb7a334.html