Java Code Examples for android.view.ViewTreeObserver#addOnPreDrawListener()

The following examples show how to use android.view.ViewTreeObserver#addOnPreDrawListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhotoAlbumPickerActivity.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void fixLayout()
{
    if (listView != null)
    {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
        {
            @Override
            public boolean onPreDraw()
            {
                fixLayoutInternal();
                if (listView != null)
                {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
    }
}
 
Example 2
Source File: PasscodeActivity.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if (listView != null)
    {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
        {
            @Override
            public boolean onPreDraw()
            {
                listView.getViewTreeObserver().removeOnPreDrawListener(this);
                fixLayoutInternal();
                return true;
            }
        });
    }
}
 
Example 3
Source File: IdenticonActivity.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void fixLayout() {
    ViewTreeObserver obs = fragmentView.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (fragmentView == null) {
                return true;
            }
            fragmentView.getViewTreeObserver().removeOnPreDrawListener(this);
            WindowManager manager = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE);
            int rotation = manager.getDefaultDisplay().getRotation();

            if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            } else {
                linearLayout.setOrientation(LinearLayout.VERTICAL);
            }

            fragmentView.setPadding(fragmentView.getPaddingLeft(), 0, fragmentView.getPaddingRight(), fragmentView.getPaddingBottom());
            return true;
        }
    });
}
 
Example 4
Source File: MatrixImageView.java    From LLApp with Apache License 2.0 6 votes vote down vote up
@Override
public void setImageBitmap(Bitmap bm) {
	// TODO Auto-generated method stub
	super.setImageBitmap(bm);
	//大小为0 表示当前控件大小未测量  设置监听函数  在绘制前赋值
	if(getWidth()==0){
		ViewTreeObserver vto = getViewTreeObserver();   
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
		{
			public boolean onPreDraw()
			{
				initData();
				//赋值结束后,移除该监听函数
				MatrixImageView.this.getViewTreeObserver().removeOnPreDrawListener(this);
				return true;
			}
		});
	}else {
		initData();
	}		
}
 
Example 5
Source File: ViewUtils.java    From PowerRecyclerView with Apache License 2.0 6 votes vote down vote up
public static void calculateTag1(TextView first, TextView second, final String text) {
    ViewTreeObserver observer = first.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            Layout layout = first.getLayout();
            int lineEnd = layout.getLineEnd(0);
            String substring = text.substring(0, lineEnd);
            String substring1 = text.substring(lineEnd, text.length());
            Log.i("TAG", "onGlobalLayout:" + "+end:" + lineEnd);
            Log.i("TAG", "onGlobalLayout: 第一行的内容::" + substring);
            Log.i("TAG", "onGlobalLayout: 第二行的内容::" + substring1);
            if (TextUtils.isEmpty(substring1)) {
                second.setVisibility(View.GONE);
                second.setText(null);
            } else {
                second.setVisibility(View.VISIBLE);
                second.setText(substring1);
            }
            first.getViewTreeObserver().removeOnPreDrawListener(
                    this);
            return false;
        }
    });

}
 
Example 6
Source File: GLRelativeLayout.java    From EZFilter with MIT License 6 votes vote down vote up
private void init() {
    ViewTreeObserver observer = getViewTreeObserver();
    if (observer != null) {
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (isDirty()) {
                    // 只在需要绘制的时候刷新
                    if (mRenderMode == GLSurfaceView.RENDERMODE_WHEN_DIRTY) {
                        invalidate();
                    }
                }
                return true;
            }
        });
    }
}
 
Example 7
Source File: FloatingAction.java    From FloatingAction with MIT License 5 votes vote down vote up
private void leHide(final boolean hide, final  boolean animated, final boolean deferred) {
  if (mHide != hide || deferred) {
    mHide = hide;
    final int height = mView.getHeight();
    if (height == 0 && !deferred) {
      // Dang it, haven't been drawn before, defer! defer!
      final ViewTreeObserver vto = mView.getViewTreeObserver();
      if (vto.isAlive()) {
        vto.addOnPreDrawListener(new OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            // Sometimes is not the same we used to know
            final ViewTreeObserver currentVto = mView.getViewTreeObserver();
            if (currentVto.isAlive()) {
              currentVto.removeOnPreDrawListener(this);
            }
            leHide(hide, animated, true);
            return true;
          }
        });
        return;
      }
    }
    int marginBottom = 0;
    final ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
    if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
      marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
    }
    final int translationY = mHide ? height + marginBottom : 0;
    if (animated) {
      mView.animate()
          .setInterpolator(mInterpolator)
          .setDuration(mDuration)
          .translationY(translationY);
    } else {
      mView.setTranslationY(translationY);
    }
  }
}
 
