Android动画透明度,平移,旋转,拉伸,动态添加按钮

这篇博客详细介绍了Android中实现动画的各种方法,包括透明度变化、平移、旋转和拉伸的动画效果,并提供了相应的状态图和代码示例。此外,还展示了如何在代码中动态添加按钮并应用动画,以及通过点击事件触发图片和布局的变化。
摘要由CSDN通过智能技术生成

步骤:

透明度变化

状态图:

这里写图片描述

代码:

  //透明度变化
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);//第一个参数开始的透明度,第二个参数结束的透明度
                alphaAnimation.setDuration(3000);//多长时间完成这个动作
                imageView1.startAnimation(alphaAnimation);
  • 1
  • 2
  • 3
  • 4

平移:

状态图:

这里写图片描述

代码:

 //平移
                TranslateAnimation animation = new TranslateAnimation(0,imageView1.getMeasuredWidth(), 0, 0);
 //前两个参数是设置x轴的起止位置,后两个参数设置y轴的起止位置               
                animation.setDuration(3000);
                imageView2.startAnimation(animation);
  • 1
  • 2
  • 3
  • 4
  • 5

旋转:

状态图:

这里写图片描述

代码:

 //旋转中心是(0,0)
                RotateAnimation rotateAnimation = new RotateAnimation(0,360);
                第一个参数是旋转中心,第二个是旋转角度,旋转中心(0~100%)
                rotateAnimation.setDuration(3000);
                imageView3.startAnimation(rotateAnimation);
  • 1
  • 2
  • 3
  • 4
  • 5

拉伸:

状态图:

这里写图片描述

代码:

 //拉伸
                ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.5f,1,0.5f);//默认从(0,0)
                scaleAnimation.setDuration(3000);
                imageView4.startAnimation(scaleAnimation);
  • 1
  • 2
  • 3
  • 4

源码:

layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/button_start_animation"
            android:text="开始动画"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/animation_imageview5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/b"/>
        <ImageView
            android:id="@+id/animation_imageview1"
            android:layout_width="wrap_content
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值