Search in sources :

Example 51 with DisplayMetrics

use of android.util.DisplayMetrics in project XobotOS by xamarin.

the class ScaleGestureDetector method onTouchEvent.

public boolean onTouchEvent(MotionEvent event) {
    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onTouchEvent(event, 0);
    }
    final int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        // Start fresh
        reset();
    }
    boolean handled = true;
    if (mInvalidGesture) {
        handled = false;
    } else if (!mGestureInProgress) {
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                {
                    mActiveId0 = event.getPointerId(0);
                    mActive0MostRecent = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                reset();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                {
                    // We have a new multi-finger gesture
                    // as orientation can change, query the metrics in touch down
                    DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
                    mRightSlopEdge = metrics.widthPixels - mEdgeSlop;
                    mBottomSlopEdge = metrics.heightPixels - mEdgeSlop;
                    if (mPrevEvent != null)
                        mPrevEvent.recycle();
                    mPrevEvent = MotionEvent.obtain(event);
                    mTimeDelta = 0;
                    int index1 = event.getActionIndex();
                    int index0 = event.findPointerIndex(mActiveId0);
                    mActiveId1 = event.getPointerId(index1);
                    if (index0 < 0 || index0 == index1) {
                        // Probably someone sending us a broken event stream.
                        index0 = findNewActiveIndex(event, index0 == index1 ? -1 : mActiveId1, index0);
                        mActiveId0 = event.getPointerId(index0);
                    }
                    mActive0MostRecent = false;
                    setContext(event);
                    // Check if we have a sloppy gesture. If so, delay
                    // the beginning of the gesture until we're sure that's
                    // what the user wanted. Sloppy gestures can happen if the
                    // edge of the user's hand is touching the screen, for example.
                    final float edgeSlop = mEdgeSlop;
                    final float rightSlop = mRightSlopEdge;
                    final float bottomSlop = mBottomSlopEdge;
                    float x0 = getRawX(event, index0);
                    float y0 = getRawY(event, index0);
                    float x1 = getRawX(event, index1);
                    float y1 = getRawY(event, index1);
                    boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop;
                    boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop;
                    if (p0sloppy && p1sloppy) {
                        mFocusX = -1;
                        mFocusY = -1;
                        mSloppyGesture = true;
                    } else if (p0sloppy) {
                        mFocusX = event.getX(index1);
                        mFocusY = event.getY(index1);
                        mSloppyGesture = true;
                    } else if (p1sloppy) {
                        mFocusX = event.getX(index0);
                        mFocusY = event.getY(index0);
                        mSloppyGesture = true;
                    } else {
                        mSloppyGesture = false;
                        mGestureInProgress = mListener.onScaleBegin(this);
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (mSloppyGesture) {
                    // Initiate sloppy gestures if we've moved outside of the slop area.
                    final float edgeSlop = mEdgeSlop;
                    final float rightSlop = mRightSlopEdge;
                    final float bottomSlop = mBottomSlopEdge;
                    int index0 = event.findPointerIndex(mActiveId0);
                    int index1 = event.findPointerIndex(mActiveId1);
                    float x0 = getRawX(event, index0);
                    float y0 = getRawY(event, index0);
                    float x1 = getRawX(event, index1);
                    float y1 = getRawY(event, index1);
                    boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop;
                    boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop;
                    if (p0sloppy) {
                        // Do we have a different pointer that isn't sloppy?
                        int index = findNewActiveIndex(event, mActiveId1, index0);
                        if (index >= 0) {
                            index0 = index;
                            mActiveId0 = event.getPointerId(index);
                            x0 = getRawX(event, index);
                            y0 = getRawY(event, index);
                            p0sloppy = false;
                        }
                    }
                    if (p1sloppy) {
                        // Do we have a different pointer that isn't sloppy?
                        int index = findNewActiveIndex(event, mActiveId0, index1);
                        if (index >= 0) {
                            index1 = index;
                            mActiveId1 = event.getPointerId(index);
                            x1 = getRawX(event, index);
                            y1 = getRawY(event, index);
                            p1sloppy = false;
                        }
                    }
                    if (p0sloppy && p1sloppy) {
                        mFocusX = -1;
                        mFocusY = -1;
                    } else if (p0sloppy) {
                        mFocusX = event.getX(index1);
                        mFocusY = event.getY(index1);
                    } else if (p1sloppy) {
                        mFocusX = event.getX(index0);
                        mFocusY = event.getY(index0);
                    } else {
                        mSloppyGesture = false;
                        mGestureInProgress = mListener.onScaleBegin(this);
                    }
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                if (mSloppyGesture) {
                    final int pointerCount = event.getPointerCount();
                    final int actionIndex = event.getActionIndex();
                    final int actionId = event.getPointerId(actionIndex);
                    if (pointerCount > 2) {
                        if (actionId == mActiveId0) {
                            final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex);
                            if (newIndex >= 0)
                                mActiveId0 = event.getPointerId(newIndex);
                        } else if (actionId == mActiveId1) {
                            final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex);
                            if (newIndex >= 0)
                                mActiveId1 = event.getPointerId(newIndex);
                        }
                    } else {
                        // Set focus point to the remaining finger
                        final int index = event.findPointerIndex(actionId == mActiveId0 ? mActiveId1 : mActiveId0);
                        if (index < 0) {
                            mInvalidGesture = true;
                            Log.e(TAG, "Invalid MotionEvent stream detected.", new Throwable());
                            if (mGestureInProgress) {
                                mListener.onScaleEnd(this);
                            }
                            return false;
                        }
                        mActiveId0 = event.getPointerId(index);
                        mActive0MostRecent = true;
                        mActiveId1 = -1;
                        mFocusX = event.getX(index);
                        mFocusY = event.getY(index);
                    }
                }
                break;
        }
    } else {
        // Transform gesture in progress - attempt to handle it
        switch(action) {
            case MotionEvent.ACTION_POINTER_DOWN:
                {
                    // End the old gesture and begin a new one with the most recent two fingers.
                    mListener.onScaleEnd(this);
                    final int oldActive0 = mActiveId0;
                    final int oldActive1 = mActiveId1;
                    reset();
                    mPrevEvent = MotionEvent.obtain(event);
                    mActiveId0 = mActive0MostRecent ? oldActive0 : oldActive1;
                    mActiveId1 = event.getPointerId(event.getActionIndex());
                    mActive0MostRecent = false;
                    int index0 = event.findPointerIndex(mActiveId0);
                    if (index0 < 0 || mActiveId0 == mActiveId1) {
                        // Probably someone sending us a broken event stream.
                        Log.e(TAG, "Got " + MotionEvent.actionToString(action) + " with bad state while a gesture was in progress. " + "Did you forget to pass an event to " + "ScaleGestureDetector#onTouchEvent?");
                        index0 = findNewActiveIndex(event, mActiveId0 == mActiveId1 ? -1 : mActiveId1, index0);
                        mActiveId0 = event.getPointerId(index0);
                    }
                    setContext(event);
                    mGestureInProgress = mListener.onScaleBegin(this);
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                {
                    final int pointerCount = event.getPointerCount();
                    final int actionIndex = event.getActionIndex();
                    final int actionId = event.getPointerId(actionIndex);
                    boolean gestureEnded = false;
                    if (pointerCount > 2) {
                        if (actionId == mActiveId0) {
                            final int newIndex = findNewActiveIndex(event, mActiveId1, actionIndex);
                            if (newIndex >= 0) {
                                mListener.onScaleEnd(this);
                                mActiveId0 = event.getPointerId(newIndex);
                                mActive0MostRecent = true;
                                mPrevEvent = MotionEvent.obtain(event);
                                setContext(event);
                                mGestureInProgress = mListener.onScaleBegin(this);
                            } else {
                                gestureEnded = true;
                            }
                        } else if (actionId == mActiveId1) {
                            final int newIndex = findNewActiveIndex(event, mActiveId0, actionIndex);
                            if (newIndex >= 0) {
                                mListener.onScaleEnd(this);
                                mActiveId1 = event.getPointerId(newIndex);
                                mActive0MostRecent = false;
                                mPrevEvent = MotionEvent.obtain(event);
                                setContext(event);
                                mGestureInProgress = mListener.onScaleBegin(this);
                            } else {
                                gestureEnded = true;
                            }
                        }
                        mPrevEvent.recycle();
                        mPrevEvent = MotionEvent.obtain(event);
                        setContext(event);
                    } else {
                        gestureEnded = true;
                    }
                    if (gestureEnded) {
                        // Gesture ended
                        setContext(event);
                        // Set focus point to the remaining finger
                        final int activeId = actionId == mActiveId0 ? mActiveId1 : mActiveId0;
                        final int index = event.findPointerIndex(activeId);
                        mFocusX = event.getX(index);
                        mFocusY = event.getY(index);
                        mListener.onScaleEnd(this);
                        reset();
                        mActiveId0 = activeId;
                        mActive0MostRecent = true;
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                mListener.onScaleEnd(this);
                reset();
                break;
            case MotionEvent.ACTION_UP:
                reset();
                break;
            case MotionEvent.ACTION_MOVE:
                {
                    setContext(event);
                    // finger is lifted.
                    if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) {
                        final boolean updatePrevious = mListener.onScale(this);
                        if (updatePrevious) {
                            mPrevEvent.recycle();
                            mPrevEvent = MotionEvent.obtain(event);
                        }
                    }
                }
                break;
        }
    }
    if (!handled && mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
    }
    return handled;
}
Also used : DisplayMetrics(android.util.DisplayMetrics)

Example 52 with DisplayMetrics

use of android.util.DisplayMetrics in project simple-tool-tip by xizzhu.

the class ToolTipView method onPreDraw.

@Override
public boolean onPreDraw() {
    container.getViewTreeObserver().removeOnPreDrawListener(this);
    Context context = container.getContext();
    if (!(context instanceof Activity)) {
        return false;
    }
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int displayWidth = displayMetrics.widthPixels;
    int displayHeight = displayMetrics.heightPixels;
    Rect rect = new Rect();
    anchorView.getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int[] location = new int[2];
    anchorView.getLocationInWindow(location);
    int anchorTop = location[1] - statusBarHeight;
    int anchorLeft = location[0];
    int anchorWidth = anchorView.getWidth();
    int anchorHeight = anchorView.getHeight();
    int textWidth = text.getWidth();
    int textHeight = text.getHeight();
    int arrowWidth = arrow.getWidth();
    int arrowHeight = arrow.getHeight();
    if (gravity == Gravity.TOP || gravity == Gravity.BOTTOM) {
        int width = Math.max(textWidth, arrowWidth);
        int height = textHeight + arrowHeight;
        int leftPadding;
        int topPadding;
        if (gravity == Gravity.TOP) {
            topPadding = anchorTop - height;
        } else {
            // gravity == Gravity.BOTTOM
            topPadding = anchorTop + anchorHeight;
        }
        int anchorHorizontalCenter = anchorLeft + anchorWidth / 2;
        int left = anchorHorizontalCenter - width / 2;
        int right = left + width;
        leftPadding = Math.max(0, right > displayWidth ? displayWidth - width : left);
        container.setPadding(leftPadding, topPadding, 0, 0);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) arrow.getLayoutParams();
        layoutParams.leftMargin = anchorHorizontalCenter - leftPadding - arrowWidth / 2;
        arrow.setLayoutParams(layoutParams);
        pivotX = anchorHorizontalCenter;
        pivotY = gravity == Gravity.TOP ? anchorTop : topPadding;
    } else {
        // gravity == Gravity.LEFT || gravity == Gravity.RIGHT
        int width = textWidth + arrowWidth;
        int height = Math.max(textHeight, arrowHeight);
        int leftPadding;
        int topPadding;
        int rightPadding;
        if (gravity == Gravity.LEFT) {
            leftPadding = Math.max(0, anchorLeft - width);
            rightPadding = displayWidth - anchorLeft;
            text.setMaxWidth(displayWidth - rightPadding - leftPadding - arrowWidth);
        } else {
            // gravity == Gravity.RIGHT
            leftPadding = anchorLeft + anchorWidth;
            rightPadding = 0;
        }
        int anchorVerticalCenter = anchorTop + anchorHeight / 2;
        int top = anchorVerticalCenter - height / 2;
        int bottom = top + height;
        topPadding = Math.max(0, bottom > displayHeight ? displayHeight - height : top);
        container.setPadding(leftPadding, topPadding, rightPadding, 0);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) arrow.getLayoutParams();
        layoutParams.topMargin = anchorVerticalCenter - topPadding - arrowHeight / 2;
        arrow.setLayoutParams(layoutParams);
        pivotX = gravity == Gravity.LEFT ? anchorLeft : leftPadding;
        pivotY = anchorVerticalCenter;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        container.setAlpha(0.0F);
        container.setPivotX(pivotX);
        container.setPivotY(pivotY);
        container.setScaleX(0.0F);
        container.setScaleY(0.0F);
        container.animate().setDuration(ANIMATION_DURATION).alpha(1.0F).scaleX(1.0F).scaleY(1.0F);
    } else {
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setDuration(ANIMATION_DURATION);
        animationSet.addAnimation(new AlphaAnimation(0.0F, 1.0F));
        animationSet.addAnimation(new ScaleAnimation(0.0F, 1.0F, 0.0F, 1.0F, pivotX, pivotY));
        container.startAnimation(animationSet);
    }
    return false;
}
Also used : Context(android.content.Context) Rect(android.graphics.Rect) ViewGroup(android.view.ViewGroup) Activity(android.app.Activity) DisplayMetrics(android.util.DisplayMetrics) AnimationSet(android.view.animation.AnimationSet) AlphaAnimation(android.view.animation.AlphaAnimation) ScaleAnimation(android.view.animation.ScaleAnimation)