Example 8
Source File: TransitionAnimation.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static MoveData startAnimation(final View toView, Bundle transitionBundle, Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator, final Animator.AnimatorListener listener) {
    Context context = toView.getContext();
    if (context instanceof ContextThemeWrapper) {
        context = ((ContextThemeWrapper) context).getBaseContext();
    }

    final TransitionData transitionData = new TransitionData(context, transitionBundle);
    if (transitionData.imageFilePath != null) {
        setImageToView(toView, transitionData.imageFilePath);
    }

    final MoveData moveData = new MoveData();
    moveData.toView = toView;
    moveData.duration = duration;
    if (savedInstanceState == null) {

        ViewTreeObserver observer = toView.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                toView.getViewTreeObserver().removeOnPreDrawListener(this);

                int[] screenLocation = new int[2];
                toView.getLocationOnScreen(screenLocation);
                moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
                moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];

                moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
                moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();
                runEnterAnimation(moveData, interpolator, listener);
                return true;
            }
        });
    }
    return moveData;
}
 
Example 9
Source File: WallpapersListActivity.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void fixLayout() {
    if (listView != null) {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fixLayoutInternal();
                if (listView != null) {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
    }
}
 
Example 10
Source File: FloatingActionButton.java    From LLApp with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
    if (mVisible != visible || force) {
        mVisible = visible;
        int height = getHeight();
        if (height == 0 && !force) {
            ViewTreeObserver vto = getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggle(visible, animate, true);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height + getMarginBottom();
        if (animate) {
            ViewPropertyAnimator.animate(this).setInterpolator(mInterpolator)
                .setDuration(TRANSLATE_DURATION_MILLIS)
                .translationY(translationY);
        } else {
            ViewHelper.setTranslationY(this, translationY);
        }

        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        if (!hasHoneycombApi()) {
            setClickable(visible);
        }
    }
}
 
Example 11
Source File: DragUtils.java    From DraggedViewPager with Apache License 2.0 5 votes vote down vote up
public static void postOnPreDraw(View view, final Runnable runnable) {
    final ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (observer.isAlive()) {
                observer.removeOnPreDrawListener(this);
            }
            runnable.run();
            return true;
        }
    });
}
 
Example 12
Source File: FloatingActionsMenu.java    From TestChat with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
        if (mVisible != visible || force) {
                mVisible = visible;
                int height = getHeight();
                if (height == 0 && !force) {
                        ViewTreeObserver vto = getViewTreeObserver();
                        if (vto.isAlive()) {
                                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                                        @Override
                                        public boolean onPreDraw() {
                                                ViewTreeObserver currentVto = getViewTreeObserver();
                                                if (currentVto.isAlive()) {
                                                        currentVto.removeOnPreDrawListener(this);
                                                }
                                                toggle(visible, animate, true);
                                                return true;
                                        }
                                });
                                return;
                        }
                }
                int translationY = visible ? 0 : height + mAddButton.getMarginBottom();
                if (animate) {
                        ViewPropertyAnimator.animate(this).setInterpolator(mInterpolator)
                                .setDuration(TRANSLATE_DURATION_MILLIS)
                                .translationY(translationY);
                } else {
                        ViewHelper.setTranslationY(this, translationY);
                }

                // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
                if (!hasHoneycombApi()) {
                        setClickable(visible);
                }
        }
}
 
Example 13
Source File: KJDragGridView.java    From KJFrameForAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 交换item,并且控制item之间的显示与隐藏效果
 * 
 * @param moveX
 * @param moveY
 */
private void onSwapItem(int moveX, int moveY) {
    // 获取我们手指移动到的那个item的position

    final int tempPosition = pointToPosition(moveX, moveY);

    // 假如tempPosition 改变了并且tempPosition不等于-1,则进行交换

    if (tempPosition != mDragPosition
            && tempPosition != AdapterView.INVALID_POSITION
            && mAnimationEnd) {
        mDragAdapter.reorderItems(mDragPosition, tempPosition);
        mDragAdapter.setHideItem(tempPosition);

        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);
                animateReorder(mDragPosition, tempPosition);
                mDragPosition = tempPosition;
                return true;
            }
        });

    }
}
 
Example 14
Source File: TransitionAnimation.java    From candybar-library with Apache License 2.0 5 votes vote down vote up
public static MoveData startAnimation(Context context, final View toView, Bundle transitionBundle, Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator) {
    final TransitionData transitionData = new TransitionData(context, transitionBundle);
    if (transitionData.imageFilePath != null) {
        setImageToView(toView, transitionData.imageFilePath);
    }
    final MoveData moveData = new MoveData();
    moveData.toView = toView;
    moveData.duration = duration;
    if (savedInstanceState == null) {
        ViewTreeObserver observer = toView.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                toView.getViewTreeObserver().removeOnPreDrawListener(this);

                int[] screenLocation = new int[2];
                toView.getLocationOnScreen(screenLocation);
                moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
                moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];

                moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
                moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();

                runEnterAnimation(moveData, interpolator);

                return true;
            }
        });
    }
    return moveData;
}
 
