使用XML给图片添加动画

在res包下新建anim包

//消失
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="1"
    android:toAlpha="0.1"
    android:interpolator="@android:anim/linear_interpolator"
    >

</alpha>
//拉伸
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:pivotY="50%"
    android:pivotX="50%"
    android:fromXScale="1"
    android:toXScale="2"
    android:fromYScale="1"
    android:toYScale="1"
    >


</scale>
//旋转
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:anim/linear_interpolator"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:fillAfter="true"
    android:pivotX="50%"
    android:pivotY="50%"
    >

</rotate>
```xml
//组合动画
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="@android:anim/linear_interpolator"
    android:duration="1000"
    android:repeatMode="restart"
    >

    <translate
        android:fromXDelta="0"
        android:toXDelta="300"
        android:fromYDelta="0"
        android:toYDelta="300"
        />
    <rotate
        android:pivotY="50%"
        android:pivotX="50%"
        android:fromDegrees="0"
        android:toDegrees="360"
        />
</set>

```xml
//平移
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0"
    android:toXDelta="100%"
    android:fromYDelta="0"
    android:toYDelta="100%"
    android:interpolator="@android:anim/linear_interpolator"
    >

</translate>
//布局文件
<Button
    android:text="xml位移动画"
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="xml旋转动画"
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="xml渐变动画"
    android:id="@+id/btn3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="xml缩放动画"
    android:id="@+id/btn4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="位移动画"
    android:id="@+id/btn5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="旋转动画"
    android:id="@+id/btn6"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="渐变动画"
    android:id="@+id/btn7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="缩放动画"
    android:id="@+id/btn8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="animation_set"
    android:id="@+id/btn9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp">
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp" />
</RelativeLayout>

private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tween);
    init();
}

private void init() {
    img = findViewById(R.id.img);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn1:
            //分类筛选下拉列表过度使用
            TranslateAnimation t1 = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.t_anim);
            img.startAnimation(t1);
            break;
        case R.id.btn2:
            //下拉刷新旋转箭头动画
            RotateAnimation r1 = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.r_anim);
            img.startAnimation(r1);
            break;
        case R.id.btn3:
            AlphaAnimation a1 = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.a_anim);
            img.startAnimation(a1);
            break;
        case R.id.btn4:
            //滑动隐藏,停止显示过度效果
            ScaleAnimation s1 = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.s_anim);
            img.startAnimation(s1);
            break;
        case R.id.btn5:
            //java位移动画
            TranslateAnimation t2 = new TranslateAnimation(0,img.getWidth(),0,img.getHeight());
            t2.setDuration(1000);
            t2.setFillAfter(true);
            t2.setInterpolator(new LinearInterpolator());
            t2.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    Log.i("TAG","onAnimationStart");
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    Log.i("TAG","onAnimationEnd");
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    Log.i("TAG","onAnimationRepeat");
                }
            });
            img.startAnimation(t2);
            break;
        case R.id.btn6:
            //旋转
            RotateAnimation r2 = new RotateAnimation(0, 180, 0, 0);
            r2.setDuration(1000);
            r2.setInterpolator(new LinearInterpolator());
            img.startAnimation(r2);
            break;
        case R.id.btn7:
            //淡化
            AlphaAnimation a2 = new AlphaAnimation(1.0f, 0.5f);
            a2.setDuration(1000);
            a2.setInterpolator(new LinearInterpolator());
            img.startAnimation(a2);
            break;
        case R.id.btn8:
            //缩放
            ScaleAnimation s2 = new ScaleAnimation(1, 3, 1, 1);
            s2.setDuration(1000);
            s2.setInterpolator(new LinearInterpolator());
            img.startAnimation(s2);
            break;
        case R.id.btn9:
            Animation set = AnimationUtils.loadAnimation(this, R.anim.set_anim);
            img.startAnimation(set);
            break;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值