Example usage for android.view View LAYER_TYPE_HARDWARE

List of usage examples for android.view View LAYER_TYPE_HARDWARE

Introduction

In this page you can find the example usage for android.view View LAYER_TYPE_HARDWARE.

Prototype

int LAYER_TYPE_HARDWARE

To view the source code for android.view View LAYER_TYPE_HARDWARE.

Click Source Link

Document

Indicates that the view has a hardware layer.

Usage

From source file:Main.java

public static void expand(final View v, final int targetHeight, int duration, Interpolator interpolator,
        final Animation.AnimationListener listener) {
    final int initialHeight = v.getMeasuredHeight();
    Animation a = new Animation() {
        @Override/*from w  w w .  java2s . c om*/
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = initialHeight + (int) (targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };
    a.setDuration(duration);
    if (interpolator != null) {
        a.setInterpolator(interpolator);
    }
    a.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            if (listener != null)
                listener.onAnimationStart(animation);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            v.setLayerType(View.LAYER_TYPE_NONE, null);
            if (listener != null)
                listener.onAnimationEnd(animation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            if (listener != null)
                onAnimationRepeat(animation);
        }
    });

    v.startAnimation(a);
}

From source file:com.android.madpausa.cardnotificationviewer.CardElementHolder.java

private void initAnimator() {
    scroller = new Scroller(root.getContext());
    animator = ValueAnimator.ofFloat(1, 0);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override//from  w  ww  .j  ava  2  s .c om
        public void onAnimationUpdate(ValueAnimator animation) {
            animateScroll();
            if (cancel)
                root.setAlpha((Float) animation.getAnimatedValue());
        }
    });
    animator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            root.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            endAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            endAnimation();
        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }

        private void endAnimation() {
            root.setTranslationX(scroller.getFinalX());
            root.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            if (cancel)
                nService.cancelNotification(sbn);
        }
    });
}

From source file:com.purchasingpower.inappbrowser.WebViewActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setContentView(R.layout.activity_main);
    setContentView(getResources().getIdentifier("nativebrowserplugin", "layout", getPackageName()));
    if (!checkPermission()) {
        requestPermission();/*  w w w .  j  a v a2s.com*/
    }
    Intent intent = getIntent();
    String url = intent.getExtras().getString("url");
    final WebView myWebView = (WebView) findViewById(
            getResources().getIdentifier("webview", "id", getPackageName()));
    myWebView.setWebViewClient(new MyWebViewClient());

    webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setAllowFileAccess(true);
    myWebView.setWebChromeClient(new PQChromeClient());
    //if SDK version is greater of 19 then activate hardware acceleration otherwise activate software acceleration
    if (Build.VERSION.SDK_INT >= 19) {
        myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) {
        myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    myWebView.loadUrl(url);

    back = (ImageView) findViewById(getResources().getIdentifier("back", "id", getPackageName()));
    back.setImageResource(getResources().getIdentifier("back_gray", "drawable", getPackageName()));
    forward = (ImageView) findViewById(getResources().getIdentifier("forward", "id", getPackageName()));
    forward.setImageResource(getResources().getIdentifier("forward_gray", "drawable", getPackageName()));
    done = (TextView) findViewById(getResources().getIdentifier("done", "id", getPackageName()));
    pbar = (ProgressBar) findViewById(getResources().getIdentifier("pbar", "id", getPackageName()));
    pbar.setVisibility(View.VISIBLE);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myWebView.canGoBack()) {
                myWebView.goBack();
            }
        }
    });
    forward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myWebView.canGoForward()) {
                myWebView.goForward();
            }
        }
    });
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Done", "Clicked");
            finish();
        }
    });
}

From source file:com.facebook.react.uimanager.BaseViewManager.java

@ReactProp(name = PROP_RENDER_TO_HARDWARE_TEXTURE)
public void setRenderToHardwareTexture(T view, boolean useHWTexture) {
    view.setLayerType(useHWTexture ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null);
}

From source file:pl.rmakowiecki.simplemusicplayer.ui.widget.MusicCoverView.java

