Android WindowManager and its animation Problems

Source: Internet
Author: User

Android WindowManager and its animation Problems
Current: 1.0 Date: 2014.8.16 2014.8.23 2014.8.26 copyright:©2014 kin 1. In overview development, the animation effect on WindowManager is invalid, for example, the following code:

        ImageView iv = new ImageView(this);        iv.setImageResource(R.drawable.ic_launcher);        TranslateAnimation animation = new TranslateAnimation(                Animation.ABSOLUTE, 20, Animation.ABSOLUTE, 300,                Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 400);        animation.setDuration(1000);        animation.setFillAfter(false);        iv.setAnimation(animation);        WindowManager mWindowManager = (WindowManager) this                .getSystemService(Context.WINDOW_SERVICE);        WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();        mLayoutParams.x = 20;        mLayoutParams.y = 100;        mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;        mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;        mLayoutParams.gravity = Gravity.TOP;        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;        mWindowManager.addView(iv, mLayoutParams);        animation.start();
Ii. Why does the analysis not execute the animation? the reason is: the view which is going to be animated must not be directly added to the top window, because top window of android is not a real ViewGroup. so the view must be added to a ViewGroup like FrameLayout first and then this ViewGroup be added to the top window. it means that the animation execution condition is that the animation cannot be directly added to the top-level Window, but a container is required. For example, animations can be used for controls defined in xml. Later, we found that WindowManager. LayoutParams has an animation attribute: windowAnimations, which can be used in this way.
Lp = new WindowManager. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, WindowManager. layoutParams. TYPE_PHONE, WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_TOUCHABLE, PixelFormat. RGBA_8888); lp. gravity = Gravity. LEFT | Gravity. TOP; lp. windowAnimations = R. style. anim_view; // animated wm = (WindowManager) getSystemService (Context. WINDOW_SERVICE); wm. addView (view, lp );
However, this is an animation of the entire View, rather than an animation of a control in the View. In addition, the animation effect is displayed only when the View status changes. For example, the animation effect is only available when the image disappears or appears. Therefore, this solution does not work. Since WindowManager is not a ViewGroup, construct a container to load WindowManager as follows:
/*****/Package com. kince. apus. widget; import com. kince. apus. r; import android. animation. animator; import android. animation. animator. animatorListener; import android. animation. animatorSet; import android. animation. objectAnimator; import android. content. context; import android. graphics. pixelFormat; import android. OS. handler; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. view. windowManager; import android. widget. frameLayout; import android. widget. imageView; import android. widget. textView;/*** @ author kince * @ category Windowmanager application in Layout ***/public class GuideLayout extends FrameLayout {private WindowManager wManager; private WindowManager. layoutParams wmParams; private View addView; private TextView mTextView; private ImageView mImageView; private boolean isAddView; private AnimatorSet values, values; private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {super. handleMessage (msg); switch (msg. what) {case 1: showAnimator (); break; default: break ;}};};/*** @ param context */public GuideLayout (Context context) {this (context, null); // TODO Auto-generated constructor stub}/*** @ param context * @ param attrs */public GuideLayout (Context context, AttributeSet attrs) {this (context, attrs, 0 ); // TODO Auto-generated constructor stub}/*** @ param context * @ param attrs * @ param defStyle */public GuideLayout (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); addView = LayoutInflater. from (context ). inflate (R. layout. guide_layout, this); mTextView = (TextView) addView. findViewById (R. id. TV); mImageView = (ImageView) addView. findViewById (R. id. iv); mTextView. setVisibility (View. GONE); mImageView. setVisibility (View. GONE); setAnimator (); getWindowManager (context);}/*** @ category: Use * @ param context */private void getWindowManager (final Context context) to instantiate the WindowManager initial simulated location) {wManager = (WindowManager) context. getApplicationContext (). getSystemService (Context. WINDOW_SERVICE); wmParams = new WindowManager. layoutParams (); wmParams. type = WindowManager. layoutParams. TYPE_SYSTEM_ALERT; wmParams. format = PixelFormat. TRANSPARENT; wmParams. flags = WindowManager. layoutParams. FLAG_NOT_TOUCH_MODAL | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_TOUCHABLE; wmParams. gravity = 17; wmParams. width = WindowManager. layoutParams. MATCH_PARENT; wmParams. height = WindowManager. layoutParams. MATCH_PARENT;} private void setAnimator () {mShowAnimatorSet = new AnimatorSet (); Animator [] showAnimator = new Animator [2]; showAnimator [0] = ObjectAnimator. ofFloat (mTextView, "alpha", new float [] {0.0F, 1.0F}); showAnimator [1] = ObjectAnimator. ofFloat (mImageView, "alpha", new float [] {0.0F, 1.0F}); mShowAnimatorSet. playTogether (showAnimator); mShowAnimatorSet. setDuration (1500l); mHideAnimatorSet = new AnimatorSet (); Animator [] hideAnimator = new Animator [2]; hideAnimator [0] = ObjectAnimator. ofFloat (mTextView, "alpha", new float [] {1.0F, 0.0F}); hideAnimator [1] = ObjectAnimator. ofFloat (mImageView, "alpha", new float [] {1.0F, 0.0F}); mHideAnimatorSet. playTogether (hideAnimator); mHideAnimatorSet. setDuration (1500l);} public void showAnimator () {mTextView. setVisibility (View. VISIBLE); mImageView. setVisibility (View. VISIBLE); mShowAnimatorSet. start (); isAddView = true;} public void hideAnimator () {mHideAnimatorSet. start (); mHideAnimatorSet. addListener (new AnimatorListener () {@ Override public void onAnimationStart (Animator animation) {// TODO Auto-generated method stub} @ Override public void onAnimationRepeat (Animator animation) {// TODO Auto-generated method stub} @ Override public void onAnimationEnd (Animator animation) {// TODO Auto-generated method stub mTextView. setVisibility (View. INVISIBLE); mImageView. setVisibility (View. INVISIBLE) ;}@ Override public void onAnimationCancel (Animator animation) {// TODO Auto-generated method stub }});} public void sendMessage () {if (isAddView) {wManager. removeView (this); mHandler. removeMessages (1); isAddView = false;} mHandler. sendEmptyMessage (1); wManager. addView (this, wmParams );}}
In this way, you can achieve the animation effect on WindowManager. In fact, the reason for this phenomenon is that the Android API and its system are not deeply understood. Ignoring the basic conditions required for animation execution, the problem of shooting is that the problem is not comprehensive enough. Therefore, no matter which function is developed and which API is used, preliminary planning and research are very important. Know yourself and know him, that's all.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.