zoukankan      html  css  js  c++  java
  • 补间动画的实现步骤

    1.在res目录下建立文件夹anim

    2.在anim文件夹下建立animation.xml动画文件

    3.在animation.xml文件中描述某种动画的属性

    4.在代码中利用AnimationUtils.loadAnimation方法加载动画文件

    5.在代码中用View的startAnimation方法启动动画

    渐变动画可以演示一个视图在透明度上的渐变效果。它的主要属性有:

    fromAlpha动画起始时透明度,值取在0.0-1.0之间

    toAlpha   动画结束时透明度,值取在0.0-1.0之间

    duration  动画持续时间,以毫秒为单位

    repeatCount动画重复次数,这个重复次数不包括第一次播放

    MainActivity.java

    package com.example.deligence.demo5;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
    
        ImageView view;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            view = (ImageView) findViewById(R.id.img);
        }
    
        public void btnClickXML(View v){
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
            view.clearAnimation();
            view.startAnimation(animation);
        }
    }
    
    acitivity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="com.example.deligence.demo5.MainActivity">
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img_bird"
            android:layout_centerInParent="true"
            />
        <Button
            android:text="播放动画XML"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:onClick="btnClickXML"
            />
    
    </RelativeLayout>
    

    res/anim/alpha.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatCount="3"
        />
    
    </set>
    
    
    通过Java代码实现
    ImageView view = (ImageView) findViewById(R.id.img);
    
    
    Animation animation;
    //设置自定义动画
    animation = new AlphaAnimation(0.1f,1.0f); //是定透明度起点和终点
    animation.setDuration(2000); //设置动画时长
    animation.setRepeatCount(3); //设置重读次数
    
    
    view.clearAnimation(); //清除动画
    view.startAnimation(animation); //启动动画
    
    



  • 相关阅读:
    switch statement
    Block statement
    Variable hoisting Global variables Constants
    How the Web works
    Discover browser developer tools
    Publishing via GitHub
    第十二节,OpenCV学习(一)图像的读取、显示、保存
    剑指Offer编程题3——从尾到头打印链表
    剑指Offer编程题2——替换空格
    剑指Offer编程题1——二维数组中的查找
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11782061.html
Copyright © 2011-2022 走看看