关闭

自定义控件(22)---FloatView悬浮窗(1)

1321人阅读 评论(0) 收藏 举报
分类:

点击打开链接,现在demo。。。。


自定义控件(23)---FloatView悬浮窗(2)

提问:我的代码有个问题,初始化默认2个绿色的imageView打开状态,此刻点击,让其收回,在点击让其打开,结果bug是2个绿色的imageview位置没有正确的放置

但是这个demo在手机N5上显示正常的,重点看下这个打开的操作,不知道哪里错了,应该是不兼容的问题,望网友解答,多谢

public void open() {
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(btnEncryption, "x",
				434.5f, 0f);
		anim2.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
			}

			@Override
			public void onAnimationEnd(Animator animation) {
				if (animListener != null) {
					animListener.onAnimOpenEnd(null);
				}
			}

			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
		anim2.setDuration(DURAIION);

		ObjectAnimator anim4 = ObjectAnimator.ofFloat(btnTransmission, "x",
				434.5f, 225f);
		anim4.setDuration(DURAIION);

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.play(anim2).with(anim4);
		animatorSet.start();

		isOpen = true;
	}



activity_main.xml看主界面的布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#ffffff"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnstart"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="悬浮开"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnstop"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="悬浮关"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>

MainActivity-看主界面的代码

package com.example.floatbutton;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	Button btnStart;
	Button btnStop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnStart = (Button) findViewById(R.id.btnstart);
		btnStop = (Button) findViewById(R.id.btnstop);
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnstart) {
			Intent service = new Intent();
			service.setClass(MainActivity.this, FloatService.class);
			startService(service);
			
		}
		if (v.getId() == R.id.btnstop) {
			Intent serviceStop = new Intent();
			serviceStop.setClass(MainActivity.this, FloatService.class);
			stopService(serviceStop);
			
		}
	}

}

FloatService

package com.example.floatbutton;

import com.example.floatbutton.FloatViewListener.FloatViewAnimListener;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

public class FloatService extends Service {
	private HorizontalAniView expandView;
	private WindowManager wm_expand = null;
	private WindowManager.LayoutParams wmParams_expand = null;

	private Handler mHandler = new Handler();
	@Override
	public void onCreate() {
		super.onCreate();
		expandView = (HorizontalAniView) LayoutInflater.from(this).inflate(
				R.layout.floating, null);
		initWindowManger();
	}

	Runnable run = new Runnable() {
		public void run() {
			expandView.close();
		}
	};

	private void initWindowManger() {
		// 获取WindowManager
		wm_expand = (WindowManager) getApplicationContext().getSystemService(
				"window");
		// 设置LayoutParams(全局变量)相关参数
		wmParams_expand = getWmParams();

		expandView.setFloatViewAnimListener(new FloatViewAnimListener() {

			@Override
			public void onAnimOpenEnd(int[] location) {
				mHandler.postDelayed(run, 2000);
				
			}

		});

		wm_expand.addView(expandView, wmParams_expand);
	}

	private WindowManager.LayoutParams getWmParams() {

		WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
		wmParams.type = 2002;
		wmParams.flags |= 8;
		wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
		// 以屏幕左上角为原点,设置x、y初始值
		wmParams.x = 0;
		wmParams.y = 25; // 25是系统状态栏的高度
		// 设置悬浮窗口长宽数据
		wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.format = 1;
		return wmParams;
	}

	@Override
	public void onDestroy() {
		Log.d("FloatService", "onDestroy");
		try {
			wm_expand.removeView(expandView);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

}

上面的service中的代码添加的布局文件

expandView = (HorizontalAniView) LayoutInflater.from(this).inflate(
				R.layout.floating, null);
布局文件为 floating.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.floatbutton.HorizontalAniView
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" />

HorizontalAniView

package com.example.floatbutton;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.Animator.AnimatorListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class HorizontalAniView extends LinearLayout {
	private ImageView btnEncryption;
	private ImageView btnTransmission;
	private ImageView btnMain;

	private boolean isOpen = true;

	protected final int DURAIION = 300;
	protected View root;

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param attrs
	 */
	public HorizontalAniView(Context context, AttributeSet attrs) {
		super(context, attrs);
		InflaterView(context);
	}

	public HorizontalAniView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		InflaterView(context);
	}

	public HorizontalAniView(Context context) {
		super(context);
		InflaterView(context);
	}

	public View InflaterView(Context context) {
		root = LayoutInflater.from(context).inflate(
				R.layout.horizontal_ani_view, this);
		btnEncryption = (ImageView) root.findViewById(R.id.btnEncryption);
		btnTransmission = (ImageView) root.findViewById(R.id.btnTransmission);
		btnMain = (ImageView) root.findViewById(R.id.btnMain);
		btnMain.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				toggle();
			}
		});
		return root;
	}

	private void toggle() {
		if (isOpen)
			close();
		else
			open();
	}

	/**
	 * 更改开关状态
	 */
	protected FloatViewListener.FloatViewAnimListener animListener;


	public void setFloatViewAnimListener(
			FloatViewListener.FloatViewAnimListener listener) {
		this.animListener = listener;
	}

	public void open() {
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(btnEncryption, "x",
				434.5f, 0f);
		anim2.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
			}

			@Override
			public void onAnimationEnd(Animator animation) {
				if (animListener != null) {
					animListener.onAnimOpenEnd(null);
				}
			}

			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
		anim2.setDuration(DURAIION);

		ObjectAnimator anim4 = ObjectAnimator.ofFloat(btnTransmission, "x",
				434.5f, 225f);
		anim4.setDuration(DURAIION);

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.play(anim2).with(anim4);
		animatorSet.start();

		isOpen = true;
	}

	public void close() {
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(btnEncryption, "x",
				0f,434.5f);
		anim2.setDuration(DURAIION);
		ObjectAnimator anim4 = ObjectAnimator.ofFloat(btnTransmission, "x",
				225f, 434.5f);
		anim4.setDuration(DURAIION);
		AnimatorSet animatorSet = new AnimatorSet();

		animatorSet.play(anim2).with(anim4);
		animatorSet.start();

		isOpen = false;
	}

}

