android桌面悬浮窗显示录屏时间控制效果

作者:zhuxingchong 时间:2022-04-21 14:54:12 

本文实例为大家分享了android桌面悬浮窗,实现录屏时间控制显示效果的具体代码,供大家参考,具体内容如下

android桌面悬浮窗显示录屏时间控制效果

悬浮窗效果如上图所示:

很简单的一个布局直接上代码

悬浮窗布局如下record_screen_time_float.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">

<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/corners_bg"
 android:paddingBottom="3dp"
 android:paddingTop="3dp"
 android:paddingLeft="15dp"
 android:paddingRight="8dp"
 android:layout_gravity="center"
 android:gravity="center"
 android:orientation="horizontal">

<TextView
  android:id="@+id/record_time"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="00:00"
  android:textColor="#ffffff"
  android:textSize="10dp" />
 <View
  android:layout_width="2dp"
  android:layout_height="match_parent"
  android:layout_marginLeft="5dp"
  android:layout_marginRight="5dp"
  android:textColor="#ffffff" />
 <LinearLayout
  android:id="@+id/stop_record"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:gravity="center"
  android:orientation="horizontal">
  <ImageView
   android:id="@+id/record_hint_button"
   android:layout_width="10dp"
   android:layout_height="10dp"
   android:layout_marginRight="5dp"
   android:background="#FF4040" />

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="结束"
   android:textColor="#ffffff"
   android:textSize="10dp" />
 </LinearLayout>
</LinearLayout>
</LinearLayout>

悬浮窗是在service中拉起可以根据个人需要修改


package com.android.systemui;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

import android.graphics.PixelFormat;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.android.systemui.R;
import android.util.Log;
import java.io.File;
import java.io.IOException;

public class ScreenRecordService extends Service implements Handler.Callback {

private final String TAG = "ScreenRecordService";
private Handler mHandler;
//已经录制多少秒了
private int mRecordSeconds = 0;
private static final int MSG_TYPE_COUNT_DOWN = 110;

/**
 * 定义浮动窗口布局
 */
LinearLayout mlayout;
TextView recordTime;
/**
 * 悬浮窗控件
 */
ImageView recordHintButton;
LinearLayout stopRecord;
/**
 * 悬浮窗的布局
 */
WindowManager.LayoutParams wmParams;
LayoutInflater inflater;
/**
 * 创建浮动窗口设置布局参数的对象
 */
WindowManager mWindowManager;

//触摸 *
GestureDetector mGestureDetector;

FloatingListener mFloatingListener;

@Override
public void onCreate() {
 super.onCreate();
 initWindow();//设置窗口的参数
 initFloating();//设置悬浮窗图标
}
@Override
public void onDestroy() {
 super.onDestroy();
 try {
  if (mlayout != null) {
   // 移除悬浮窗口
   mWindowManager.removeView(mlayout);
  }
 } catch (Exception e) {
  Log.e(TAG, "not attached to window manager");
 }
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean handleMessage(Message msg) {
 switch (msg.what) {

case MSG_TYPE_COUNT_DOWN: {

mRecordSeconds++;
   int minute = 0, second = 0;
   if (mRecordSeconds >= 60) {
    minute = mRecordSeconds / 60;
    second = mRecordSeconds % 60;
   } else {
    second = mRecordSeconds;
   } String timeTip = ""+minute+":"+second;
   recordTime.setText(timeTip);
  }
   break;
  }
 }
 return true;
}

/**
 * 初始化windowManager
 */
private void initWindow() {
 if (mWindowManager == null) {
  mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
 }
 wmParams = getParams(wmParams);//设置好悬浮窗的参数
 // 悬浮窗默认显示以左上角为起始坐标
 wmParams.gravity = Gravity.LEFT | Gravity.TOP;
 //悬浮窗的开始位置,因为设置的是从左上角开始,所以屏幕左上角是x=0;y=0
 wmParams.x = 0;
 wmParams.y = 0;
 //得到容器,通过这个inflater来获得悬浮窗控件
 if (inflater == null) {
  inflater = LayoutInflater.from(getApplication());
 }
 // 获取浮动窗口视图所在布局
 if (mlayout == null) {
  mlayout = (LinearLayout) inflater.inflate(R.layout.record_screen_time_float, null);
 }
 // 添加悬浮窗的视图
 mWindowManager.addView(mlayout, wmParams);
}

/**
 * 对windowManager进行设置
 *
 * @param wmParams
 * @return
 */
public WindowManager.LayoutParams getParams(WindowManager.LayoutParams wmParams) {
 if (wmParams == null) {
  wmParams = new WindowManager.LayoutParams();
 }
 //设置window type 下面变量2002是在屏幕区域显示,2003则可以显示在状态栏之上
 //wmParams.type = LayoutParams.TYPE_PHONE;
 //wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
 wmParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
 //设置图片格式,效果为背景透明
 wmParams.format = PixelFormat.RGBA_8888;
 //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
 //wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
 //设置可以显示在状态栏上
 wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
   WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
   WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
 //设置悬浮窗口长宽数据
 wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
 wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
 return wmParams;
}

/**
 * 找到悬浮窗的图标,并且设置事件
 * 设置悬浮窗的点击、滑动事件
 */
private void initFloating() {
 recordTime = (TextView) mlayout.findViewById(R.id.record_time);
 recordHintButton = (ImageView) mlayout.findViewById(R.id.record_hint_button);
 setFlickerAnimation(recordHintButton);
 stopRecord = (LinearLayout) mlayout.findViewById(R.id.stop_record);
 mlayout.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   Log.d(TAG, "OnClickListener");
   ScreenUtil.stopScreenRecord(ScreenRecordService.this);
  }
 });
 if (mGestureDetector == null) {
  mGestureDetector = new GestureDetector(this, new MyOnGestureListener());
 }
 if(mFloatingListener == null){
  //设置 *
  mFloatingListener = new FloatingListener();
 }
 mlayout.setOnTouchListener(mFloatingListener);
 stopRecord.setOnTouchListener(mFloatingListener);
}

