您的位置:首页 > 其它

以动画的方式实现View的展开和缩放

2016-03-12 17:42 393 查看
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">Activity代码如下</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">
</span></span>


<span style="font-size:18px;">import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
private LinearLayout contentPanel;
private Button expand_btn;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentPanel= (LinearLayout) findViewById(R.id.contentPanel);
expand_btn= (Button) findViewById(R.id.expand_btn);
contentPanel.measure(0, 0);
height=contentPanel.getMeasuredHeight();

}
public void to(View v){
if(contentPanel.isShown()){
contraction();
}else{
expand();
}
}

/**
* 收缩
*/
private void contraction(){
AnimatorSet set=new AnimatorSet();
//根据值的改变,动态设置view的高度
ValueAnimator valueAnimator=ValueAnimator.ofInt(height,0);
valueAnimator.setDuration(1000);
valueAnimator.setInterpolator(new AccelerateInterpolator());

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int) animation.getAnimatedValue();
Log.e("height", height + "");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
contentPanel.setLayoutParams(params);
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
contentPanel.setVisibility(View.GONE);
expand_btn.setText("展开");
}
});
//将view从不透明变成透明
ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",1f,0f);
alpha.setDuration(1000);

set.play(valueAnimator).with(alpha);
set.start();
}

/**
* 展开
*/
private void expand(){
contentPanel.setVisibility(View.VISIBLE);

AnimatorSet set=new AnimatorSet();

ValueAnimator valueAnimator=ValueAnimator.ofInt(0,height);
valueAnimator.setDuration(1000);
valueAnimator.setInterpolator(new AccelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int) animation.getAnimatedValue();
Log.e("height", height + "");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
contentPanel.setLayoutParams(params);
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
expand_btn.setText("收起");
}
});
//将view从透明变成不透明
ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",0f,1f);
alpha.setDuration(1000);

set.play(valueAnimator).with(alpha);
set.start();
}
}
</span></span>

XML代码:

<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span></span>
<span style="font-size:18px;">    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/expand_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="to"
android:text="展开"/>
<LinearLayout
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
</span></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息