Example 53 with DisplayMetrics

use of android.util.DisplayMetrics in project NoHttp by yanzhenjie.

the class DisplayUtils method initScreen.

/**
     * 初始化屏幕宽度和高度
     */
public static void initScreen(Activity activity) {
    if (isInitialize)
        return;
    isInitialize = true;
    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics metric = new DisplayMetrics();
    // 屏幕宽度、高度、密度、缩放因子
    if (VERSION.SDK_INT >= 17) {
        display.getRealMetrics(metric);
    } else {
        try {
            Class<?> clazz = Class.forName("android.view.Display");
            Method method = clazz.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, metric);
        } catch (Throwable e) {
            display.getMetrics(metric);
        }
    }
    try {
        // 状态栏高度
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Field field = clazz.getField("status_bar_height");
        int x = Integer.parseInt(field.get(clazz.newInstance()).toString());
        statusBarHeight = activity.getResources().getDimensionPixelSize(x);
    } catch (Throwable e) {
        e.printStackTrace();
        Rect outRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        statusBarHeight = outRect.top;
    }
    screenWidth = metric.widthPixels;
    screenHeight = metric.heightPixels;
    screenDpi = metric.densityDpi;
    density = metric.density;
    scaledDensity = metric.scaledDensity;
    if (screenWidth >= 320 && screenWidth < 480) {
        displayType = DISPLAY_SMALL;
    } else if (screenWidth >= 480 && screenWidth < 720) {
        displayType = DISPLAY_MIDDLE;
    } else if (screenWidth >= 720 && screenWidth < 1080) {
        displayType = DISPLAY_LARGE;
    } else {
        displayType = DISPLAY_XLARGE;
    }
}
Also used : Field(java.lang.reflect.Field) Rect(android.graphics.Rect) Method(java.lang.reflect.Method) DisplayMetrics(android.util.DisplayMetrics) Display(android.view.Display)

