赞
踩
最近在学习android,忽然思考到一些直播软件有的那种拖拽悬浮播放框的效果是如何做的,所以去网上搜了搜,特意记录下来:
两种方式都需要给控件添加onTouchListenner()监听,并处理其中的方法:
第一种:
- public class ThirdActivity extends AppCompatActivity {
-
- private Button button;
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.ac_third);
- newWindow();
- }
-
- private void newWindow() {
-
- final Button button = new Button(this);
- button = new Button(this);
- button.setBackground(getResources().getDrawable(R.drawable.shape_main_floating));
- button.setText("风险\n应急");
- button.setPadding(5,5,5,5);
- button.setTextColor(Color.parseColor("#FFFFFF"));
- button.setVisibility(View.GONE);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (riskAccidentId != -1) {
- Bundle bundle = new Bundle();
- bundle.putString("id", riskAccidentId + "");
- RouteHelper.startFragment(MangoRouteURL.RiskAccidentDetailFragment, bundle);
- }
- }
- });
- final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
- final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT
- , WindowManager.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSLUCENT);
- layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
- layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
- | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
- layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
- layoutParams.x = getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 80);
- layoutParams.y = getResources().getDisplayMetrics().heightPixels - DensityUtil.dip2px(MainBottomActivity.this, 160);
- layoutParams.width=DensityUtil.dip2px(MainBottomActivity.this, 60);
- layoutParams.height=DensityUtil.dip2px(MainBottomActivity.this, 60);
- layoutParams.windowAnimations=0;
- windowManager.addView(button, layoutParams);
- button.setOnTouchListener(new View.OnTouchListener() {
- public int lastX;
- public int lastY;
- public int X;
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- lastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
- lastY = (int) event.getRawY();
-
- X=(int)event.getRawX();
- break;
- case MotionEvent.ACTION_MOVE:
- layoutParams.x+=(int) event.getRawX()-lastX;
- layoutParams.y+=(int) event.getRawY()-lastY;
- windowManager.updateViewLayout(v,layoutParams);
- lastX= (int) event.getRawX();
- lastY = (int) event.getRawY();
- break;
- case MotionEvent.ACTION_UP:
- if (Math.abs(event.getRawX() - X) < 10) {
- button.performClick();
- }
- return true;
- }
- return true;
- }
- });
- }
- }
第二种:
- package com.xuganwen.colorfullimage;
-
- import android.content.Context;
- import android.graphics.PixelFormat;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
-
- import com.xuganwen.colorfullphoto.R;
-
- public class ThirdActivity extends AppCompatActivity {
-
- private Button button;
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.ac_third);
- newWindow();
- }
-
- private void newWindow() {
-
- button=(Button) findViewById(R.id.btn);
- button.setOnTouchListener(new View.OnTouchListener() {
- public int lastX;
- public int lastY;
- public float X;
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- lastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
- lastY = (int) event.getRawY();
- X = event.getRawX();
- break;
- case MotionEvent.ACTION_MOVE:
- //event.getRawX();获得移动的位置
- int dx = (int) event.getRawX() - lastX;
- int dy = (int) event.getRawY() - lastY;
- int l = v.getLeft() + dx;
- if (l < 0) {
- l = 0;
- } else if (l > getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 60)) {
- l = getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 60);
- }
- int b = v.getBottom() + dy;
- if (b < DensityUtil.dip2px(MainBottomActivity.this, 60)) {
- b = DensityUtil.dip2px(MainBottomActivity.this, 60);
- } else if (b > getResources().getDisplayMetrics().heightPixels) {
- b = getResources().getDisplayMetrics().heightPixels;
- }
- int r = v.getRight() + dx;
- if (r < DensityUtil.dip2px(MainBottomActivity.this, 60)) {
- r = DensityUtil.dip2px(MainBottomActivity.this, 60);
- } else if (r > getResources().getDisplayMetrics().widthPixels) {
- r = getResources().getDisplayMetrics().widthPixels;
- }
- int t = v.getTop() + dy;
- if (t < 0) {
- t = 0;
- } else if (t > getResources().getDisplayMetrics().heightPixels) {
- t = getResources().getDisplayMetrics().heightPixels - DensityUtil.dip2px(MainBottomActivity.this, 60);
- }
- v.layout(l < 0 ? 0 : l, t < 0 ? 0 : t
- , r
- , b);
- lastX = (int) event.getRawX();
- lastY = (int) event.getRawY();
- v.postInvalidate();
- break;
- case MotionEvent.ACTION_UP:
- if (Math.abs(event.getRawX() - X) < 10) {
- button.performClick(); //如果是点击行为,则执行点击事件
- }
- return true;
- }
- return true;
- }
- });
- }
- }
公用布局文件:
- <?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">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn"
- android:text="xuganwen"
- />
- </RelativeLayout>
两种方式都能保证不会滑动到屏幕外,效果都还可以,第二种效果比较推荐,欢迎评论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。