×录屏状态显示(闪烁效果)
×/
private void setFlickerAnimation(ImageView iv_chat_head) {
 final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
 animation.setDuration(500); // duration - half a second
 animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
 animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
 animation.setRepeatMode(Animation.REVERSE); //
 iv_chat_head.setAnimation(animation);
}

//开始触控的坐标,移动时的坐标(相对于屏幕左上角的坐标)
private int mTouchStartX, mTouchStartY, mTouchCurrentX, mTouchCurrentY;
//开始时的坐标和结束时的坐标(相对于自身控件的坐标)
private int mStartX, mStartY, mStopX, mStopY;
private boolean isMove;//判断悬浮窗是否移动

/**
 * 悬浮窗 *
 */
private class FloatingListener implements OnTouchListener {

@Override
 public boolean onTouch(View arg0, MotionEvent event) {

int action = event.getAction();
  switch (action) {
   case MotionEvent.ACTION_DOWN:
    isMove = false;
    mTouchStartX = (int) event.getRawX();
    mTouchStartY = (int) event.getRawY();
    mStartX = (int) event.getX();
    mStartY = (int) event.getY();
    break;
   case MotionEvent.ACTION_MOVE:
    mTouchCurrentX = (int) event.getRawX();
    mTouchCurrentY = (int) event.getRawY();
    wmParams.x += mTouchCurrentX - mTouchStartX;
    wmParams.y += mTouchCurrentY - mTouchStartY;
    if (mlayout != null) {
     mWindowManager.updateViewLayout(mlayout, wmParams);
    }
    mTouchStartX = mTouchCurrentX;
    mTouchStartY = mTouchCurrentY;
    break;
   case MotionEvent.ACTION_UP:
    mStopX = (int) event.getX();
    mStopY = (int) event.getY();
    if (Math.abs(mStartX - mStopX) >= 1 || Math.abs(mStartY - mStopY) >= 1) {
     isMove = true;
    }
    break;
  }
  return mGestureDetector.onTouchEvent(event); //此处必须返回false,否则OnClickListener获取不到监听
 }
}

/**
 * 自定义的手势监听类
 */
class MyOnGestureListener extends SimpleOnGestureListener {
 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
  if (!isMove) {
   System.out.println("onclick");
  }
  return super.onSingleTapConfirmed(e);
 }
}

}

来源:https://blog.csdn.net/zhuxingchong/article/details/80760179

标签:android,悬浮窗,录屏
0
投稿

猜你喜欢

  • 详解Java图形化编程中的鼠标事件设计

    2022-07-11 08:11:31
  • Java并发编程学习之Unsafe类与LockSupport类源码详析

    2022-10-21 19:07:01
  • Android如何创建桌面快捷方式

    2022-09-16 18:37:06
  • Java并发包线程池ThreadPoolExecutor的实现

    2022-11-10 09:52:41
  • Spring Cloud Gateway(读取、修改 Request Body)的操作

    2023-11-09 19:25:46
  • java Semaphore共享锁实现原理解析

    2021-11-02 23:12:38
  • Java Class 加密工具 ClassFinal详解

    2023-02-10 14:58:48
  • swing分割窗口控件JSplitPane使用方法详解

    2021-07-28 14:15:20
  • Java如何实现List自定义排序

    2021-07-03 06:09:15
  • 解析Android开发优化之:对界面UI的优化详解(一)

    2023-05-23 17:45:10
  • 详解Java中方法重写和方法重载的6个区别

    2023-11-28 13:42:18
  • C#中将xml文件反序列化为实例时采用基类还是派生类的知识点讨论

    2022-08-04 19:27:32
  • android 引导界面的实现方法

    2023-08-07 04:58:11
  • Java实例讲解动态代理

    2023-03-21 22:56:45
  • MyBatis-Plus拦截器实现数据权限控制的示例

    2022-12-10 05:10:17
  • 配合Swagger使用绝佳的两款直观易用JSON可视化工具

    2021-10-14 23:50:23
  • Flutter加载图片流程MultiFrameImageStreamCompleter解析

    2023-07-19 02:45:55
  • Java编程中实现归并排序算法的实例教程

    2023-09-10 07:40:41
  • SSH框架网上商城项目第8战之查询和删除商品类别功能实现

    2023-02-12 05:54:39
  • java8新特性教程之time包使用总结

    2023-02-08 03:58:19
  • asp之家 软件编程 m.aspxhome.com