Example 54 with DisplayMetrics

use of android.util.DisplayMetrics in project horizontalpager by ysamlan.

the class HorizontalPager method init.

/**
     * Sets up the scroller and touch/fling sensitivity parameters for the pager.
     */
private void init() {
    mScroller = new Scroller(getContext());
    // Calculate the density-dependent snap velocity in pixels
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
    mDensityAdjustedSnapVelocity = (int) (displayMetrics.density * SNAP_VELOCITY_DIP_PER_SECOND);
    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    mTouchSlop = configuration.getScaledTouchSlop();
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
Also used : ViewConfiguration(android.view.ViewConfiguration) Scroller(android.widget.Scroller) DisplayMetrics(android.util.DisplayMetrics)

Example 55 with DisplayMetrics

use of android.util.DisplayMetrics in project banner by youth5201314.

the class App method getScreen.

public void getScreen(Context aty) {
    DisplayMetrics dm = aty.getResources().getDisplayMetrics();
    H = dm.heightPixels;
    W = dm.widthPixels;
}
Also used : DisplayMetrics(android.util.DisplayMetrics)

Aggregations

DisplayMetrics (android.util.DisplayMetrics)772 WindowManager (android.view.WindowManager)107 Resources (android.content.res.Resources)99 Display (android.view.Display)78 Configuration (android.content.res.Configuration)61 Point (android.graphics.Point)57 View (android.view.View)52 SuppressLint (android.annotation.SuppressLint)47 Bitmap (android.graphics.Bitmap)42 Paint (android.graphics.Paint)42 Activity (android.app.Activity)32 ImageView (android.widget.ImageView)27 AssetManager (android.content.res.AssetManager)25 TypedArray (android.content.res.TypedArray)25 Context (android.content.Context)23 TypedValue (android.util.TypedValue)23 ViewGroup (android.view.ViewGroup)23 TextView (android.widget.TextView)22 Intent (android.content.Intent)21 RelativeLayout (android.widget.RelativeLayout)20