اضافة تاثير دوران ثلاثي الأبعاد إلي listview في سكيتشوير

 


الكود المستخدم 

- اسم البلوكة الاولي


extra{codeBox}


- الكود المستخدم

public static class Rotate3dAnimation extends Animation {

    private final float mFromDegrees;

    private final float mToDegrees;

    private final float mCenterX;

    private final float mCenterY;

    private final float mDepthZ;

    private final boolean mReverse;

    private Camera mCamera;

    float scale = 1;

    public Rotate3dAnimation(Context context, float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) {

        mFromDegrees = fromDegrees;

        mToDegrees = toDegrees;

        mCenterX = centerX;

        mCenterY = centerY;

        mDepthZ = depthZ;

        mReverse = reverse;

        scale = context.getResources().getDisplayMetrics().density;

    }

    @Override

    public void initialize(int width, int height, int parentWidth, int parentHeight) {

        super.initialize(width, height, parentWidth, parentHeight);

        mCamera = new Camera();

    }

    @Override

    protected void applyTransformation(float interpolatedTime, Transformation t) {

        final float fromDegrees = mFromDegrees;

        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

        final float centerX = mCenterX;

        final float centerY = mCenterY;

        final Camera camera = mCamera;

        final Matrix matrix = t.getMatrix();

        camera.save();

        if (mReverse) {

            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);

        } else {

            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));

        }

        camera.rotateY(degrees);

        camera.getMatrix(matrix);

        camera.restore();

        float[] mValues = new float[9];

        matrix.getValues(mValues);

        mValues[6] = mValues[6] / scale;

        mValues[7] = mValues[7] / scale;

        matrix.setValues(mValues);

        matrix.preTranslate(-centerX, -centerY);

        matrix.postTranslate(centerX, centerY);

    }

}



public static class Rotate3D {

    private Context context;

    private View parentView;

    private View positiveView;

    private View negativeView;

    private int centerX;

    private int centerY;

    private int depthZ;

    private int duration;

    private Rotate3dAnimation openAnimation;

    private Rotate3dAnimation closeAnimation;

    private boolean isOpen = false;

    private Rotate3D(Builder builder) {

        this.context = builder.context;

        this.parentView = builder.parentView;

        this.positiveView = builder.positiveView;

        this.negativeView = builder.negativeView;

        this.duration = builder.duration;

        this.depthZ = builder.depthZ;

    }

    public View getParentView() {

        return parentView;

    }

    public View getPositiveView() {

        return positiveView;

    }

    public View getNegativeView() {

        return negativeView;

    }

    public void transform() {

        centerX = parentView.getWidth() / 2;

        centerY = parentView.getHeight() / 2;

        if (openAnimation == null) {

            initOpenAnim();

            initCloseAnim();

        }

        if (openAnimation.hasStarted() && !openAnimation.hasEnded()) {

            return;

        }

        if (closeAnimation.hasStarted() && !closeAnimation.hasEnded()) {

            return;

        }

        parentView.startAnimation(isOpen ? closeAnimation : openAnimation);


        isOpen = !isOpen;

    }

    public boolean isOpen() {

        return isOpen;

    }

    private void initOpenAnim() {

        openAnimation = new Rotate3dAnimation(context, 0, 90, centerX, centerY, depthZ, true);

        openAnimation.setDuration(duration);

        openAnimation.setFillAfter(true);

        openAnimation.setInterpolator(new AccelerateInterpolator());

        openAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override

            public void onAnimationStart(Animation animation) {

            }

            @Override

            public void onAnimationRepeat(Animation animation) {

            }

            @Override

            public void onAnimationEnd(Animation animation) {

                positiveView.setVisibility(View.GONE);

                negativeView.setVisibility(View.VISIBLE);

                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(context, 270, 360, centerX, centerY, depthZ, false);

                rotateAnimation.setDuration(duration);

                rotateAnimation.setFillAfter(true);

                rotateAnimation.setInterpolator(new DecelerateInterpolator());

                parentView.startAnimation(rotateAnimation);

            }

        });

    }

    private void initCloseAnim() {

        closeAnimation = new Rotate3dAnimation(context, 360, 270, centerX, centerY, depthZ, true);

        closeAnimation.setDuration(duration);

        closeAnimation.setFillAfter(true);

        closeAnimation.setInterpolator(new AccelerateInterpolator());

        closeAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override

            public void onAnimationStart(Animation animation) {

            }

            @Override

            public void onAnimationRepeat(Animation animation) {

            }

            @Override

            public void onAnimationEnd(Animation animation) {

                positiveView.setVisibility(View.VISIBLE);

                negativeView.setVisibility(View.GONE);

                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(context, 90, 0, centerX, centerY, depthZ, false);

                rotateAnimation.setDuration(duration);

                rotateAnimation.setFillAfter(true);

                rotateAnimation.setInterpolator(new DecelerateInterpolator());

                parentView.startAnimation(rotateAnimation);

            }

        });

    }

    public static class Builder {

        private int depthZ = 400;

        private int duration = 400;

        private Context context;

        private View parentView;

        private View positiveView;

        private View negativeView;

        public Builder(Context context) {

            this.context = context;

        }

        public Builder setDepthZ(int depthZ) {

            this.depthZ = depthZ;

            return this;

        }

        public Builder bindParentView(View parentView) {

            this.parentView = parentView;

            return this;

        }

        public Builder bindPositiveView(View positiveView) {

            this.positiveView = positiveView;

            return this;

        }

        public Builder bindNegativeView(View negativeView) {

            this.negativeView = negativeView;

            return this;

        }

        public Builder setDuration(int duration) {

            this.duration = duration;

            return this;

        }

        public Rotate3D create() {

            Rotate3D rotate3D = new Rotate3D(this);

            if (rotate3D.getParentView() == null) {

                throw new NullPointerException("Please set ParentView");

            }

            if (rotate3D.getPositiveView() == null) {

                throw new NullPointerException("Please set PositiveView");

            }

            if (rotate3D.getNegativeView() == null) {

                throw new NullPointerException("Please set NegativeView");

            }

            return rotate3D;

        }

    }

}{codeBox}


- اسم البلوكة الثانيه


Rotate3D{codeBox}


- الكود المستخدم 


final Rotate3D anim;

anim = new Rotate3D.Builder(this)

.bindParentView(_view)

.bindPositiveView(_view)

.bindNegativeView(_view)

.create();

anim.transform();{codeBox}




{getButton} $text={نسخ الكود من التليجرام} $icon={preview} $color={#4061FF}

 

 

 

 



إرسال تعليق

اكتب تعليق

أحدث أقدم