Example 15
Source File: PhotoPickerActivity.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void fixLayout() {
    if (listView != null) {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fixLayoutInternal();
                if (listView != null) {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
    }
}
 
Example 16
Source File: FloatingActionsMenu.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
    if (mVisible != visible || force) {
        mVisible = visible;
        int height = getHeight();
        if (height == 0 && !force) {
            ViewTreeObserver vto = getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggle(visible, animate, true);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height + getMarginBottom();
        if (animate) {
            animate().setInterpolator(mInterpolator)
                .setDuration(TRANSLATE_DURATION_MILLIS)
                .translationY(translationY);
        } else {
            setTranslationY(translationY);
        }
        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        if (!Utils.hasHoneycomb()) {
            setClickable(visible);
        }
    }

    if(isMenuOpen()) {
        closeMenu();
    }
}
 
Example 17
Source File: PhotoAlbumPickerActivity.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void fixLayout() {
    if (listView != null) {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fixLayoutInternal();
                if (listView != null) {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
    }
}
 
Example 18
Source File: AnimatedLinearLayout.java    From RxAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
private void waitForNextFrame() {
    ViewTreeObserver treeObserver = getViewTreeObserver();
    if (treeObserver != null && treeObserver.isAlive()) {
        treeObserver.addOnPreDrawListener(new PreDrawListener());
    }
}
 
Example 19
Source File: AnimatedLinearLayout.java    From MVPAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
private void waitForNextFrame() {
    ViewTreeObserver treeObserver = getViewTreeObserver();
    if (treeObserver != null && treeObserver.isAlive()) {
        treeObserver.addOnPreDrawListener(new PreDrawListener());
    }
}
 
Example 20
Source File: AvsActivity.java    From AVS with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	setContentView(R.layout.activity_main);
	if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    	getWindow().setStatusBarColor(getResources().getColor(R.color.background));
    }
	mLayoutInflater = LayoutInflater.from(this);
	TextView cidView = (TextView) findViewById(R.id.cid);
	cidView.setOnClickListener(this);
	TextView pwdView = (TextView) findViewById(R.id.pwd);
	pwdView.setOnClickListener(this);
	TextView statusView = (TextView) findViewById(R.id.logState);
	TextView deviceName = (TextView) findViewById(R.id.deviceName);
	ImageView barCodeView = (ImageView) findViewById(R.id.barcode);
	ImageView menuView = (ImageView) findViewById(R.id.menu);
	menuView.setOnClickListener(this);
    menu_layout = (LinearLayout) findViewById(R.id.menu_layout);
    menu_layout.setOnClickListener(this);
    findViewById(R.id.main_layout).setOnClickListener(this);
    findViewById(R.id.help).setOnClickListener(this);
    findViewById(R.id.feedback).setOnClickListener(this);
    findViewById(R.id.about).setOnClickListener(this);
    findViewById(R.id.disclaimer).setOnClickListener(this);
    findViewById(R.id.avs_title).setOnClickListener(this);
    mMyAvsHelper = new MyAvsHelper(getApplicationContext());
    mMyAvsHelper.setViews(cidView, pwdView, statusView, deviceName, barCodeView);
    mMyAvsHelper.login();
	final int[] size = mMyAvsHelper.getVideoSize();
	
	mMediaSurfaceView = (MediaSurfaceView) findViewById(R.id.cameraView);
	mMediaSurfaceView.openCamera(mMyAvsHelper);
	
	ViewTreeObserver viewTreeObserver = mMediaSurfaceView.getViewTreeObserver();
	viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener(){
		@Override
		public boolean onPreDraw(){
			if (isFirst){
				int height = mMediaSurfaceView.getMeasuredHeight();
				int width = mMediaSurfaceView.getMeasuredWidth();
				float r = (float)height/(float)width;
				float r2 = (float)size[1]/(float)size[0];
				RelativeLayout.LayoutParams pvLayout = (RelativeLayout.LayoutParams) mMediaSurfaceView.getLayoutParams();
				if (r > r2){
					pvLayout.height = (int) (width*r2);
				}else{
					pvLayout.width = (int) (height/r2);
				}
				isFirst = false;
			}
			return true;
		}
	});		
	
	mShowChinese = "zh".equals(Locale.getDefault().getLanguage().toLowerCase());
	//update
	UmengUpdateAgent.setUpdateOnlyWifi(false);
	UmengUpdateAgent.update(this);
	
}