public MusicCoverView(Context context, AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    // Canvas.clipPath works wrong when running with hardware acceleration on Android N
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }//ww w  .j a va 2s.  c o m

    final float density = getResources().getDisplayMetrics().density;
    mTrackSize = TRACK_SIZE * density;
    mTrackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTrackPaint.setStyle(Paint.Style.STROKE);
    mTrackPaint.setStrokeWidth(TRACK_WIDTH * density);

    mStartRotateAnimator = ObjectAnimator.ofFloat(this, View.ROTATION, 0, FULL_ANGLE);
    mStartRotateAnimator.setInterpolator(new LinearInterpolator());
    mStartRotateAnimator.setRepeatCount(Animation.INFINITE);
    mStartRotateAnimator.setDuration(DURATION);
    mStartRotateAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            float current = getRotation();
            float target = current > HALF_ANGLE ? FULL_ANGLE : 0; // Choose the shortest distance to 0 rotation
            float diff = target > 0 ? FULL_ANGLE - current : current;
            mEndRotateAnimator.setFloatValues(current, target);
            mEndRotateAnimator.setDuration((int) (DURATION_PER_DEGREES * diff));
            mEndRotateAnimator.start();
        }
    });

    mEndRotateAnimator = ObjectAnimator.ofFloat(MusicCoverView.this, View.ROTATION, 0);
    mEndRotateAnimator.setInterpolator(new LinearInterpolator());
    mEndRotateAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            setRotation(0);
            // isRunning method return true if it's called form here.
            // So we need call from post method to get the right returning.
            post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        mCallbacks.onRotateEnd(MusicCoverView.this);
                    }
                }
            });
        }
    });

    mRectToCircleTransition = new MorphTransition(SHAPE_RECTANGLE);
    mRectToCircleTransition.addTarget(this);
    mRectToCircleTransition.addListener(new TransitionAdapter() {
        @Override
        public void onTransitionStart(Transition transition) {
            mIsMorphing = true;
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            mIsMorphing = false;
            mShape = SHAPE_CIRCLE;
            if (mCallbacks != null) {
                mCallbacks.onMorphEnd(MusicCoverView.this);
            }
        }
    });

    mCircleToRectTransition = new MorphTransition(SHAPE_CIRCLE);
    mCircleToRectTransition.addTarget(this);
    mCircleToRectTransition.addListener(new TransitionAdapter() {
        @Override
        public void onTransitionStart(Transition transition) {
            mIsMorphing = true;
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            mIsMorphing = false;
            mShape = SHAPE_RECTANGLE;
            if (mCallbacks != null) {
                mCallbacks.onMorphEnd(MusicCoverView.this);
            }
        }
    });

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MusicCoverView);
    @Shape
    int shape = a.getInt(R.styleable.MusicCoverView_cover_shape, SHAPE_RECTANGLE);
    @ColorInt
    int trackColor = a.getColor(R.styleable.MusicCoverView_trackColor, TRACK_COLOR);
    a.recycle();

    setShape(shape);
    setTrackColor(trackColor);
    setScaleType();
}

From source file:com.android.madpausa.cardnotificationviewer.CardElementHolder.java

private void resetHolder() {
    cancel = false;//from  ww w .  ja va  2s . c o  m
    root.setTranslationX(0);
    root.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    root.setAlpha(1);
    root.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

From source file:org.mozilla.gecko.animation.PropertyAnimator.java

public void start() {
    if (mDuration == 0) {
        return;/*from   w w w.  ja va  2 s.c  om*/
    }

    mStartTime = AnimationUtils.currentAnimationTimeMillis();

    // Fix the from value based on current position and property
    for (ElementHolder element : mElementsList) {
        if (element.property == Property.ALPHA)
            element.from = element.proxy.getAlpha();
        else if (element.property == Property.TRANSLATION_Y)
            element.from = element.proxy.getTranslationY();
        else if (element.property == Property.TRANSLATION_X)
            element.from = element.proxy.getTranslationX();
        else if (element.property == Property.SCROLL_Y)
            element.from = element.proxy.getScrollY();
        else if (element.property == Property.SCROLL_X)
            element.from = element.proxy.getScrollX();
        else if (element.property == Property.WIDTH)
            element.from = element.proxy.getWidth();
        else if (element.property == Property.HEIGHT)
            element.from = element.proxy.getHeight();

        ViewCompat.setHasTransientState(element.view, true);

        if (shouldEnableHardwareLayer(element))
            element.view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        else
            element.view.setDrawingCacheEnabled(true);
    }

    // Get ViewTreeObserver from any of the participant views
    // in the animation.
    final ViewTreeObserver treeObserver;
    if (mElementsList.size() > 0) {
        treeObserver = mElementsList.get(0).view.getViewTreeObserver();
    } else {
        treeObserver = null;
    }

    final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (treeObserver.isAlive()) {
                treeObserver.removeOnPreDrawListener(this);
            }

            mFramePoster.postFirstAnimationFrame();
            return true;
        }
    };

    // Try to start animation after any on-going layout round
    // in the current view tree. OnPreDrawListener seems broken
    // on pre-Honeycomb devices, start animation immediatelly
    // in this case.
    if (Versions.feature11Plus && treeObserver != null && treeObserver.isAlive()) {
        treeObserver.addOnPreDrawListener(preDrawListener);
    } else {
        mFramePoster.postFirstAnimationFrame();
    }

    if (mListeners != null) {
        for (PropertyAnimationListener listener : mListeners) {
            listener.onPropertyAnimationStart();
        }
    }
}

