package com.xunao.effectdemo.view;

import android.content.ClipData;
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.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.xunao.effectdemo.R;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/514:02
 * desc :
 * version: 1.0
 */
public class MyLinearLayout extends LinearLayout {
	private String left_text;
	private TextView tv;
	private int bgColor;
	private LinearLayout ll_bg;
	private View view;
	private LinearLayout ll_content;
//	private boolean viewIs

	public MyLinearLayout(Context context) {
		super(context);
	}

	public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		init(context,attrs);
	}

	public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	private void init(Context context, @Nullable AttributeSet attrs) {
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyLinearLayout);
		left_text = typedArray.getString(R.styleable.MyLinearLayout_text_num);
		bgColor = typedArray.getResourceId(R.styleable.MyLinearLayout_bg_color,0);
		typedArray.recycle();
		LayoutInflater.from(context).inflate(R.layout.view_linear, this, true);
		tv=findViewById(R.id.tv);
		ll_bg=findViewById(R.id.ll_bg);
		ll_bg.setBackgroundResource(bgColor);
		ll_content = findViewById(R.id.ll_content);
		tv.setText(left_text);
		view = findViewById(R.id.view);
		ll_content.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				ClipData clipData = new ClipData(ClipData.newPlainText("text", "aaa"));
				//定义一个拖拽阴影
				View.DragShadowBuilder myShadow = new View.DragShadowBuilder(view);

				//开始拖拽. 第三个参数在拖拽过程用来传递数据,这里我传view即被拖动的view自己,
				//后面可在DragEvent中获得这个参数view
				view.startDrag(clipData, myShadow, view, 0);
				view.setVisibility(View.GONE);
				return false;
			}
		});
	}

	public void setViewGone(){
		view.setVisibility(GONE);
	}
	public void setViewVisible(){
		view.setVisibility(VISIBLE);
	}

	public void setViewByAnim(){
		Animation animUp= new TranslateAnimation(0, 0, 0, -20);
		animUp.setDuration(0);
		animUp.setRepeatCount(0);//动画的反复次数
		animUp.setFillAfter(true);//设置为true，动画转化结束后被应用
		animUp.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				view.setVisibility(GONE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}
		});
		ll_bg.startAnimation(animUp);
	}

}
