Search in sources :

Example 11 with ViewTreeObserver

use of android.view.ViewTreeObserver in project UltimateAndroid by cymcsg.

the class TextRoundCornerProgressBar method setup.

@SuppressLint("NewApi")
private void setup(Context context, AttributeSet attrs) {
    int color;
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerProgress);
    autoTextChange = typedArray.getBoolean(R.styleable.RoundCornerProgress_rcp_autoTextChange, false);
    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, metrics);
    radius = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcp_backgroundRadius, radius);
    textViewValue = (TextView) findViewById(R.id.round_corner_progress_text);
    textSize = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcp_textProgressSize, textSize);
    textViewValue.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textViewValue.setTextColor(typedArray.getColor(R.styleable.RoundCornerProgress_rcp_textProgressColor, textColor));
    text = typedArray.getString(R.styleable.RoundCornerProgress_rcp_textProgress);
    text = (text == null) ? "" : text;
    textViewValue.setText(text);
    textPadding = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcp_textProgressPadding, textPadding);
    textViewValue.setPadding(textPadding, 0, textPadding, 0);
    textUnit = typedArray.getString(R.styleable.RoundCornerProgress_rcp_textProgressUnit);
    textUnit = (textUnit == null) ? "" : textUnit;
    textWidth = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcp_textProgressWidth, textWidth);
    layoutBackground = (LinearLayout) findViewById(R.id.round_corner_progress_background);
    padding = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcp_backgroundPadding, padding);
    layoutBackground.setPadding(padding, padding, padding, padding);
    if (!isBackgroundColorSetBeforeDraw) {
        color = typedArray.getColor(R.styleable.RoundCornerProgress_rcp_backgroundColor, backgroundColor);
        setBackgroundColor(color);
    }
    ViewTreeObserver observer = layoutBackground.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            layoutBackground.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            int height = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                backgroundWidth = layoutBackground.getMeasuredWidth() - textWidth;
                height = layoutBackground.getMeasuredHeight();
            } else {
                backgroundWidth = layoutBackground.getWidth() - textWidth;
                height = layoutBackground.getHeight();
            }
            backgroundHeight = (height == 0) ? (int) dp2px(DEFAULT_PROGRESS_BAR_HEIGHT) : height;
            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) layoutBackground.getLayoutParams();
            params.width = backgroundWidth;
            params.height = backgroundHeight;
            layoutBackground.setLayoutParams(params);
            setProgress(progress);
        }
    });
    layoutProgress = (LinearLayout) findViewById(R.id.round_corner_progress_progress);
    if (!isProgressColorSetBeforeDraw) {
        color = typedArray.getColor(R.styleable.RoundCornerProgress_rcp_progressColor, progressColor);
        setProgressColor(color);
    }
    if (!isMaxProgressSetBeforeDraw) {
        max = (int) typedArray.getInt(R.styleable.RoundCornerProgress_rcp_max, 0);
    }
    if (!isProgressSetBeforeDraw) {
        progress = (int) typedArray.getInt(R.styleable.RoundCornerProgress_rcp_progress, 0);
    }
    typedArray.recycle();
}
Also used : ViewGroup(android.view.ViewGroup) TypedArray(android.content.res.TypedArray) OnGlobalLayoutListener(android.view.ViewTreeObserver.OnGlobalLayoutListener) DisplayMetrics(android.util.DisplayMetrics) ViewTreeObserver(android.view.ViewTreeObserver) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 12 with ViewTreeObserver

use of android.view.ViewTreeObserver in project Libraries-for-Android-Developers by eoecn.

the class MenuPopupHelper method tryShow.

public boolean tryShow() {
    mPopup = new IcsListPopupWindow(mContext, null, R.attr.popupMenuStyle);
    mPopup.setOnDismissListener(this);
    mPopup.setOnItemClickListener(this);
    mAdapter = new MenuAdapter(mMenu);
    mPopup.setAdapter(mAdapter);
    mPopup.setModal(true);
    View anchor = mAnchorView;
    if (anchor != null) {
        // Don't attach to the VTO unless the anchor itself is attached to avoid VTO-related leaks.
        if (anchor.getWindowToken() != null) {
            ViewTreeObserver vto = anchor.getViewTreeObserver();
            if (vto != mTreeObserver) {
                if (mTreeObserver != null && mTreeObserver.isAlive()) {
                    mTreeObserver.removeGlobalOnLayoutListener(this);
                }
                if ((mTreeObserver = vto) != null) {
                    vto.addOnGlobalLayoutListener(this);
                }
            }
        } else if (anchor instanceof View_HasStateListenerSupport) {
            ((View_HasStateListenerSupport) anchor).addOnAttachStateChangeListener(this);
        }
        mPopup.setAnchorView(anchor);
    } else {
        return false;
    }
    mPopup.setContentWidth(Math.min(measureContentWidth(mAdapter), mPopupMaxWidth));
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
    mPopup.show();
    mPopup.getListView().setOnKeyListener(this);
    return true;
}
Also used : View_HasStateListenerSupport(com.actionbarsherlock.internal.view.View_HasStateListenerSupport) View(android.view.View) AdapterView(android.widget.AdapterView) ViewTreeObserver(android.view.ViewTreeObserver) IcsListPopupWindow(com.actionbarsherlock.internal.widget.IcsListPopupWindow)

Example 13 with ViewTreeObserver

use of android.view.ViewTreeObserver in project Libraries-for-Android-Developers by eoecn.

the class ActivityChooserView method dismissPopup.

/**
     * Dismisses the popup window with activities.
     *
     * @return True if dismissed, false if already dismissed.
     */
