发布于 2017-09-21 00:57:27 | 11713 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android手机悬浮窗口小案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

–主页面——–


//布局中就一个Button
public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
   //目的就是启动Service来打开悬浮窗体
    startService(new Intent(MainActivity.this, FloatService.class));
    finish();
   }
  });

 }
}

—Service开启悬浮窗体——-


/**
 * description:通过Service来开启FloatView
 * 作者:ldm
 * 时间:20162016/8/17 14:03
 * 邮箱:1786911211@qq.com
 */
public class FloatService extends Service {
 @Override
 public void onCreate() {
  super.onCreate();
  CustomViewManager.getInstance(this).showFloatViewOnWindow();
 }

 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }
}

—悬浮窗体管理工具类——-


/**
 * description:
 * 作者:ldm
 * 时间:20162016/8/17 11:57
 * 邮箱:1786911211@qq.com
 */
public class CustomViewManager {
 //上下文
 private Context mContext;
 //本类实例
 private static CustomViewManager instance;
 //自定义的FloatView
 private FloatView mFloatView;
 //窗口管理类
 private WindowManager mWindowManager;

 private CustomViewManager(Context context) {
  this.mContext = context;
  mFloatView = new FloatView(mContext);
  mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
 }

 /**
  * @param
  * @description 通过单例模式获取实例对象
  * @author ldm
  * @time 2016/8/17 11:59
  */
 public static CustomViewManager getInstance(Context mContext) {
  if (null == instance) {
   synchronized (CustomViewManager.class) {
    if (null == instance) {
     instance = new CustomViewManager(mContext);
    }
   }
  }
  return instance;
 }

 /**
  * @param
  * @description 在手机屏幕上显示自定义的FloatView
  * @author ldm
  * @time 2016/8/17 13:47
  */
 public void showFloatViewOnWindow() {
  WindowManager.LayoutParams parmas = new WindowManager.LayoutParams();
  parmas.width = mFloatView.getFloatWidth();
  parmas.height = mFloatView.getFloatHeight();
  //窗口图案放置位置
  parmas.gravity = Gravity.LEFT | Gravity.CENTER;
  // 如果忽略gravity属性,那么它表示窗口的绝对X位置。
  parmas.x = 0;
  //如果忽略gravity属性,那么它表示窗口的绝对Y位置。
  parmas.y = 0;
  ////电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。
  parmas.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  //FLAG_NOT_FOCUSABLE让window不能获得焦点,这样用户快就不能向该window发送按键事件及按钮事件
  //FLAG_NOT_TOUCH_MODAL即使在该window在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window.
  parmas.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  // 期望的位图格式。默认为不透明。参考android.graphics.PixelFormat。
  parmas.format = PixelFormat.RGBA_8888;
  mWindowManager.addView(mFloatView, parmas);
 }
}

—自定义的FloatView——-


/**
 * description:
 * 作者:ldm
 * 时间:20162016/8/17 11:17
 * 邮箱:1786911211@qq.com
 */
public class FloatView extends View {
 //悬浮球宽度
 private int floatWidth = 150;
 //悬浮球高度
 private int floatHeight = 150;
 //悬浮球画笔
 private Paint mPaint;
 //绘制文字画笔
 private Paint mTextPaint;
 private String text = "50%";

 public FloatView(Context context) {
  super(context);
  initPaint();
 }


 public int getFloatWidth() {
  return floatWidth;
 }


 public int getFloatHeight() {
  return floatHeight;
 }

 public FloatView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initPaint();
 }

 public FloatView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initPaint();
 }

 /**
  * @param
  * @description 初始化画笔
  * @author ldm
  * @time 2016/8/17 11:37
  */
 private void initPaint() {
  //设置悬浮球画笔
  mPaint = new Paint();
  mPaint.setColor(Color.GREEN);
  mPaint.setAntiAlias(true);
  mPaint.setDither(true);
  //设置文字画笔
  mTextPaint = new Paint();
  mTextPaint.setTextSize(25);
  mPaint.setAntiAlias(true);
  mTextPaint.setColor(Color.WHITE);
  mTextPaint.setFakeBoldText(true);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  //设置大小
  setMeasuredDimension(floatWidth, floatHeight);
 }

 /**
  * @param
  * @description 绘制图案
  * @author ldm
  * @time 2016/8/17 11:44
  */
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  //绘制悬浮球
  canvas.drawCircle(floatWidth / 2, floatHeight / 2, floatWidth / 2, mPaint);
  //绘制文字
  Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
  //文字大小计算可以参考:http://mikewang.blog.51cto.com/3826268/871765/
  float textWidth = mTextPaint.measureText(text);
  float x = floatWidth / 2 - textWidth / 2;
  float dy = -(metrics.descent + metrics.ascent) / 2;
  float y = floatHeight / 2 + dy;
  canvas.drawText(text, x, y, mTextPaint);
 }
}


最后,在AndroidManifest.xml中不要忘记添加权限:


 <!--添加权限-->
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />


当然也要记得添加Service组件


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


有的手机运行后,发现没有出现我们想要的悬浮窗体,可以进入手机设置中心,点击应用设置,在指定的应用权限设置中打开悬浮窗体相应的设置开关即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务