Android 使用xml的方式定义补间动画

public class MainActivity extends AppCompatActivity {

    private ImageView iv;

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

        //这个控件用来执行动画
        iv = (ImageView) findViewById(R.id.iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "你点不到我", Toast.LENGTH_SHORT).show();
            }
        });
    }
    // 点击按钮 实现透明效果
    public void click1(View v) {
        // 创建透明动画 1.0意味着完全不透明 0.0意味着完全透明
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
        // 开始执行动画
        iv.startAnimation(animation);
    }

    // 点击按钮 实现旋转效果
    public void click2(View v) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
        // 开始执行动画
        iv.startAnimation(animation);
    }

    // 点击按钮 实现缩放效果
    public void click3(View v) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
        // 开始执行动画
        iv.startAnimation(animation);
    }

    // 点击按钮 实现平移效果
    public void click4(View v) {
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
        // 开始执行动画
        iv.startAnimation(animation);
    }

    // 点击按钮 让动画一起执行
    public void click5(View v) {
        // 创建动画的合集
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);
        // 执行动画
        iv.startAnimation(animation);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="2000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?>
<rotate
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:repeatMode="reverse"
    android:repeatCount="1"
    android:duration="2000"
    xmlns:android="http://schemas.android.com/apk/res/android"/>
<?xml version="1.0" encoding="utf-8"?>
<scale
    android:fromXScale="1.0"
    android:toXScale="2.0"
    android:fromYScale="1.0"
    android:toYScale="2.0"

    android:pivotX="50%"
    android:pivotY="50%"

    android:repeatCount="1"
    android:repeatMode="reverse"
    android:duration="2000"
    xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?>
<translate
    android:fromXDelta="0%p"
    android:toXDelta="0%p"
    android:fromYDelta="0%p"
    android:toYDelta="20%p"
    android:fillAfter="true"
    android:duration="2000"
    xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?>
<set>

    <alpha
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromAlpha="1.0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="0.0" >
    </alpha>

    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" >
    </rotate>

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fillAfter="true"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="0%p"
        android:toYDelta="20%p" >
    </translate>

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXScale="2.0"
        android:toYScale="2.0" >
    </scale>

</set>
在res目录下新建anim文件夹,在文件夹内新建alpha.xml等等。。










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值