上文android 仿iphoneX上滑退出应用动画(一)已经说了思路和解决思想,这里对代码进行实现。
先明确,锁屏界面,通知栏和工具栏下滑界面是不需要滑动条的,launcher界面需要一个透明的滑动条,其他界面根据背景色确定需要白色还是黑色的滑动条。
监听到广播top_activity_changes,说明activity有变化
// add by csc for slide
if(topAppPackageName!=null){
if(!isInLockScreen())
addOrRemoveSlideView(true);
if(topAppPackageName.equals("com.mediatek.oobe")&&!topAppClassName.equals("com.mediatek.oobe.basic.OobeLockScreenActivity")){
addOrRemoveSlideView(false);
}
if(!topAppClassName.equals("com.android.systemui.recents.RecentsActivity"))
isOpenRecents = false;
}
// add by csc for slide
setPanelExpanded方法判断左右下滑的通知栏和工具栏是否下滑,还可以判断锁屏界面的出现和消失,所以这里也需要addOrRemoveSlideView来remove滑动条
public void setPanelExpanded(boolean isExpanded) {
if(DEBUG)Log.i("notification_panel","PhoneStatusBar setPanelExpanded() isExpanded="+isExpanded);
mStatusBarWindowManager.setPanelExpanded(isExpanded);
if (isExpanded && getBarState() != StatusBarState.KEYGUARD) {
if (DEBUG) {
Log.v(TAG, "clearing notification effects from setPanelExpanded");
}
clearNotificationEffects();
}
if (!isExpanded) {
removeRemoteInputEntriesKeptUntilCollapsed();
mNotificationsDislpayLayout.resetView();//added by xss for ios11
if(getBarState() == StatusBarState.KEYGUARD)unLockScreen();//added by xss for ios11 face unlock
}
//showOrHideNavigationBar(!isExpanded);//modified by xss for ios11 for NavigationBarView
addOrRemoveSlideView(!isExpanded);// add by csc for slide
}
addOrRemoveSlideView方法的作用是add滑动条或者remove滑动条,其中用isAddSlideView判断是否已经存在滑动条,如果已经存在则不在添加
private void addOrRemoveSlideView(boolean isAdd){
WindowManager mWindowManager1 = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display mDisplay = mWindowManager1.getDefaultDisplay();
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
mDisplay.getRealMetrics(mDisplayMetrics);
Matrix mDisplayMatrix = new Matrix();
float[] dims = { mDisplayMetrics.widthPixels,mDisplayMetrics.heightPixels};
x = (int)mDisplayMetrics.widthPixels;
y = (int)mDisplayMetrics.heightPixels;
if(isAdd){
if(topAppPackageName!=null) {
if (!isAddSlideView) {
final int flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
/*ViewGroup.LayoutParams.MATCH_PARENT*/250,
/*ViewGroup.LayoutParams.MATCH_PARENT*/20,
WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
flags,
PixelFormat.TRANSLUCENT);
lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
mSlideView = new ImageView(mContext);
mSlideView.setOnTouchListener(setOnTouchSlideListener);
if(topAppPackageName.equals("com.hskj.hometest")){
mSlideView.setBackgroundResource(0);
}else if(topAppPackageName.equals("com.android.mms")) {
mSlideView.setBackgroundResource(R.drawable.slide_black);
}else{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Bitmap mBitmap = takeScreenshot();
int pixel = mBitmap.getPixel(mBitmap.getWidth()/2, mBitmap.getHeight()-1);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int grayLevel = (int) (redValue * 0.299 + blueValue * 0.587 + greenValue * 0.114);
if (grayLevel >= 220) {
mSlideView.setBackgroundResource(R.drawable.slide_black);
} else {
mSlideView.setBackgroundResource(R.drawable.slide_white);
}
}
},500);
}
mWindowManager.addView(mSlideView, lp);
isAddSlideView = true;
}else{
if(topAppPackageName.equals("com.hskj.hometest")){
mSlideView.setBackgroundResource(0);
}else if(topAppPackageName.equals("com.android.mms")) {
mSlideView.setBackgroundResource(R.drawable.slide_black);
}else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Bitmap mBitmap = takeScreenshot();
Log.d("chenshichun"," "+this.getClass().getCanonicalName()+" ::::::mBitmap.getWidth()/2::: "+mBitmap.getWidth()/2+" mBitmap.getHeight()-1 "+(mBitmap.getHeight()-1));
int pixel = mBitmap.getPixel(mBitmap.getWidth()/2, mBitmap.getHeight()-1);
Log.d("chenshichun"," "+this.getClass().getCanonicalName()+" ::::pixel:::::"+pixel);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
Log.d("chenshichun"," "+this.getClass().getCanonicalName()+" ::::redValue:::::"+redValue);
int grayLevel = (int) (redValue * 0.299 + blueValue * 0.587 + greenValue * 0.114);
if (grayLevel >= 220) {
mSlideView.setBackgroundResource(R.drawable.slide_black);
} else {
mSlideView.setBackgroundResource(R.drawable.slide_white);
}
}
},500);
}
}
}
}else{
if (isAddSlideView) {
mWindowManager.removeView(mSlideView);
isAddSlideView = false;
}
}
}
Bitmap mBitmap = takeScreenshot();
int pixel = mBitmap.getPixel(mBitmap.getWidth()/2, mBitmap.getHeight()-1);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int grayLevel = (int) (redValue * 0.299 + blueValue * 0.587 + greenValue * 0.114);
if (grayLevel >= 220) {
mSlideView.setBackgroundResource(R.drawable.slide_black);
} else {
mSlideView.setBackgroundResource(R.drawable.slide_white);
}
到此,就把滑动条加上去了。