public boolean dismissPopup() {
    if (isShowingPopup()) {
        getListPopupWindow().dismiss();
        ViewTreeObserver viewTreeObserver = getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
        }
    }
    return true;
}
Also used : ViewTreeObserver(android.view.ViewTreeObserver)

Example 14 with ViewTreeObserver

use of android.view.ViewTreeObserver in project Libraries-for-Android-Developers by eoecn.

the class ActivityChooserView method onDetachedFromWindow.

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    ActivityChooserModel dataModel = mAdapter.getDataModel();
    if (dataModel != null) {
        try {
            dataModel.unregisterObserver(mModelDataSetOberver);
        } catch (IllegalStateException e) {
        //Oh, well... fixes issue #557
        }
    }
    ViewTreeObserver viewTreeObserver = getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
    }
    mIsAttachedToWindow = false;
}
Also used : ViewTreeObserver(android.view.ViewTreeObserver)

Example 15 with ViewTreeObserver

use of android.view.ViewTreeObserver in project android_frameworks_base by ParanoidAndroid.

the class LayoutTransition method runChangeTransition.

/**
     * This function sets up animations on all of the views that change during layout.
     * For every child in the parent, we create a change animation of the appropriate
     * type (appearing, disappearing, or changing) and ask it to populate its start values from its
     * target view. We add layout listeners to all child views and listen for changes. For
     * those views that change, we populate the end values for those animations and start them.
     * Animations are not run on unchanging views.
     *
     * @param parent The container which is undergoing a change.
     * @param newView The view being added to or removed from the parent. May be null if the
     * changeReason is CHANGING.
     * @param changeReason A value of APPEARING, DISAPPEARING, or CHANGING, indicating whether the
     * transition is occurring because an item is being added to or removed from the parent, or
     * if it is running in response to a layout operation (that is, if the value is CHANGING).
     */
private void runChangeTransition(final ViewGroup parent, View newView, final int changeReason) {
    Animator baseAnimator = null;
    Animator parentAnimator = null;
    final long duration;
    switch(changeReason) {
        case APPEARING:
            baseAnimator = mChangingAppearingAnim;
            duration = mChangingAppearingDuration;
            parentAnimator = defaultChangeIn;
            break;
        case DISAPPEARING:
            baseAnimator = mChangingDisappearingAnim;
            duration = mChangingDisappearingDuration;
            parentAnimator = defaultChangeOut;
            break;
        case CHANGING:
            baseAnimator = mChangingAnim;
            duration = mChangingDuration;
            parentAnimator = defaultChange;
            break;
        default:
            // Shouldn't reach here
            duration = 0;
            break;
    }
    // If the animation is null, there's nothing to do
    if (baseAnimator == null) {
        return;
    }
    // reset the inter-animation delay, in case we use it later
    staggerDelay = 0;
    // used for later cleanup
    final ViewTreeObserver observer = parent.getViewTreeObserver();
    if (!observer.isAlive()) {
        // If the observer's not in a good state, skip the transition
        return;
    }
    int numChildren = parent.getChildCount();
    for (int i = 0; i < numChildren; ++i) {
        final View child = parent.getChildAt(i);
        // only animate the views not being added or removed
        if (child != newView) {
            setupChangeAnimation(parent, changeReason, baseAnimator, duration, child);
        }
    }
    if (mAnimateParentHierarchy) {
        ViewGroup tempParent = parent;
        while (tempParent != null) {
            ViewParent parentParent = tempParent.getParent();
            if (parentParent instanceof ViewGroup) {
                setupChangeAnimation((ViewGroup) parentParent, changeReason, parentAnimator, duration, tempParent);
                tempParent = (ViewGroup) parentParent;
            } else {
                tempParent = null;
            }
        }
    }
    // This is the cleanup step. When we get this rendering event, we know that all of
    // the appropriate animations have been set up and run. Now we can clear out the
    // layout listeners.
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        public boolean onPreDraw() {
            parent.getViewTreeObserver().removeOnPreDrawListener(this);
            int count = layoutChangeListenerMap.size();
            if (count > 0) {
                Collection<View> views = layoutChangeListenerMap.keySet();
                for (View view : views) {
                    View.OnLayoutChangeListener listener = layoutChangeListenerMap.get(view);
                    view.removeOnLayoutChangeListener(listener);
                }
            }
            layoutChangeListenerMap.clear();
            return true;
        }
    });
}
Also used : ViewGroup(android.view.ViewGroup) ViewParent(android.view.ViewParent) Collection(java.util.Collection) ViewTreeObserver(android.view.ViewTreeObserver) View(android.view.View)

Aggregations

ViewTreeObserver (android.view.ViewTreeObserver)222 View (android.view.View)56 OnGlobalLayoutListener (android.view.ViewTreeObserver.OnGlobalLayoutListener)25 ImageView (android.widget.ImageView)25 TextView (android.widget.TextView)15 ViewGroup (android.view.ViewGroup)14 SuppressLint (android.annotation.SuppressLint)12 AdapterView (android.widget.AdapterView)12 TypedArray (android.content.res.TypedArray)7 Test (org.junit.Test)7 RectF (android.graphics.RectF)6 DisplayMetrics (android.util.DisplayMetrics)6 ViewParent (android.view.ViewParent)6 LinearLayout (android.widget.LinearLayout)6 ListView (android.widget.ListView)6 Resources (android.content.res.Resources)5 ValueAnimator (android.animation.ValueAnimator)4 TargetApi (android.annotation.TargetApi)4 Paint (android.graphics.Paint)4 Point (android.graphics.Point)4