Skip to content

Commit

Permalink
优化float view
Browse files Browse the repository at this point in the history
  • Loading branch information
pengjianbo committed Nov 18, 2015
1 parent b6ccab6 commit dfc6892
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 56 deletions.
Binary file modified FloatViewFinal-Sample.apk
Binary file not shown.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# FloatViewFinal简介
泡椒网游戏SDK Float View(悬浮窗)实现
(兼容MIUI系统)

* 无需任何权限
* 兼容MIUI系统

关于Android无需权限显示悬浮窗,请【[点击](http://www.jianshu.com/p/167fd5f47d5c)
关于悬浮窗更详细资料,请【[点击](http://www.jianshu.com/p/167fd5f47d5c)

DEMO apk文件【[下载](https://raw.githubusercontent.com/pengjianbo/FloatViewFinal/master/FloatViewFinal-Sample.apk)

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.finalteam.floatviewfinal.sample" >

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import cn.finalteam.floatviewfinal.widget.FloatView;
import java.lang.ref.WeakReference;

/**
* Desction:Float view service
Expand All @@ -32,24 +30,17 @@
public class FloatViewService extends Service{

private FloatView mFloatView;
private IBinder mFloatViewServiceBinder;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return mFloatViewServiceBinder;
return new FloatViewServiceBinder();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mFloatView != null) {
return START_STICKY;
}

@Override
public void onCreate() {
super.onCreate();
mFloatView = new FloatView(this);
mFloatViewServiceBinder = new FloatViewServiceBinder(this);

return START_REDELIVER_INTENT;
}

public void showFloat() {
Expand All @@ -64,24 +55,22 @@ public void hideFloat() {
}
}

@Override
public void onDestroy() {
super.onDestroy();
public void destroyFloat() {
if ( mFloatView != null ) {
mFloatView.destroy();
}
mFloatView = null;
}

public class FloatViewServiceBinder extends Binder {

private final WeakReference<FloatViewService> mService;

FloatViewServiceBinder(FloatViewService service) {
mService = new WeakReference<>(service);
}
@Override
public void onDestroy() {
super.onDestroy();
destroyFloat();
}

public class FloatViewServiceBinder extends Binder {
public FloatViewService getService() {
return mService.get();
return FloatViewService.this;
}
}
}
57 changes: 29 additions & 28 deletions app/src/main/java/cn/finalteam/floatviewfinal/widget/FloatView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class FloatView extends FrameLayout implements OnTouchListener {
private WindowManager mWindowManager;
private Context mContext;

private View mRootFloatView;
//private View mRootFloatView;
private ImageView mIvFloatLogo;
private ImageView mIvFloatLoader;
private LinearLayout mLlFloatMenu;
Expand Down Expand Up @@ -142,7 +142,6 @@ private void init(Context mContext) {
mWindowManager.addView(this, mWmParams);

mTimer = new Timer();

hide();
}

Expand Down Expand Up @@ -189,16 +188,16 @@ protected void onConfigurationChanged(Configuration newConfig) {
private View createView(final Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
// 从布局文件获取浮动窗口视图
mRootFloatView = inflater.inflate(ResourceUtils.getLayoutId(context, "pj_widget_float_view"), null);
mFlFloatLogo = (FrameLayout) mRootFloatView.findViewById(ResourceUtils.getId(context, "pj_float_view"));
View rootFloatView = inflater.inflate(ResourceUtils.getLayoutId(context, "pj_widget_float_view"), null);
mFlFloatLogo = (FrameLayout) rootFloatView.findViewById(ResourceUtils.getId(context, "pj_float_view"));

mIvFloatLogo = (ImageView) mRootFloatView.findViewById(ResourceUtils.getId(context,
mIvFloatLogo = (ImageView) rootFloatView.findViewById(ResourceUtils.getId(context,
"pj_float_view_icon_imageView"));
mIvFloatLoader = (ImageView) mRootFloatView.findViewById(ResourceUtils.getId(
mIvFloatLoader = (ImageView) rootFloatView.findViewById(ResourceUtils.getId(
context, "pj_float_view_icon_notify"));
mLlFloatMenu = (LinearLayout) mRootFloatView.findViewById(ResourceUtils.getId(
mLlFloatMenu = (LinearLayout) rootFloatView.findViewById(ResourceUtils.getId(
context, "ll_menu"));
mTvAccount = (TextView) mRootFloatView.findViewById(ResourceUtils.getId(
mTvAccount = (TextView) rootFloatView.findViewById(ResourceUtils.getId(
context, "tv_account"));
mTvAccount.setOnClickListener(new OnClickListener() {
@Override
Expand All @@ -207,7 +206,7 @@ public void onClick(View arg0) {
openUserCenter();
}
});
mTvFeedback = (TextView) mRootFloatView.findViewById(ResourceUtils.getId(
mTvFeedback = (TextView) rootFloatView.findViewById(ResourceUtils.getId(
context, "tv_feedback"));
mTvFeedback.setOnClickListener(new OnClickListener() {
@Override
Expand All @@ -216,8 +215,8 @@ public void onClick(View arg0) {
mLlFloatMenu.setVisibility(View.GONE);
}
});
mRootFloatView.setOnTouchListener(this);
mRootFloatView.setOnClickListener(new OnClickListener() {
rootFloatView.setOnTouchListener(this);
rootFloatView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if ( !mDraging ) {
Expand All @@ -229,12 +228,12 @@ public void onClick(View v) {
}
}
});
mRootFloatView.measure(View.MeasureSpec.makeMeasureSpec(0,
rootFloatView.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


return mRootFloatView;
return rootFloatView;
}

@Override
Expand All @@ -251,7 +250,7 @@ public boolean onTouch(View v, MotionEvent event) {
mIvFloatLogo.setImageResource(ResourceUtils.getDrawableId(
mContext, "pj_image_float_logo"));
mWmParams.alpha = 1f;
mWindowManager.updateViewLayout(FloatView.this, mWmParams);
mWindowManager.updateViewLayout(this, mWmParams);
mDraging = false;
break;
case MotionEvent.ACTION_MOVE:
Expand Down Expand Up @@ -301,35 +300,37 @@ private void removeTimerTask() {

private void removeFloatView() {
try {
if (mRootFloatView != null) {
mWindowManager.removeView(mRootFloatView);
}
mWindowManager.removeView(this);
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* 隐藏悬浮窗
*/
public void hide() {
if (mRootFloatView != null) {
mRootFloatView.setVisibility(View.GONE);
removeTimerTask();
}
setVisibility(View.GONE);
Message message = mTimerHandler.obtainMessage();
message.what = HANDLER_TYPE_HIDE_LOGO;
mTimerHandler.sendMessage(message);
removeTimerTask();
}

/**
* 显示悬浮窗
*/
public void show() {
if (mRootFloatView != null) {
mIvFloatLogo.setImageResource(ResourceUtils.getDrawableId(
mContext, "pj_image_float_logo"));
mWmParams.alpha = 1f;
mWindowManager.updateViewLayout(FloatView.this, mWmParams);
mRootFloatView.setVisibility(View.VISIBLE);
timerForHide();
if (getVisibility() != View.VISIBLE) {
setVisibility(View.VISIBLE);
if (mShowLoader) {
mIvFloatLogo.setImageResource(ResourceUtils.getDrawableId(
mContext, "pj_image_float_logo"));
mWmParams.alpha = 1f;
mWindowManager.updateViewLayout(this, mWmParams);

timerForHide();

mShowLoader = false;
Animation rotaAnimation = AnimationUtils.loadAnimation(mContext,
ResourceUtils.getAnimId(mContext, "pj_loading_anim"));
Expand Down

0 comments on commit dfc6892

Please sign in to comment.