【vip源码网站】【ocal 源码分析】【鸿蒙核心源码】androidanimation源码
1.å¦ä½å¨XMLä¸ä½¿ç¨èªå®ä¹Animationå¨ç»ç±»
2.Android常è§ç¥è¯ç¹
3.androidå¼å设置å±è½å½å¶
å¦ä½å¨XMLä¸ä½¿ç¨èªå®ä¹Animationå¨ç»ç±»
å¨å®ååºç¨çå¨ç»å¼åä¸ï¼å¯è½SDKä¸èªå¸¦çè¡¥é´å¨ç»ä¸è½æ»¡è¶³åºç¨çéæ±ï¼éè¦å¨Java代ç ä¸èªå®ä¹ä¸äºå¨ç»ç±»ï¼å½ç¶é½æ¯ç»§æ¿èªAnimationç±»ãå®ç°ä¹åï¼æ们ä¸è¬ç´æ¥å¨ä»£ç ä¸ä½¿ç¨ï¼ç±»ä¼¼ä¸é¢è¿æ ·ï¼CustomAnimationcustomAnimation=newCustomAnimation();customAnimation.setDuration();customAnimation.setFillAfter(true);effectView.startAnimation(customAnimation);å½Viewåæ¶è¦åºç¨åScaleï¼Alphaè¿æ ·çè¡¥é´å¨ç»æ¶ï¼ä½ å°±éè¦å¤æ·»å 类似ä¸é¢ç代ç ï¼CustomAnimationcustomAnimation=newCustomAnimation();customAnimation.setDuration();customAnimation.setFillAfter(true);AnimationscaleAnimation=newScaleAnimation(0f,源码vip源码网站1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);AnimationalphaAnimation=newAlphaAnimation(0.1f,1.0f);scaleAnimation.setDuration();alphaAnimation.setDuration();AnimationSetset=newAnimationSet(true);set.addAnimation(customAnimation);set.addAnimation(scaleAnimation);set.addAnimation(alphaAnimation);set.setFillAfter(true);set.setFillEnabled(true);effectView.startAnimation(set);å¦æç´æ¥å¨xmlä¸ææéçææè¡¥é´å¨ç»ï¼å æ¬èªå®ä¹å¨ç»ç±»æ¾å°ä¸ä¸ªéåï¼äºæ çèµ·æ¥å°±æ²¡é£ä¹å¤æãå¨xmlå®ä¹å¥½å¨ç»éæ两个好å¤ï¼ä½¿ç¨å¨ç»æ¶éè¦æ´å°çJava代ç ï¼æ´ä½ä¸çä¸å»æ´å¹²åå¨xmlä¸å®ä¹ï¼å个å¨ç»å±æ§ä¸ç®äºç¶ä¹æ´éä¸ï¼æ¹ä¾¿é 读ä¸ç»´æ¤æ¢ç¶æè¿æ ·ç好å¤ï¼æ们就å¼å§å¹²å§ãé¦å å¨xmlä¸åä¸é¢è¿æ ·å®ä¹ä¸ä¸ªå¨ç»éï¼R.anim.my_anim_setmyapp:customProp2=""myapp:customProp3="%"android:duration=""android:fillAfter="true"/>ç¶åï¼æ们æç §å¸¸çæ¥ï¼å¨Java代ç ä¸è¿æ ·æ¥å è½½æ们å®ä¹çxmlå¨ç»éï¼AnimationSetset=(AnimationSet)AnimationUtils.loadAnimation(this,R.anim.my_anim_set);effectView.startAnimation(set);ä½æ¯ï¼æ±æï¼ä¸é¢ç代ç æ¯ä¸æ£ç¡®æ§è¡ï¼è¿è¡èµ·æ¥ç¨åºä¼ç´æ¥ç»æ¢ãé£ä»ä¹åå å¢ï¼æ¥çAnimationUtils.loadAnimationæºä»£ç æ们ç¥éï¼å¨å ¶ä»xmlè½½å ¥å¨ç»ç±»çæ¶åï¼åªè®¤alphaãscaleãrotateãtranslateè¿å 个SDKèªå¸¦çå¨ç»ç±»ï¼èæ们åå ¥çèªå®ä¹å¨ç»ç±»CustomAnimationä¼å¯¼è´å ¶æ¥Unknownanimationnameçå¼å¸¸ãå®æ¹SDKä¹æ²¡ææä¾è§£å³è¿ä¸ªé®é¢çå ¶ä»APIæ¹æ³ï¼é£ä¹æä¹è§£å³å¢ï¼å¾ç®åï¼åªéå¨åæçAnimationUtils.loadAnimationæºç ä¸æ¹å¨ä¸è¡ï¼ä»ClassLoaderè½½å ¥èªå®ä¹å¨ç»ç±»å³å¯ãå°å ¶æºç æ·è´è¿æ¥ï¼å®ç°ä¸ä¸ªèªå·±çloadAnimationæ¹æ³ï¼å¦ä¸ï¼OptAnimationLoader.javapublicclassOptAnimationLoader{ publicstaticAnimationloadAnimation(Contextcontext,intid)throwsResources.NotFoundException{ XmlResourceParserparser=null;try{ parser=context.getResources().getAnimation(id);returncreateAnimationFromXml(context,parser);}catch(XmlPullParserExceptionex){ Resources.NotFoundExceptionrnf=newResources.NotFoundException("Can'tloadanimationresourceID#0x"+Integer.toHexString(id));rnf.initCause(ex);throwrnf;}catch(IOExceptionex){ Resources.NotFoundExceptionrnf=newResources.NotFoundException("Can'tloadanimationresourceID#0x"+Integer.toHexString(id));rnf.initCause(ex);throwrnf;}finally{ if(parser!=null)parser.close();}}privatestaticAnimationcreateAnimationFromXml(Contextc,XmlPullParserparser)throwsXmlPullParserException,IOException{ returncreateAnimationFromXml(c,parser,null,Xml.asAttributeSet(parser));}privatestaticAnimationcreateAnimationFromXml(Contextc,XmlPullParserparser,AnimationSetparent,AttributeSetattrs)throwsXmlPullParserException,IOException{ Animationanim=null;//Makesureweareonastarttag.inttype;intdepth=parser.getDepth();while(((type=parser.next())!=XmlPullParser.END_TAG||parser.getDepth()>depth)&&type!=XmlPullParser.END_DOCUMENT){ if(type!=XmlPullParser.START_TAG){ continue;}Stringname=parser.getName();if(name.equals("set")){ anim=newAnimationSet(c,attrs);createAnimationFromXml(c,parser,(AnimationSet)anim,attrs);}elseif(name.equals("alpha")){ anim=newAlphaAnimation(c,attrs);}elseif(name.equals("scale")){ anim=newScaleAnimation(c,attrs);}elseif(name.equals("rotate")){ anim=newRotateAnimation(c,attrs);}elseif(name.equals("translate")){ anim=newTranslateAnimation(c,attrs);}else{ try{ anim=(Animation)Class.forName(name).getConstructor(Context.class,AttributeSet.class).newInstance(c,attrs);}catch(Exceptionte){ thrownewRuntimeException("Unknownanimationname:"+parser.getName()+"error:"+te.getMessage());}}if(parent!=null){ parent.addAnimation(anim);}}returnanim;}}è¿æ ·ï¼ä½¿ç¨OptAnimationLoader.loadAnimationæ¹æ³å°±å¯ä»¥ä»xmlä¸è½½å ¥å å«èªå®ä¹å¨ç»çå¨ç»éäºã
Android常è§ç¥è¯ç¹
跳槽æ éå°±æ¯é±å°äºæä¸ç½äºï¼æ 论æä¹æ ·ï¼è®°ä½ï¼ä¸è¦è£¸è¾ï¼
ä¸è¦è£¸è¾ï¼
ä¸è¦è£¸è¾ï¼
为ä»ä¹å¢ï¼
1ã裸è¾å°±æ²¡æé±æ¿äºï¼è¿ä¸å¦éªé©´æ¾é©¬ã
2ã裸è¾ä¹åå¦æä¸ä¸ªæå 没ææ¾å°å·¥ä½ï¼é£ä¹ç¤¾ä¿å°±ä¼æäºï¼é¤éä½ èªå·±æ¾æ¸ é交äºã
3ã裸è¾ä¹åççä¼å¾é¢åºï¼
å½åè¿å¨ä¸ççæ¶åå°±æ³çï¼è£¸è¾äºï¼é¦å è±å 天æ¶é´å§ç®åå®åä¸ä¸ï¼æç¥è¯ç¹æ¶è¡¥ä¸ä¸ï¼ç¶åæç®åï¼é¢è¯ï¼å¦¥å¦¥ç妥妥ã
ç»æå¢ï¼æ¯æ¬¡è£¸è¾ä¹åé½æ¯ï¼
é¦å 躺尸ä¸ä¸ªææï¼
ç¶åç¨äºä¸ä¸ªææææ ¢ååçæ¹å®ç®åï¼
ç¶åæµ·æï¼æ²¡åå¤ï¼ä¿®æ¹ç®åï¼
å次海æï¼é¢è¯ï¼è¢«èå¾ä½æ å®è¤ï¼æç人çï¼
åæ¹ç®åï¼åæµ·æï¼ä¸ä¸å°å¿ä¸äºã
å½ç¶æµ·æä¹æ¯æ个ç®æ èå´çã
å¦æä¸æ¯è£¸è¾ï¼é£ä¹ç°å¨åºè¯¥è¿æ¯å¨å ¬å¸ä¸çï¼å¨å®æå·¥ä½ä¹ä½ï¼å°±ä¼é¼çèªå·±å¤ä¹ ç¥è¯ç¹äºï¼èµ·ç ä¸ä¼å¨å®¶å è½ãå¨å®¶ä¸ä¸çå°±æ¯ç¡è§ãççµå½±ï¼æ¨¯æ©¹ç°é£ççï¼æ以ä¸è¦è£¸è¾ã
ç¶å¹¶åµï¼æä¾ç¶è£¸è¾äºã请åé¢è¯ççå¾ç¦ã
onPauseï¼åå¦ä»ActivityAå¯å¨Bï¼å¦æBæ¯éæçï¼åä¸ä¼åè°AçonStopæ¹æ³ã
æ¹æ³ä¸ï¼
æ¹æ³äºï¼
1ãå好å¨ç»æ件 R.anim.enter ã R.anim.exit
2ãè°ç¨ overridePendingTransition 设置å¨ç»
å¼ç³ï¼å¦Activity设置为singleInstanceï¼ååºè¯¥æä¹è®¾ç½®è·³è½¬å¨ç»ï¼
1ãstartServiceå¯å¨æ¹ä¸Service并没æå ³èï¼åªæå½Serviceè°ç¨ stopSelf æè å ¶å®ç»ä»¶è°ç¨ stopService çæ¶åæå¡æä¼ç»æ¢ã
2ãbindServiceå¯å¨æ¹ç»å®Serviceï¼å¹¶ä¸å¯ä»¥éè¿Binderä¸ä¹äº¤äºï¼å½å¯å¨æ¹éæ¯æ¶ï¼ä¹ä¼èªå¨unbindServiceï¼å½ææå¯å¨æ¹é½unbindServiceä¹åï¼Serviceä¹å°±èªå¨éæ¯äºã
为ä»ä¹å¢ï¼å®æ¹ææ¡£æ¯è¿æ ·åçï¼
大æ¦æææ¯ onReceive() æ§è¡å®æ¯ä¹åï¼å®æå¨çè¿ç¨å°±ä¼åæä½ä¼å 级è¿ç¨ï¼ææ被系ç»ææ»ã
å两ç§æ åµåæä¸ä¸ï¼
ä¸ãæ¶å°å¹¿æçæ¶åï¼åºç¨æ£å¨è¿è¡ï¼
æ¤æ¶å¦æ没æå¨Manifestä¸è®¾ç½®äºç¬ç«è¿ç¨ï¼å onReceive() å°±ç´æ¥å¨ä¸»è¿ç¨ä¸»çº¿ç¨æ§è¡ï¼è¿éå¾ææ¾ä¸è½æ§è¡èæ¶æä½ã
äºãæ¶å°å¹¿æçæ¶åï¼åºç¨æ²¡æå¯å¨ï¼
è¿æ¶åç³»ç»ä¼å¯å¨ä¸ä¸ªè¿ç¨å»æ§è¡ onReceive() ï¼ï¼å¦æManifestä¸æ²¡æ设置è¿ç¨åï¼åè¿ç¨å为å åï¼ï¼(æä¸å¥ï¼ææè¿ç¨é½ä¼å建ä¸ä¸ªApplicationå®ä¾)ï¼å½onReceiveæ§è¡å®æ¯ä¹åï¼æ¤è¿ç¨å°±åæä½ä¼å 级äºï¼éæ¶æå¯è½è¢«ç³»ç»ææ»ï¼å¦æä½ å¨onReceiveéé¢å¯å¨äºçº¿ç¨æ§è¡èæ¶ä»»å¡ï¼é£å¾æå¯è½å线ç¨æ²¡æ§è¡å®æ¯ï¼è¿ç¨å°±è¢«ææ»äºï¼è¿ç¨æ²¡äºï¼çº¿ç¨èªç¶å°±æäºã
é£ä¹ç¡®å®è¦æ§è¡èæ¶æä½å¢ï¼æä¹åï¼
æ¹æ³ä¸ï¼goAsync()
æ¹æ³äºï¼schedule a JobService from the receiver using the JobScheduler
ä¸ç§å®ç°æ¹æ³
1ã继æ¿ç°æçç»ä»¶ï¼å¦TextViewçï¼è¿è¡æå±ã
2ã继æ¿ViewGroupï¼èªå®ä¹å¸å±ã
3ã继æ¿Viewï¼å¨onDraw()ä¸æç»ã
onMeasure()
onLayout()
onDraw()
å ¶å®
attachToRoot ä»åé¢ç解就æ¯æ¯å¦ç»å®å° root ä¸é¢å»äºã
1ã attachToRoot=true ï¼åè¿åçview为rootçåviewï¼
2ã attachToRoot=false ï¼åè¿åçviewæ¯ä¸ªåç¬çviewï¼ä¼ å ¥çrootåªæ¯æä¾ä¸äºåæ°ç»view使ç¨èå·²ã
é£ä¹è¿éä¸ä¼ å ¥ attachToRoot å¢ï¼é£å°±çrootæ¯å¦ä¸ºç©ºäºï¼å¦æä¼ å ¥rootä¸ä¸ºç©ºï¼åé»è®¤ç»å®å°rootï¼ä½ä¸ºrootçåviewè¿åã
ä¹å°±æ¯æè°çFrameå¨ç»ãæéè¿æå®æ¯ä¸å¸§çå¾çåææ¾æ¶é´ï¼æåºçè¿è¡ææ¾èå½¢æå¨ç»ææã
å¯ä»¥éè¿æå ¥å¨ Interpolator æ§å¶å¨ç»çååé度ã
ä¹å°±æ¯æè°è¡¥é´å¨ç»ãæéè¿æå®Viewçåå§ç¶æãååæ¶é´ãæ¹å¼ï¼éè¿ä¸ç³»åçç®æ³å»è¿è¡å¾å½¢åæ¢ï¼ä»èå½¢æå¨ç»ææï¼ä¸»è¦æ AlphaAnimation ã TranslateAnimation ã ScaleAnimation ã RotateAnimation åç§ã
注æï¼åªæ¯å¨è§å¾å±å®ç°äºå¨ç»ææï¼å¹¶æ²¡æçæ£æ¹åViewçå±æ§ã
å±æ§å¨ç»ï¼éè¿ä¸æçæ¹åViewçå±æ§ï¼ä¸æçéç»èå½¢æå¨ç»ææãç¸æ¯äºè§å¾å¨ç»ï¼Viewçå±æ§æ¯çæ£æ¹åäºã
注æï¼Android 3.0(API )以ä¸ææ¯æã
æ常ç¨çç±»æ ObjectAnimator
P.S. æä¸æç½cancelåå¨çæä¹ã
å¦å¤ï¼ DialogFragment æ¯æ²¡æcancelçã
ping
å å大è´å为ä¸ä¸ªåºï¼æ åºãå åºãæ¹æ³åºã
æ åº
å åº
æ¹æ³åº
JAVAä¸å 许æå¨éæ¾å åï¼åªè½éè¿åå¾åæ¶ç¨åºä¸å®æ对é£äºä¸å被å¼ç¨ç对象è¿è¡åæ¶ã
é£ä¹æä¹å¤æåªäºå¯¹è±¡éè¦åæ¶ï¼
1ãå¼ç¨è®¡æ°æ³
å°±æ¯ç»å¯¹è±¡æ·»å ä¸ä¸ªå¼ç¨è®¡æ°å¨ï¼å¼ç¨å¯¹è±¡æ¶+1ï¼å¼ç¨å¤±ææ¶-1ãä½æ¯è¿ç§æ¹æ³è§£å³ä¸äºå¯¹è±¡ç¸äºå¼ç¨çæ åµã
2ãå¯è¾¾æ§åææ³
éè¿ä¸ç³»åâGCRootsâ对象ä½ä¸ºèµ·ç¹è¿è¡æç´¢ï¼å½GCRootsåä¸ä¸ªå¯¹è±¡ä¹é´æ²¡æå¯è¾¾è·¯å¾ï¼å认为æ¤å¯¹è±¡ä¸å¯ç¨ï¼ä½æ¯ä¸å¯ç¨ä¸ä¸å®ä¼æ为å¯åæ¶å¯¹è±¡ã
ç¼åAIDLæ件ï¼å®ä¹æ¥å£ã
ç¼è¯çæJAVAæ件ã
å®ä¹è¿ç¨çº§Serviceï¼onBindä¸è¿åInterface.Stub()ã
onServiceConnectedä¸Interface aidl = Interface.Stub.asInterface(service);
æ已修å¤çclassæ件æå ædexæ件ï¼ç½ç»ä¼ è¾å°ç¨æ·ææºä¸ï¼å©ç¨ç±»å è½½å¨æè¿äºç±»å è½½å°ç±»éåçåé¢å³å¯ã
ãæªå®å¾ ç»ã
å¦æå ¬å¸å½ç¨æï¼ä¸ç®¡æ¯ä¸å¹´è¿æ¯äºå¹´ï¼é¦å æé½ä¼å æå ¬å¸çä»»å¡å好ï¼ç¶åä¸ææ·±å ¥ç 究Androidçç¸å ³ææ¯ï¼ç¹å«æ¯Androidæºç ï¼äºè§£Androidåºå±åçï¼ä»¥ä¾¿æ´å¥½çä¼åæ§è½ï¼é¿å ä¸äºä¸å¿ è¦çå¥è©é®é¢ï¼è¿æå°±æ¯ç 究ä¸äºæ°çæ¡æ¶çåçï¼å¦ä¹ å«äººçæç»´ãæåå°±æ¯å¦ä¹ å¨è¾¹è¯è¨ï¼æ¯å¦åå°ï¼å端ççã
androidå¼å设置å±è½å½å¶
项ç®å¼åä¸ï¼ä¸ºäºç¨æ·ä¿¡æ¯çå®å ¨ï¼ä¼æç¦æ¢é¡µé¢è¢«æªå±ãå½å±çéæ±ãè¿ç±»èµæï¼å¨ç½ä¸æå¾å¤ï¼ä¸è¬é½æ¯éè¿è®¾ç½®ActivityçFlag解å³ï¼å¦ï¼
//ç¦æ¢é¡µé¢è¢«æªå±ãå½å±getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
è¿ç§è®¾ç½®å¯è§£å³ä¸è¬çé²æªå±ãå½å±çéæ±ã
å¦æ页é¢ä¸æå¼¹åºPopupwindowï¼å¨å½å±è§é¢ä¸çæææ¯ï¼
éPopupwindowåºå为é»è²
ä½Popupwindowåºåä»ç¶æ¯å¯ä»¥çå°ç
å¦ä¸é¢ä¸¤å¼ Gifå¾æ示ï¼
æªè®¾ç½®FLAG_SECUREï¼å½å±çææï¼å¦ä¸å¾ï¼gitå¾çä¸é´çæ°´å°å¿½ç¥ï¼ï¼
设置äºFLAG_SECUREä¹åï¼å½å±çææï¼å¦ä¸å¾ï¼gitå¾çä¸é´çæ°´å°å¿½ç¥ï¼ï¼
åå åæ
çå°äºä¸é¢çææï¼æ们å¯è½ä¼æçé®PopupWindowä¸åDialogæèªå·±çwindow对象ï¼èæ¯ä½¿ç¨WindowManager.addViewæ¹æ³å°Viewæ¾ç¤ºå¨Activityçªä½ä¸çãé£ä¹ï¼Activityå·²ç»è®¾ç½®äºFLAG_SECUREï¼ä¸ºä»ä¹å½å±æ¶è¿è½çå°PopupWindowï¼
æ们å éè¿getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);æ¥åæä¸æºç ï¼
1ãWindow.java
//windowå¸å±åæ°private final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();//æ·»å æ è¯public void addFlags(int flags) {
setFlags(flags, flags);
}//éè¿mWindowAttributes设置æ è¯public void setFlags(int flags, int mask) { final WindowManager.LayoutParams attrs = getAttributes();
attrs.flags = (attrs.flags&~mask) | (flags&mask);
mForcedWindowFlags |= mask;
dispatchWindowAttributesChanged(attrs);
}//è·å¾å¸å±åæ°å¯¹è±¡ï¼å³mWindowAttributespublic final WindowManager.LayoutParams getAttributes() { return mWindowAttributes;
}
éè¿æºç å¯ä»¥çå°ï¼è®¾ç½®windowå±æ§çæºç é常ç®åï¼å³ï¼éè¿windowéçå¸å±åæ°å¯¹è±¡mWindowAttributes设置æ è¯å³å¯ã
2ãPopupWindow.java
//æ¾ç¤ºPopupWindowpublic void showAtLocation(View parent, int gravity, int x, int y) {
mParentRootView = new WeakReference<>(parent.getRootView());
showAtLocation(parent.getWindowToken(), gravity, x, y);
}//æ¾ç¤ºPopupWindowpublic void showAtLocation(IBinder token, int gravity, int x, int y) { if (isShowing() || mContentView == null) { return;
}
TransitionManager.endTransitions(mDecorView);
detachFromAnchor();
mIsShowing = true;
mIsDropdown = false;
mGravity = gravity;
//å建Windowå¸å±åæ°å¯¹è±¡
final WindowManager.LayoutParams p =createPopupLayoutParams(token);
preparePopup(p);
p.x = x;
p.y = y;
invokePopup(p);
}//å建Windowå¸å±åæ°å¯¹è±¡protected final WindowManager.LayoutParams createPopupLayoutParams(IBinder token) { final WindowManager.LayoutParams p = new WindowManager.LayoutParams();
p.gravity = computeGravity();
p.flags = computeFlags(p.flags);
p.type = mWindowLayoutType;
p.token = token;
p.softInputMode = mSoftInputMode;
p.windowAnimations = computeAnimationResource(); if (mBackground != null) {
p.format = mBackground.getOpacity();
} else {
p.format = PixelFormat.TRANSLUCENT;
} if (mHeightMode < 0) {
p.height = mLastHeight = mHeightMode;
} else {
p.height = mLastHeight = mHeight;
} if (mWidthMode < 0) {
p.width = mLastWidth = mWidthMode;
} else {
p.width = mLastWidth = mWidth;
}
p.privateFlags = PRIVATE_FLAG_WILL_NOT_REPLACE_ON_RELAUNCH
| PRIVATE_FLAG_LAYOUT_CHILD_WINDOW_IN_PARENT_FRAME;
p.setTitle("PopupWindow:" + Integer.toHexString(hashCode())); return p;
}//å°PopupWindowæ·»å å°Windowä¸private void invokePopup(WindowManager.LayoutParams p) { if (mContext != null) {
p.packageName = mContext.getPackageName();
} final PopupDecorView decorView = mDecorView;
decorView.setFitsSystemWindows(mLayoutInsetDecor);
setLayoutDirectionFromAnchor();
mWindowManager.addView(decorView, p); if (mEnterTransition != null) {
decorView.requestEnterTransition(mEnterTransition);
}
}
éè¿PopupWindowçæºç åæï¼æ们ä¸é¾çåºï¼å¨è°ç¨showAtLocationæ¶ï¼ä¼åç¬å建ä¸ä¸ªWindowManager.LayoutParamså¸å±åæ°å¯¹è±¡ï¼ç¨äºæ¾ç¤ºPopupWindowï¼è该å¸å±åæ°å¯¹è±¡ä¸å¹¶æªè®¾ç½®ä»»ä½é²æ¢æªå±Flagã
å¦ä½è§£å³
åå æ¢ç¶æ¾å°äºï¼é£ä¹å¦ä½å¤çå¢ï¼
åå头åæä¸Windowçå ³é®ä»£ç ï¼
//éè¿mWindowAttributes设置æ è¯public void setFlags(int flags, int mask) { final WindowManager.LayoutParams attrs = getAttributes();
attrs.flags = (attrs.flags&~mask) | (flags&mask);
mForcedWindowFlags |= mask;
dispatchWindowAttributesChanged(attrs);
}
å ¶å®åªéè¦è·å¾WindowManager.LayoutParams对象ï¼å设置ä¸flagå³å¯ã
ä½æ¯PopupWindow并没æåActivityä¸æ ·æç´æ¥è·å¾windowçæ¹æ³ï¼æ´å«è¯´è®¾ç½®Flagäºãæ们ååæä¸PopupWindowçæºç ï¼
//å°PopupWindowæ·»å å°Windowä¸private void invokePopup(WindowManager.LayoutParams p) { if (mContext != null) {
p.packageName = mContext.getPackageName();
}
final PopupDecorView decorView = mDecorView;
decorView.setFitsSystemWindows(mLayoutInsetDecor);
setLayoutDirectionFromAnchor(); //æ·»å View
mWindowManager.addView(decorView, p); if (mEnterTransition != null) {
decorView.requestEnterTransition(mEnterTransition);
}
}
æ们è°ç¨showAtLocationï¼æç»é½ä¼æ§è¡mWindowManager.addView(decorView, p);
é£ä¹æ¯å¦å¯ä»¥å¨addViewä¹åè·åå°WindowManager.LayoutParamså¢ï¼
çæ¡å¾ææ¾ï¼é»è®¤æ¯ä¸å¯ä»¥çãå 为PopupWindow并没æå ¬å¼è·åWindowManager.LayoutParamsçæ¹æ³ï¼èä¸mWindowManagerä¹æ¯ç§æçã
å¦ä½æè½è§£å³å¢ï¼
æ们å¯ä»¥éè¿hookçæ¹å¼è§£å³è¿ä¸ªé®é¢ãæ们å 使ç¨å¨æ代çæ¦æªPopupWindowç±»çaddViewæ¹æ³ï¼æ¿å°WindowManager.LayoutParams对象ï¼è®¾ç½®å¯¹åºFlagï¼ååå°è·å¾mWindowManager对象å»æ§è¡addViewæ¹æ³ã
é£é©åæï¼
ä¸è¿ï¼éè¿hookçæ¹å¼ä¹æä¸å®çé£é©ï¼å 为mWindowManageræ¯ç§æ对象ï¼ä¸åPublicçAPIï¼è°·æåç»å级Androidçæ¬ä¸ä¼èèå ¶å ¼å®¹æ§ï¼æ以æå¯è½åç»Androidçæ¬ä¸æ¹äºå ¶å称ï¼é£ä¹æ们éè¿åå°è·å¾mWindowManager对象ä¸å°±æé®é¢äºãä¸è¿ä»å代çæ¬çAndroidæºç å»çï¼mWindowManager被æ¹çå çä¸å¤§ï¼æ以hookä¹æ¯å¯ä»¥ç¨çï¼æ们尽éå代ç æ¶èèä¸è¿ç§é£é©ï¼é¿å 以ååºé®é¢ã
public class PopupWindow {
...... private WindowManager mWindowManager;
......
}
èaddViewæ¹æ³æ¯ViewMangeræ¥å£çå ¬å ±æ¹æ³ï¼æ们å¯ä»¥æ¾å¿ä½¿ç¨ã
public interface ViewManager{ public void addView(View view, ViewGroup.LayoutParams params); public void updateViewLayout(View view, ViewGroup.LayoutParams params); public void removeView(View view);
}
åè½å®ç°
èèå°hookçå¯ç»´æ¤æ§åæ©å±æ§ï¼æ们å°ç¸å ³ä»£ç å°è£ æä¸ä¸ªç¬ç«çå·¥å ·ç±»å§ã
package com.ccc.ddd.testpopupwindow.utils;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.PopupWindow;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class PopNoRecordProxy implements InvocationHandler { private Object mWindowManager;//PopupWindowç±»çmWindowManager对象
public static PopNoRecordProxy instance() { return new PopNoRecordProxy();
} public void noScreenRecord(PopupWindow popupWindow) { if (popupWindow == null) { return;
} try { //éè¿åå°è·å¾PopupWindowç±»çç§æ对象ï¼mWindowManager
Field windowManagerField = PopupWindow.class.getDeclaredField("mWindowManager");
windowManagerField.setAccessible(true);
mWindowManager = windowManagerField.get(popupWindow); if(mWindowManager == null){ return;
} //å建WindowManagerçå¨æ代ç对象proxy
Object proxy = Proxy.newProxyInstance(Handler.class.getClassLoader(), new Class[]{ WindowManager.class}, this); //æ³¨å ¥å¨æ代ç对象proxyï¼å³ï¼mWindowManager对象ç±proxy对象æ¥ä»£çï¼
windowManagerField.set(popupWindow, proxy);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { //æ¦æªæ¹æ³mWindowManager.addView(View view, ViewGroup.LayoutParams params);
if (method != null && method.getName() != null && method.getName().equals("addView")
&& args != null && args.length == 2) { //è·åWindowManager.LayoutParamsï¼å³ï¼ViewGroup.LayoutParams
WindowManager.LayoutParams params = (WindowManager.LayoutParams) args[1]; //ç¦æ¢å½å±
setNoScreenRecord(params);
}
} catch (Exception ex) {
ex.printStackTrace();
} return method.invoke(mWindowManager, args);
} /
*** ç¦æ¢å½å±
*/
private void setNoScreenRecord(WindowManager.LayoutParams params) {
setFlags(params, WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
} /
*** å 许å½å±
*/
private void setAllowScreenRecord(WindowManager.LayoutParams params) {
setFlags(params, 0, WindowManager.LayoutParams.FLAG_SECURE);
} /
*** 设置WindowManager.LayoutParams flagå±æ§ï¼åèç³»ç»ç±»Window.setFlags(int flags, int mask)ï¼
*
* @param params WindowManager.LayoutParams
* @param flags The new window flags (see WindowManager.LayoutParams).
* @param mask Which of the window flag bits to modify.
*/
private void setFlags(WindowManager.LayoutParams params, int flags, int mask) { try { if (params == null) { return;
} params.flags = (params.flags & ~mask) | (flags & mask);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Popwindowç¦æ¢å½å±å·¥å ·ç±»ç使ç¨ï¼ä»£ç 示ä¾ï¼
//å建PopupWindow
//æ£å¸¸é¡¹ç®ä¸ï¼è¯¥æ¹æ³å¯æ¹æå·¥åç±»
//æ£å¸¸é¡¹ç®ä¸ï¼ä¹å¯èªå®ä¹PopupWindowï¼å¨å ¶ç±»ä¸è®¾ç½®ç¦æ¢å½å±
private PopupWindow createPopupWindow(View view, int width, int height) {
PopupWindow popupWindow = new PopupWindow(view, width, height); //PopupWindowç¦æ¢å½å±
PopNoRecordProxy.instance().noScreenRecord(popupWindow); return popupWindow;
} //æ¾ç¤ºPopupwindow
private void showPm() {
View view = LayoutInflater.from(this).inflate(R.layout.pm1, null);
PopupWindow pw = createPopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pw1.setFocusable(false);
pw1.showAtLocation(this.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.RIGHT, PopConst.PopOffsetX, PopConst.PopOffsetY);
}
å½å±ææå¾ï¼