android 隐藏展开功能,95.android 简单的布局隐藏与展开效果的属性动画(两种方式)...
//效果图://第一种方式(纯平移动画类型)://第一步 我的Activity布局://第二步 我的Activity代码实现:public class MainActivity extends AppCompatActivity implements View.OnClickListener {private LinearLayout mData;private TextView mShowH..
//效果图:
//第一种方式(纯平移动画类型):
//第一步 我的Activity布局:
//第二步 我的Activity代码实现:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout mData;
private TextView mShowHidden;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mData = (LinearLayout) findViewById(R.id.mData);
mShowHidden = (TextView) findViewById(R.id.mShowHidden);
mShowHidden.setOnClickListener(this);
//获取组件高度
initOnPreDrawListener();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mShowHidden:
if (mData.getVisibility() == View.GONE) {
start();
} else {
end();
}
break;
}
}
/*
* 方法名:initOnPreDrawListener()
* 功 能:初始化viewTreeObserver事件监听,重写OnPreDrawListener获取组件高度
* 参 数:无
* 返回值:无
*/
private void initOnPreDrawListener() {
final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
height = mData.getMeasuredHeight();
// int width = mText1.getMeasuredWidth();
Log.e("TAG", "高:" + height);
// 移除OnPreDrawListener事件监听
MainActivity.this.getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);
//获取完高度后隐藏控件
mData.setVisibility(View.GONE);
return true;
}
});
}
/*
* 方法名:start()
* 功 能:显示控件,开启动画,判断,控件高度只获取一次
* 参 数:无
* 返回值:无
*/
private void start() {
// 显示控件
mData.setVisibility(View.VISIBLE);
//开启平移动画
TranslateAnimation startTranslateAnim = new TranslateAnimation(0, 0, -height, 0);
startTranslateAnim.setDuration(1600);
Log.e("TAG2", "高:" + height);
//控件开始动画
mData.startAnimation(startTranslateAnim);
mShowHidden.startAnimation(startTranslateAnim);
//开启动画的监听
startTranslateAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始调用
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束时调用
//控件右边重新设置图片,相当于设置drawableRight
Drawable drawable = getResources().getDrawable(R.mipmap.bb);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
mShowHidden.setCompoundDrawables(null, null, drawable, null);//画在右边
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重复时调用
}
});
}
/*
* 方法名:end()
* 功 能:关闭平移动画,控件开始执行关闭动画,动画的监听
* 参 数:无
* 返回值:无
*/
private void end() {
// 关闭平移动画
TranslateAnimation endTranslateAnim = new TranslateAnimation(0, 0, 0, -height);
endTranslateAnim.setDuration(1600);
Log.e("TAG2", "高:" + height);
//控件开始动画
mData.startAnimation(endTranslateAnim);
mShowHidden.startAnimation(endTranslateAnim);
//关闭动画的监听
endTranslateAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始调用
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束时调用
mData.setVisibility(View.GONE);
Drawable drawable = getResources().getDrawable(R.mipmap.aa);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
mShowHidden.setCompoundDrawables(null, null, drawable, null);//画在右边
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重复时调用
}
});
}
}
//第二种 方式(加了ValueAnimator来设置布局移动):
//第一步 Activity布局和第一个是一样的。
//第二步 Activity代码实现:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout mData;
private TextView mShowHidden;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mData = (LinearLayout) findViewById(R.id.mData);
mShowHidden = (TextView) findViewById(R.id.mShowHidden);
mShowHidden.setOnClickListener(this);
//获取控件高度
initOnPreDrawListener();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mShowHidden:
if (mData.getVisibility() == View.GONE) {
//开启布局动画
animateOpen(mData);
//开启平移动画
startTranslationAnimation();
} else {
//关闭布局动画
animateClose(mData);
//关闭平移动画
endTranslationAnimation();
}
break;
}
}
/*
* 方法名:initOnPreDrawListener()
* 功 能:获取控件高度
* 参 数:无
* 返回值:无
*/
private void initOnPreDrawListener() {
final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
height = mData.getMeasuredHeight();
// int width = mText1.getMeasuredWidth();
Log.e("TAG", "高:" + height);
// 移除OnPreDrawListener事件监听
MainActivity.this.getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);
//获取完高度后隐藏控件
mData.setVisibility(View.GONE);
return true;
}
});
}
/*
* 方法名:animateOpen(View v)
* 功 能:布局向下移动
* 参 数:View v
* 返回值:无
*/
private void animateOpen(View v) {
v.setVisibility(View.VISIBLE);
ValueAnimator animator = createDropAnimator(v, 0, height);
animator.start();
}
/*
* 方法名:animateClose(View v)
* 功 能:布局向上移动
* 参 数:View v
* 返回值:无
*/
private void animateClose(final View view) {
int origHeight = view.getHeight();
ValueAnimator animator = createDropAnimator(view, origHeight, 0);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
animator.start();
}
/*
* 方法名:startTranslationAnimation()
* 功 能:开启平移动画
* 参 数:无
* 返回值:无
*/
private void startTranslationAnimation() {
TranslateAnimation startTranslateAnim = new TranslateAnimation(0, 0, -height, 0);
startTranslateAnim.setDuration(300);
mData.startAnimation(startTranslateAnim);
}
/*
* 方法名:endTranslationAnimation()
* 功 能:关闭平移动画
* 参 数:无
* 返回值:无
*/
private void endTranslationAnimation() {
TranslateAnimation endTranslateAnim= new TranslateAnimation(0, 0, 0, -height);
endTranslateAnim.setDuration(300);
mData.startAnimation(endTranslateAnim);
}
/*
* 方法名:createDropAnimator(final View v, int start, int end)
* 功 能:ValueAnimator动画,布局上下移动
* 参 数:View v, int start, int end
* 返回值:ValueAnimator animator
*/
private ValueAnimator createDropAnimator(final View v, int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator arg0) {
int value = (int) arg0.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.height = value;
v.setLayoutParams(layoutParams);
}
});
return animator;
}
}
//-------------------------------------------------------------------------完-------------------------------------------------------------------------------------
更多推荐
所有评论(0)