上面代码中

root = LayoutInflater.from(context).inflate(
				R.layout.horizontal_ani_view, this);

加载的布局文件

horizontal_ani_view.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"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/btnEncryption"
        android:layout_width="60.33dp"
        android:layout_height="60.33dp"
        android:src="@drawable/bendi" />

    <ImageView
        android:id="@+id/btnTransmission"
        android:layout_width="60.33dp"
        android:layout_height="60.33dp"
        android:layout_marginLeft="14.67dp"
        android:src="@drawable/chuanshu2" />

    <ImageView
        android:id="@+id/btnMain"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="14.67dp"
        android:src="@drawable/zhuicon" />

</LinearLayout>


另外涉及到的监听事件

FloatViewListener

package com.example.floatbutton;

public class FloatViewListener {


	public interface FloatViewAnimListener {
		public void onAnimOpenEnd(int[] location);

	}
}

用到的权限

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



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

自定义控件(23)---FloatView悬浮窗(2)

点击打开链接,下载demo... 点击打开链接,下载自定义控件(22)---FloatView悬浮窗(1) 点击打开链接,最原始的悬浮窗代码。。。 先看效果图 先看...
  • u013210620
  • u013210620
  • 2015-11-18 17:17
  • 2246

Android手机悬浮窗口小例子

–主页面——–//布局中就一个Button public class MainActivity extends Activity { @Override protected void o...
  • true100
  • true100
  • 2016-08-17 15:43
  • 3362

自定义控件(23)---FloatView悬浮窗(2)

点击打开链接,下载demo... 点击打开链接,下载自定义控件(22)---FloatView悬浮窗(1) 点击打开链接,最原始的悬浮窗代码。。。 先看效果图 先看...
  • u013210620
  • u013210620
  • 2015-11-18 17:17
  • 2246

android 应用内悬浮框,并在指定页面显示

一、实现悬浮悬浮框基本的实现方式有两种: 1、 在一个页面内,可以用FrameLayout 或者RelativeLayout。FrameLayout 中view是在左上角堆叠的,也就是说是z-or...
  • y1962475006
  • y1962475006
  • 2017-06-06 22:49
  • 1461

自定义控件(23)---FloatView悬浮窗(2)

点击打开链接,下载demo... 点击打开链接,下载自定义控件(22)---FloatView悬浮窗(1) 点击打开链接,最原始的悬浮窗代码。。。 先看效果图 先看...
  • u013210620
  • u013210620
  • 2015-11-18 17:17
  • 2246

自定义的悬浮窗,可托拉拽

  • 2014-11-24 10:52
  • 1.28MB
  • 下载

自定义悬浮窗显示照相机

  • 2016-07-24 20:20
  • 2.54MB
  • 下载

Android开发笔记(一百一十八)自定义悬浮窗

WindowManager 在前面《Android开发笔记(六十六)自定义对话框》中,我们提到每个页面都是一个Window窗口,许多的Window对象需要一个管家来打理,这个管家我们称之为Window...
  • aqi00
  • aqi00
  • 2016-08-10 16:55
  • 3429

自定义水波球清理内存的悬浮窗小工具

一、概述 现在一些手机管家都会有一个用来清理内存的悬浮窗小工具,感觉挺实用的,就自己做了一个。首先介绍一下这个工具的功能,除了可以清理内存,还有调节手机屏幕亮度、手电筒、无线网、移动数据、蓝牙、GPS...
  • hello_1995
  • hello_1995
  • 2016-10-31 17:34
  • 2819

共计22款(导航栏、图片转轮、时间控件、小图标素材、悬浮窗、客服)HTML,JQ,CSS,JS源码

  • 2017-04-07 09:01
  • 8.79MB
  • 下载