From source file:com.ycdyng.onemulti.MultiFragment.java

@Override
public Animation onCreateAnimation(final int transit, final boolean enter, final int nextAnim) {
    if (nextAnim == 0) {
        return null;
    }/*from   www  .j  ava 2 s .  co  m*/
    //        if(enter) {
    //            if(nextAnim == StartAnimations[0]) {
    //                ViewCompat.setTranslationZ(getView(), 1.0f);
    //            } else {
    //                ViewCompat.setTranslationZ(getView(), 0.0f);
    //            }
    //        } else {
    //            if(nextAnim == BackAnimations[0]) {
    //                ViewCompat.setTranslationZ(getView(), 1.0f);
    //            } else {
    //                ViewCompat.setTranslationZ(getView(), 0.0f);
    //            }
    //        }
    Animation animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    animation.setAnimationListener(new Animation.AnimationListener() {

        private int mLayerType;

        @Override
        public void onAnimationStart(Animation animation) {
            if (getView() != null) {
                mLayerType = getView().getLayerType();
                getView().setLayerType(View.LAYER_TYPE_HARDWARE, null);
            }
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //                if(getView() != null) {
            //                    getView().setLayerType(mLayerType, null);
            //                    getView().post(new Runnable() {
            //                        @Override
            //                        public void run() {
            //                            if(getView() != null) {
            //                                ViewCompat.setTranslationZ(getView(), 0.0f);
            //                            }
            //                        }
            //                    });
            //                }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    // NOTE: the animation must be added to an animation set in order for the listener
    // to work on the exit animation
    AnimationSet animSet = new AnimationSet(true);
    animSet.addAnimation(animation);
    return animSet;
}

From source file:com.yahala.ui.Views.BaseFragment.java

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

    // HW layer support only exists on API 11+
    Animation animation = super.onCreateAnimation(transit, enter, nextAnim);

    // HW layer support only exists on API 11+
    if (Build.VERSION.SDK_INT >= 11) {
        if (animation == null && nextAnim != 0) {
            animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        }/*  w  w  w .j  ava  2  s .c o m*/

        if (animation != null) {
            getView().setLayerType(View.LAYER_TYPE_HARDWARE, null);

            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    BaseFragment.this.onAnimationStart();
                }

                public void onAnimationEnd(Animation animation) {
                    try {
                        getView().setLayerType(View.LAYER_TYPE_NONE, null);
                        BaseFragment.this.onAnimationEnd();
                    } catch (Exception e) {
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }

            });
        }
    }

    return animation;

    /*if (nextAnim != 0) {
    Animation animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    if (Build.VERSION.SDK_INT >= 11) {
        getView().setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }
    animation.setAnimationListener(new Animation.AnimationListener() {
            
        public void onAnimationStart(Animation animation) {
            BaseFragment.this.onAnimationStart();
        }
            
        public void onAnimationRepeat(Animation animation) {
            
        }
            
        public void onAnimationEnd(Animation animation){
            BaseFragment.this.onAnimationEnd();
            if (Build.VERSION.SDK_INT >= 11) {
                getView().setLayerType(View.LAYER_TYPE_NONE, null);
          }
        }
    });
            
    return animation;
    } else {
    return super.onCreateAnimation(transit, enter, nextAnim);
    }*/
}

From source file:com.andremion.music.MusicCoverView.java

