仿360浮动金币实现
最近也在做下载平台,仿91做了个挖宝功能,现在就来说下浮动金币效果
先实现GoldApplication继承Application
View Code
public class GoldApplication extends Application { private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams(); public WindowManager.LayoutParams getWmParams(){ return wmParams; } }
再实现金币类,可以拖动,自动靠边。如果程序结束或Home键回到桌面,就调用dismiss,不然的话金币会一直在桌面上哦。
View Code
public class GoldIcon extends LinearLayout { private float mTouchRawX; private float mTouchRawY; private GestureDetector gestureDetector; private ImageButton gold; private WindowManager wm = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); //此wmParams变量为获取的全局变量,用以保存悬浮窗口的属性 private WindowManager.LayoutParams wmParams = ((GoldApplication)getContext().getApplicationContext()).getWmParams(); private int width=0,height=0;//控件宽高 private int DW=0,DH=0,MW=0;//屏幕宽高,屏幕宽的一半 public GoldIcon(Context context) { super(context); init(context); DW=BitmapTools.WH[0]; DH=BitmapTools.WH[1]; MW=DW/2; } private void init(Context context) { gestureDetector = new GestureDetector(context, gestureListener); View view = View.inflate(context,R.layout.gold_floatlayout,null); gold=(ImageButton) view.findViewById(R.id.gold); gold.setOnClickListener(onClick); gold.setOnTouchListener(frameOnTouchListener); this.addView(view); } final static int MESSAGE_MOVE = 0x1001; final static int MESSAGE_DOWN = 0x1002; final static int MESSAGE_UP = 0x1003; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_MOVE: updateViewPosition(); break; case MESSAGE_DOWN: break; case MESSAGE_UP: updateUpViewPosition(); break; default: } } }; // 点击Listener OnTouchListener frameOnTouchListener = new OnTouchListener() { float x,y; @Override public boolean onTouch(View v, MotionEvent event) { if (null == gestureDetector || null == event) { return false; } switch(event.getAction()) { case MotionEvent.ACTION_DOWN: x=event.getRawX(); y=event.getRawY(); break; case MotionEvent.ACTION_UP://松手时候靠边 if(Math.abs(x-event.getRawX())>10&&Math.abs(y-event.getRawY())>10) { new Thread(new Run()).start(); return true; } else { return false; } } return gestureDetector.onTouchEvent(event); } }; // 手势识别 GestureDetector.OnGestureListener gestureListener = new GestureDetector.OnGestureListener() { @Override public boolean onDown(MotionEvent e) { mTouchRawX = e.getX(); mTouchRawY = e.getY(); // mHandler.sendEmptyMessage(MESSAGE_MOVE); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return true; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub // Log.d("OnGestureListener", "onLongPress"); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { mTouchRawX = e2.getRawX(); mTouchRawY = e2.getRawY(); mHandler.sendEmptyMessage(MESSAGE_MOVE); return true; } @Override public void onShowPress(MotionEvent e) { // Log.d("OnGestureListener", "onShowPress"); } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }; public void show() { gold.setVisibility(View.VISIBLE); } public void dismiss() { gold.setVisibility(View.GONE); } private void updateUpViewPosition() { wm.updateViewLayout(this, wmParams); } private void updateViewPosition() { // 更新浮动窗口位置参数 getWH(); wmParams.x = (int) mTouchRawX-(width); wmParams.y = (int) mTouchRawY-(height); wm.updateViewLayout(this, wmParams); } class Run implements Runnable { @Override public void run() { while(wmParams.x>=0&&wmParams.x+width<=DW) { setWelt(); try { Thread.sleep(3); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } mHandler.sendEmptyMessage(MESSAGE_UP); } } private void setWelt() { if(wmParams.x+width/2<MW)//左靠边 { wmParams.x=wmParams.x-2; } else//右靠边 { wmParams.x=wmParams.x+2; } mHandler.sendEmptyMessage(MESSAGE_UP); } private void getWH() { if(width<=0||height<=0) { width=this.getWidth(); height=this.getHeight(); } } // 响应悬浮窗口中的Button点击 View.OnClickListener onClick = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.gold: // MainActivity.main.goToBigger(); break; } } }; }
在要程序启动的首个Activity中
View Code
private WindowManager wm = null; private WindowManager.LayoutParams wmParams = null; private GoldIcon gold;//仿91浮动金币 initGold();
View Code
private void initGold() { BitmapTools.WH=BitmapTools.getWindowWidthAddHeight(this); gold=new GoldIcon(getApplicationContext()); // 获取WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // 设置LayoutParams(全局变量)相关参数 wmParams = ((GoldApplication) getApplication()).getWmParams(); /** * 以下都是WindowManager.LayoutParams的相关属性 具体用途可参考SDK文档 */ wmParams.type = LayoutParams.TYPE_PHONE;//LayoutParams.TYPE_PHONE; // 设置window type wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明 // 设置Window flag wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; /* * 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。 * wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | * LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE; */ wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角 // 设置悬浮窗口长宽数据 wmParams.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; wmParams.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; // 显示myFloatView图像 wm.addView(gold, wmParams); }
View Code
public void goldShow() { gold.show(); } public void goldDismiss() { gold.dismiss(); } public void goToBigger() { radio_Digger.setChecked(true); }
View Code
android:name=".GoldApplication" <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait" android:name=".GoldApplication">