<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vbtn1"
android:text="位移动画"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移后的按钮"
android:id="@+id/vbtn2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vbtn3"
android:text="拉伸"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vbtn5"
android:text="弹性"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vbtn4"
android:text="消失"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vimg"
android:src="@mipmap/ic_launcher"
/>
private Button mbtn;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_values);
img = findViewById(R.id.vimg);
mbtn = findViewById(R.id.vbtn2);
mbtn.setOnClickListener(this);
findViewById(R.id.vbtn1).setOnClickListener(this);
findViewById(R.id.vbtn3).setOnClickListener(this);
findViewById(R.id.vbtn4).setOnClickListener(this);
findViewById(R.id.vbtn5).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.vbtn1:
TranslateAnimation t = new TranslateAnimation(0, 300, 0, 0);
t.setDuration(1000);
t.setFillAfter(true);
t.setInterpolator(new LinearInterpolator());
t.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mbtn.startAnimation(t);
break;
case R.id.vbtn2:
Log.i("TAG","按钮被点击");
break;
case R.id.vbtn3:
valueAnim();
break;
case R.id.vbtn4:
objectAnim();
break;
case R.id.vbtn5:
bounce();
break;
}
}
private void bounce() {
ValueAnimator va = ValueAnimator.ofInt(0, 400);
va.setDuration(3000);
va.setInterpolator(new BounceInterpolator());
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int margin = (int) animation.getAnimatedValue();
Log.i("TAG","onAnimationUpdate:"+margin);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) img.getLayoutParams();
lp.topMargin = margin;
img.setLayoutParams(lp);
}
});
va.start();
}
private void objectAnim() {
ObjectAnimator oa = ObjectAnimator.ofFloat(mbtn, "alpha", 0.1f, 1.0f);
oa.setDuration(1000);
oa.setInterpolator(new LinearInterpolator());
oa.start();
}
private void valueAnim() {
ValueAnimator va = ValueAnimator.ofInt(mbtn.getWidth(), mbtn.getWidth() * 2);
va.setDuration(1000);
va.setInterpolator(new LinearInterpolator());
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int width = (int) animation.getAnimatedValue();
Log.i("TAG","onAnimationUpdate->"+width);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mbtn.getLayoutParams();
lp.width = width;
mbtn.setLayoutParams(lp);
}
});
va.start();
}