public MusicCoverView(Context context, AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MusicCoverView);
    @Shape/*from   www . j  ava  2  s  .  co  m*/
    int shape = a.getInt(R.styleable.MusicCoverView_shape, SHAPE_SQUARE);
    @ColorInt
    int trackColor = a.getColor(R.styleable.MusicCoverView_trackColor, TRACK_COLOR);
    a.recycle();

    // TODO: Canvas.clipPath works wrong when running with hardware acceleration on Android N
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }

    final float density = getResources().getDisplayMetrics().density;
    mTrackSize = TRACK_SIZE * density;
    mTrackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTrackPaint.setStyle(Paint.Style.STROKE);
    mTrackPaint.setStrokeWidth(TRACK_WIDTH * density);

    mDiscPaintCenterDecor = new Paint(Paint.ANTI_ALIAS_FLAG);
    mDiscPaintCenterDecor.setStyle(Paint.Style.FILL);
    mDiscPaintCenter = new Paint(Paint.ANTI_ALIAS_FLAG);
    mDiscPaintCenter.setStyle(Paint.Style.FILL);

    setShape(shape);
    setTrackColor(trackColor);
    if (getDrawable() != null && ((BitmapDrawable) getDrawable()).getBitmap() != null) {
        setCenterColor(DISC_CENTER_COLOR, Palette.generate(((BitmapDrawable) getDrawable()).getBitmap(), 32)
                .getLightVibrantColor(DISC_CENTER_DECOR_COLOR));
    } else {
        setCenterColor(DISC_CENTER_COLOR, DISC_CENTER_DECOR_COLOR);
    }
    setScaleType();

    mStartRotateAnimator = ObjectAnimator.ofFloat(this, View.ROTATION, 0, FULL_ANGLE);
    mStartRotateAnimator.setInterpolator(new LinearInterpolator());
    if (SHAPE_SQUARE == mShape) {
        mStartRotateAnimator.setDuration(DURATION_SQUARE);
    } else {
        mStartRotateAnimator.setDuration(DURATION);
        mStartRotateAnimator.setRepeatCount(Animation.INFINITE);
    }

    mStartRotateAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            float current = getRotation();
            float target = current > HALF_ANGLE ? FULL_ANGLE : 0; // Choose the shortest distance to 0 rotation
            float diff = target > 0 ? FULL_ANGLE - current : current;
            mEndRotateAnimator.setFloatValues(current, target);
            if (SHAPE_SQUARE == mShape) {
                mEndRotateAnimator.setDuration((int) (DURATION_SQUARE_PER_DEGREES * diff));
            } else {
                mEndRotateAnimator.setDuration((int) (DURATION_PER_DEGREES * diff));
            }

            mEndRotateAnimator.start();
        }
    });

    mEndRotateAnimator = ObjectAnimator.ofFloat(MusicCoverView.this, View.ROTATION, 0);
    mEndRotateAnimator.setInterpolator(new LinearInterpolator());
    mEndRotateAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            setRotation(0);
            // isRunning method return true if it's called form here.
            // So we need call from post method to get the right returning.
            post(new Runnable() {
                @Override
                public void run() {
                    if (mCallbacks != null) {
                        mCallbacks.onRotateEnd(MusicCoverView.this);
                    }
                }
            });
        }
    });

    mRectToCircleTransition = new MorphTransition(SHAPE_RECTANGLE);
    mRectToCircleTransition.addTarget(this);
    mRectToCircleTransition.addListener(new TransitionAdapter() {
        @Override
        public void onTransitionStart(Transition transition) {
            mIsMorphing = true;
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            mIsMorphing = false;
            mShape = SHAPE_CIRCLE;
            if (mCallbacks != null) {
                mCallbacks.onMorphEnd(MusicCoverView.this);
            }
        }
    });

    mCircleToRectTransition = new MorphTransition(SHAPE_CIRCLE);
    mCircleToRectTransition.addTarget(this);
    mCircleToRectTransition.addListener(new TransitionAdapter() {
        @Override
        public void onTransitionStart(Transition transition) {
            mIsMorphing = true;
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            mIsMorphing = false;
            mShape = SHAPE_RECTANGLE;
            if (mCallbacks != null) {
                mCallbacks.onMorphEnd(MusicCoverView.this);
            }
        }
    });

    mSquareToSquareTransition = new MorphTransition(SHAPE_SQUARE);
    mSquareToSquareTransition.addTarget(this);
    mSquareToSquareTransition.addListener(new TransitionAdapter() {
        @Override
        public void onTransitionStart(Transition transition) {
            mIsMorphing = true;
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            mIsMorphing = false;
            mShape = SHAPE_SQUARE;
            if (mCallbacks != null) {
                mCallbacks.onMorphEnd(MusicCoverView.this);
            }
        }
    });
}