Tween 动画之AlphaAniamtion(透明度动画

本文介绍了Android中使用AlphaAnimation实现透明度动画的方法,包括构造方法、常用参数和方法。通过示例展示了如何创建从完全透明到完全不透明的动画,并提供了XML布局和Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tween动画的AlphaAnimation动画 :是通过改变View的透明度来实现动画效果的

      常用的构造方法如下:

             AlphaAimation(float fromAlpha  , float toAlpha )   

              【参数说明】:

               fromAlpha : 动画开始的透明度  一般是0.0f  最小的透明度

              toAlpha :动画结束的透明度  可以使0.0f~1.0f           1.0f为最大的透明度了

             【常用方法】

              1: setDuration(4000) *******  设置动画持续的时间    时间为毫秒   4000为4秒

               2:startNow()   ************动画立即开始

              3: cancel ()  **********动画立即结束

           好了 废话不说了 马上上代码吧

       XML文件

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       
       android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"
        android:onClick="start"
        android:background="@drawable/button"
        android:id="@+id/button1"/>
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop"
        android:onClick="stop"
        android:background="@drawable/button"
        android:id="@+id/button2"/>
     </LinearLayout>
     <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/ball"/>
            
</LinearLayout>      
         

上面就定义了两个Button ,一个用于开始动画 、一个用于停止动画、还有一个ImageView、动画的实行

Java代码

 package com.android.my4;


import android.app.Activity;


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity implements OnClickListener{
   /**

   *透明度动画

    /*

    private ImageView image;
    private Button start;   //声明所用到的View
    private Button stop;
    private AlphaAnimation alpha;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView)findViewById(R.id.image);  // 通过Id获取组件
        start = (Button)findViewById(R.id.button1);
        stop = (Button)findViewById(R.id.button2);
        alpha = new AlphaAnimation(0.0f, 1.0f);   //  设置透明度动画的配置  从透明度0.0f到1.0f    
        
    }

        //定义单击start按钮的监听器  并定义实现动画开始方法
public void start(View e){
alpha.setDuration(5000);     //设置动画的持续时间为5秒
start.startAnimation(alpha);  //  开始动画

}
public void stop(View e){   //定义停止动画的方法
alpha.cancel();    //动画停止

public void onClick(View v) {

}
}    

 

好了 一个简单地透明图动画就写好   其实很简单的

     上面的动画只有一个  其实可以给一个View添加几个动画效果的

        这就要用到AnimationSet动画集合 类

       【方法】 add() 方法

         AnimationSet set  = new AnimationSet();

          set.add(Animation animation  )     ********  添加动画模式  

         【方法】  start() 方法

             set.start();

           好了  下一遍在介绍AnimationSet动画结合类










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值