给按钮添加动画效果

<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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值