项目需要,制作类似360,UC类似游戏内的悬浮球
1.开始使用的是系统的悬浮,添加系统权限android.permission.SYSTEM_ALERT_WINDOW,获取windowManager , 设置布局参数,添加view来实现,
优点:处于系统最上层,一次添加,多个地方使用,简单快捷,应用退出到后台还可以显示在界面上
缺点:不能保证所有手机都系统悬浮球弹出
之前用过采用版本API>18windowManagerParams.type = LayoutParams.TYPE_TOAST(系统不检查权限)反之采用windowManagerParams.type = LayoutParams.TYPE_PHONE
还是没有很好的保证系统悬浮球弹出,只能说android手机厂商太多,各家都有自己系统定制难兼容!
2.自定义应用内,悬浮球
思路:
为了完全弹窗悬浮球,只能定义悬浮球来实现了,之后每个窗体添加下,(写个基类包含添加该悬浮球功能,其他子类继承添加就好)
优点:保证所有机器都能弹出悬浮球缺点:在需要添加的每个窗体,中添加,删除,对改悬浮球,应用退到后台后不可显示在前端
下面是自定义悬浮球控件,包括区域检测点击,onDraw有点乱,没整理抱歉,看官将就下哈!
package com.krislq.floating;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import com.fengcao.okwan.R;
public class CircleFloatView extends View{
public static String TAG = CircleFloatView.class.getCanonicalName();
private float x;
private float y;
private float mStartX;
private float mStartY;
private Context context;
Bitmap bp_logo,bp_gift,bp_account;
boolean logoIsClose=true;//关闭,打开,礼包账号
Paint pic_Paint,txt_Paint;
Rect rt_logo,rt_gift,rt_account;
int leftpadd=20;
int topadd=10;
private float currentx;
private float currenty;
int screenwidth,screenheigh;
boolean isFirstshow=true;
public CircleFloatView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public CircleFloatView(Context context) {
super(context);
this.context = context;
//获取屏幕宽高
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
screenwidth = manager.getDefaultDisplay().getWidth();
screenheigh = manager.getDefaultDisplay().getHeight();
pic_Paint=new Paint();
pic_Paint.setAntiAlias(true);
txt_Paint=new Paint();
txt_Paint.setTextSize(26);
txt_Paint.setAntiAlias(true);
txt_Paint.setColor(Color.BLACK);
bp_logo=BitmapFactory.decodeResource(getResources(),R.drawable.float_icon);
bp_gift=BitmapFactory.decodeResource(getResources(),R.drawable.gift_icon);
bp_account=BitmapFactory.decodeResource(getResources(),R.drawable.account_icon);
rt_logo=new Rect();
rt_gift=new Rect();
rt_account=new Rect();
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(logoIsClose)//
{
//画logo
float left=currentx;
float top=currenty;
if(left<=0)
{
left=0;
}
if(left>=screenwidth)
{
left=screenwidth;
}
if(top<=0)
{
top=0;
}
if(top>screenheigh)
{
top=screenheigh;
}
if(!isFirstshow)//非第一次进入
{
left=left-bp_logo.getWidth()/2;
top=top-bp_logo.getHeight()/2;
}
isFirstshow=false;
canvas.drawBitmap(bp_logo,left,top, pic_Paint);
rt_logo.left=(int)left;
rt_logo.top=(int)top;
rt_logo.right=(int)left+bp_logo.getWidth();
rt_logo.bottom=(int)top+bp_logo.getHeight();
//礼包区域
rt_gift.left=0;
rt_gift.top=0;
rt_gift.right=0;
rt_gift.bottom=0;
//账号区域
rt_account.left=0;
rt_account.top=0;
rt_account.right=0;
rt_account.bottom=0;
}
else
{
isFirstshow=false;
float left=currentx;
float top=currenty;
if(left<=0)
{
left=0;
}
if(left>=screenwidth)
{
left=screenwidth;
}
if(top<=0)
{
top=0;
}
if(top>screenheigh)
{
top=screenheigh;
}
left=left-bp_logo.getWidth()/2;
top=top-bp_logo.getHeight()/2;
//画logo
canvas.drawBitmap(bp_logo,left,top, pic_Paint);
//画礼包图片
canvas.drawBitmap(bp_gift,left+bp_logo.getWidth()+leftpadd,top, pic_Paint);
//画礼包文字
canvas.drawText("礼包",left+bp_logo.getWidth()+leftpadd+bp_logo.getWidth()/2,top+bp_gift.getHeight()+leftpadd,txt_Paint);
//画账号图片
canvas.drawBitmap(bp_account,left+bp_logo.getWidth()+20+bp_gift.getWidth()+leftpadd,top, pic_Paint);
//画账号文字
canvas.drawText("账号",left+bp_logo.getWidth()+leftpadd+bp_gift.getWidth()+leftpadd+leftpadd,top+bp_account.getHeight()+leftpadd,txt_Paint);
//logo区域
rt_logo.left=(int)left;
rt_logo.top=(int)top;
rt_logo.right=(int)left+bp_logo.getWidth();
rt_logo.bottom=(int)top+bp_logo.getHeight();
//礼包区域
rt_gift.left=rt_logo.right+leftpadd;
rt_gift.top=(int)top;
rt_gift.right=rt_gift.left+bp_gift.getWidth();
rt_gift.bottom=(int)top+bp_gift.getHeight();
//账号区域
rt_account.left=rt_gift.right+leftpadd;
rt_account.top=(int)top;
rt_account.right=rt_account.left+bp_account.getWidth();
rt_account.bottom=(int)top+bp_account.getHeight();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getRawX();
y = event.getRawY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
mStartX = x;
mStartY = y;
int clickposition1=clickStrict((int)x,(int)y);
Log.e("down","click.down.clickposition="+clickposition1);
if(clickposition1==0)//其他区域
{
return false;
}
break;
case MotionEvent.ACTION_MOVE:
x= event.getX();
y = event.getY();
currentx=x;
currenty=y;
//刷新
this.postInvalidate();
break;
case MotionEvent.ACTION_UP:
x= event.getX();
y = event.getY();
if ((x - mStartX) < 10&& (y - mStartY) < 10) //点击
{
int clickposition=clickStrict((int)x,(int)y);
if(clickposition==0)//其他区域
{
return false;
}
if(logoIsClose)
{
logoIsClose=false;
this.postInvalidate();
}
else
{
Log.e("clickstrict",clickposition+"___");
if(clickposition==1)
{
Toast.makeText(context,"点击logo",Toast.LENGTH_LONG).show();
logoIsClose=true;
this.postInvalidate();
}
if(clickposition==2)
{
Toast.makeText(context,"点击礼包",Toast.LENGTH_LONG).show();
}
if(clickposition==3)
{
Toast.makeText(context,"点击账号",Toast.LENGTH_LONG).show();
}
}
return false;
}
else
{
return false;
}
//break;
default:
}
return true;
}
/**
* 1,点击logo,2,礼包,3账号
* @param x
* @param y
* @return
*/
public int clickStrict(int x,int y)
{
Log.e("clickposition","x="+x+"___y="+y);
int clickposition=0;
if(x>=rt_logo.left&&y>=rt_logo.top&&x<=rt_logo.right&&y<=rt_logo.bottom)
{
clickposition=1;
}
if(x>=rt_gift.left&&y>=rt_gift.top&&x<=rt_gift.right&&y<=rt_gift.bottom)
{
clickposition=2;
}
if(x>=rt_account.left&&y>=rt_account.top&&x<=rt_account.right&&y<=rt_account.bottom)
{
clickposition=3;
}
return clickposition;
}
}