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

点击打开链接,现在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;
	}


自定义控件(22)---FloatView悬浮窗(1)_第1张图片自定义控件(22)---FloatView悬浮窗(1)_第2张图片

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" />



你可能感兴趣的:(自定义控件(22)---FloatView悬浮窗(1))