Android 补间动画 --透明度 alpha

1、现象

在这里插入图片描述

2、文件结构

在这里插入图片描述

3、alpha.xml 定义动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="1.1" android:toAlpha="0.1"
        android:duration="5000"/>
</set>


<!--
fromAlpha: 表示动画起始的透明度, 0.0 表示完全透明,1.0 表示完全不透明 取值在 0.0 ~ 1.0之间的 float 数据类型的数字
toAlpha: 表示动画结束的透明度,值定义同上
duration:动画持续时间,单位毫秒
-->

4、activity_main.xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/test_2"
        android:id="@+id/image_one_id" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/image_two_id"
        android:layout_marginTop="30dp"
        android:background="@drawable/test_3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Aplha"
        android:onClick="onClick"/>

</LinearLayout>

5、MainActivity.java 功能文件

package myapplication.lum.com.myanimation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


     private String TAG = "LUM: ";
     private ImageView imageViewOne,imageViewTwo;

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

        imageViewOne = (ImageView) findViewById(R.id.image_one_id);
        imageViewTwo = (ImageView) findViewById(R.id.image_two_id);

    }

    public void onClick(View view) {
        //加载 xml  文件实现 动画
        Animation alphaAnimation = AnimationUtils.loadAnimation(this,R.anim.alpha);
        imageViewOne.startAnimation(alphaAnimation);


        //代码动态实现 改变 alpha
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(1.0f,0.1f);
        alphaAnimation1.setDuration(5000);
        animationSet.addAnimation(alphaAnimation1);
        imageViewTwo.startAnimation(animationSet);
    }
}

发布了354 篇原创文章 · 获赞 114 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/99869085