欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

自定义伸缩 控件 带动画结束后 和开始前的回调

程序员文章站 2022-11-05 09:52:18
import android.animation.Animator;import android.animation.IntEvaluator;import android.animation.ValueAnimator;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;....
import android.animation.Animator;
import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.tongfantravel.passenger.R;


/**
 * Created by Administrator on 2016/11/15.
 */
public class StretchView extends LinearLayout {


    int contentViewId;
    int stretchViewId;
    int dutation = 300;
    private View contentView;
    private View stretchView;
    private boolean isOpen = false;
    private int stretchViewHeight;

    public void setOnStretchListener(OnStretchListener onStretchListener) {
        this.onStretchListener = onStretchListener;
    }

    private OnStretchListener onStretchListener;
    public StretchView(Context context) {
        super(context);
        init(context, null);
    }

    public StretchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public StretchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setOrientation(LinearLayout.VERTICAL);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StretchPanel);
        contentViewId = a.getResourceId(R.styleable.StretchPanel_contentView, -1);
        stretchViewId = a.getResourceId(R.styleable.StretchPanel_stretchView, -1);
        if (contentViewId != -1) {
            contentView = LayoutInflater.from(context).inflate(contentViewId, this, false);
            setContentView(contentView);
        }
        if (stretchViewId != -1) {
            stretchView = LayoutInflater.from(context).inflate(stretchViewId, this, false);
            setStretchView(stretchView);
        }
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (stretchViewHeight == 0) {
            stretchView.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            stretchViewHeight = stretchView.getMeasuredHeight();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        closeStrtchView();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
// closeStrtchView();
        stretchView.setFocusable(true);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    public void setContentView(View mContentView) {
        if (contentView != null) {
            removeView(contentView);
        }
        this.contentView = mContentView;
        addView(mContentView);
        setOpenClickListenerOnThis(contentView);
    }

    public void setStretchView(View mStretchView) {
        if (stretchView != null)
            removeView(stretchView);

        this.stretchView = mStretchView;
        addView(mStretchView);
    }

    public interface OnStretchListener {
        void openStretchBefore();

        void closeStretchAfter();
    }

    public void setOpenClickListenerOnThis(View openClickListenerOnThis) {
// this.openClickListenerOnThis = openClickListenerOnThis;

        if (openClickListenerOnThis != null) {
            openClickListenerOnThis.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!isOpen) {
                        openStretchView();
                    } else {
                        closeStrtchView();
                    }
                    isOpen = !isOpen;
                }
            });
        }
    }

    private void closeStrtchView() {

        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            IntEvaluator intEvaluator = new IntEvaluator();

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedFraction = animation.getAnimatedFraction();
                stretchView.getLayoutParams().height = intEvaluator.evaluate(animatedFraction, stretchViewHeight, 0);
                stretchView.requestLayout();
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
               onStretchListener.closeStretchAfter();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.setDuration(dutation).start();
    }

    private void openStretchView() {
        onStretchListener.openStretchBefore();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            IntEvaluator intEvaluator = new IntEvaluator();

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedFraction = animation.getAnimatedFraction();
                stretchView.getLayoutParams().height = intEvaluator.evaluate(animatedFraction, 0, stretchViewHeight);
                stretchView.requestLayout();
            }
        });
        valueAnimator.setDuration(dutation).start();
    }

    public void setCloseStretchView() {
        closeStrtchView();
    }
}

本文地址:https://blog.csdn.net/Victor____asd/article/details/107492529