安卓 view淡入淡出(fade in fade out) kotlin

文章目录

  • 前言
  • 一、布局文件
  • 二、kotlin扩展方法
    • 1.fadeOutAnimation 淡出动画
    • 2.fadeInAnimation 淡入动画
  • 三、使用
  • 总结


前言

好久没写文章了,简单码一个淡入淡出,我们先上效果图

淡入淡出图片
那么接下来上代码


一、布局文件

我这边直接将activity_main.xml改为下列代码,可以看到其中包含一张图片,一条文本和一个按钮

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ImageViewandroid:id="@+id/iv"android:layout_width="200dp"android:layout_height="200dp"android:src="@color/white"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"android:textSize="20sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/iv" /><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv"android:text="点击淡入淡出"/></androidx.constraintlayout.widget.ConstraintLayout>

二、kotlin扩展方法

1.fadeOutAnimation 淡出动画

利用kotlin的扩展特性写了一个view的扩展方法
代码如下(示例):

    fun View.fadeOutAnimation() {val mFadeOutAnimation: AlphaAnimation?// 监听动画结束的操作mFadeOutAnimation = AlphaAnimation(1.0f, 0.0f)//淡出时间mFadeOutAnimation.setDuration(1000)mFadeOutAnimation.fillAfter = truemFadeOutAnimation.setAnimationListener(object : AnimationListener {override fun onAnimationStart(arg0: Animation) {}override fun onAnimationRepeat(arg0: Animation) {}override fun onAnimationEnd(arg0: Animation) {this@fadeOutAnimation.visibility = View.GONE}})this@fadeOutAnimation.startAnimation(mFadeOutAnimation)}
  • setDuration 是淡出动画持续时间
  • fillAfter 是设置在动画结束过后保持最后的样子
  • setAnimationListener 是为了在动画结束时完全隐藏图片,让我们一会进行淡出时不会很突兀
  • startAnimation 开始动画

2.fadeInAnimation 淡入动画

淡入动画也是一样的
代码如下(示例):

    fun View.fadeInAnimation() {var mFadeInAnimation: AlphaAnimation?mFadeInAnimation = AlphaAnimation(0.0f, 1.0f)//淡入时间mFadeInAnimation.setDuration(1000)mFadeInAnimation.fillAfter = truethis@fadeInAnimation.startAnimation(mFadeInAnimation)}
  • setDuration 是淡入动画持续时间
  • fillAfter 是设置在动画结束过后保持最后的样子
  • startAnimation 开始动画

三、使用

直接初始化图片然后使用就好了

  // 假设imageView是你要更换图片的ImageView控件val imageView = findViewById<ImageView>(R.id.iv)val textView = findViewById<TextView>(R.id.tv)val button = findViewById<Button>(R.id.btn)button.setOnClickListener {imageView.fadeOutAnimation()textView.fadeOutAnimation()MainScope().launch {//假设这里是加载网络图片所耗时delay(300)}if (textView.text != "这是红色") {imageView.setImageResource(R.color.red)textView.text = "这是红色"} else {imageView.setImageResource(R.color.white)textView.text = "这是白色"}imageView.fadeInAnimation()textView.fadeInAnimation()}

完整activtiy代码如下

import android.os.Bundle
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launchclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContentView(R.layout.activity_main)ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)insets}// 假设imageView是你要更换图片的ImageView控件val imageView = findViewById<ImageView>(R.id.iv)val textView = findViewById<TextView>(R.id.tv)val button = findViewById<Button>(R.id.btn)button.setOnClickListener {imageView.fadeOutAnimation()textView.fadeOutAnimation()MainScope().launch {//假设这里是加载网络图片所耗时delay(300)}if (textView.text != "这是红色") {imageView.setImageResource(R.color.red)textView.text = "这是红色"} else {imageView.setImageResource(R.color.white)textView.text = "这是白色"}imageView.fadeInAnimation()textView.fadeInAnimation()}}fun View.fadeOutAnimation() {val mFadeOutAnimation: AlphaAnimation?// 监听动画结束的操作mFadeOutAnimation = AlphaAnimation(1.0f, 0.0f)//淡出时间mFadeOutAnimation.setDuration(1000)mFadeOutAnimation.fillAfter = truemFadeOutAnimation.setAnimationListener(object : AnimationListener {override fun onAnimationStart(arg0: Animation) {}override fun onAnimationRepeat(arg0: Animation) {}override fun onAnimationEnd(arg0: Animation) {this@fadeOutAnimation.visibility = View.GONE}})this@fadeOutAnimation.startAnimation(mFadeOutAnimation)}fun View.fadeInAnimation() {var mFadeInAnimation: AlphaAnimation?mFadeInAnimation = AlphaAnimation(0.0f, 1.0f)//淡入时间mFadeInAnimation.setDuration(1000)mFadeInAnimation.fillAfter = truethis@fadeInAnimation.startAnimation(mFadeInAnimation)}}

总结

本文通过一个简单的示例介绍了在Android开发中实现淡入淡出效果的方法。首先,我们定义了两个扩展方法,分别用于实现淡出动画和淡入动画。然后,在点击按钮时,我们通过调用这两个方法来实现图片和文本的淡出效果。在耗时操作完成后,我们根据文本内容的不同来切换图片和文本的内容,并进行淡入效果的展示。通过这种方式,我们可以实现图片和文本的平滑过渡,给用户带来更好的使用体验。代码简单易懂,具有一定的可复用性。希望对大家的Android开发有所帮助。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/18481.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Daily Code】leetcode2951. 找出峰值

Problem: 2951. 找出峰值 Code class Solution { public:vector<int> findPeaks(vector<int>& mountain) {int n mountain.size();vector<int> res;for(int i 1; i < n - 1; i ) {if(mountain[i] > mountain[i - 1] && mountain[i] >…

【并发程序设计】11.进程间通信

11.进程间通信 &#xff08;IPC&#xff0c;InterProcess Communication&#xff09;进程和进程之间交换信息。 常用通信方式 无名管道&#xff08;pipe&#xff09;有名管道 &#xff08;fifo&#xff09;信号&#xff08;signal&#xff09;共享内存(mmap)套接字&#xff0…

jenkins+sonarqube部署与配置过程

1、部署jenkins&#xff08;本文不做说明&#xff09; 2、部署sonarqube(docker-compose) version: "2.1"services:sonarqube:image: sonarqube:9.9.4-communitycontainer_name: sonarqubedepends_on:- dbports:- 9000:9000networks:- sonarnetenvironment:SONARQU…

大模型-大模型评测

1、参考文章&#xff1a;https://www.linkresearcher.com/information/f4a3b0e0-9d14-45cc-9f8a-acac0ce6addd 2、总结&#xff1a; 语义评测&#xff1a;评测大模型是否能正确理解语言的含义代码评测&#xff1a;评测大模型是否能给出能够执行出正确结果的代码对齐评测&#…

C++候捷stl-视频笔记1

认识headers、版本、重要资源 STL的核心思想是泛型编程 新式头文件内的组件封装在命名空间std中&#xff1a; using namespace std; using std::cout;或std::vector vec; 旧式头文件内的组件不封装在命名空间std中 注:不建直接使用using namespace xxx&#xff0c;如果使用的…

Meterpreter工具使用

Meterpreter属于stage payload&#xff0c;在Metasploit Framework中&#xff0c;Meterpreter是一种后渗透工具&#xff0c;它 属于一种在运行过程中可通过网络进行功能扩展的动态可扩展型Payload。这种工具是基于“内存DLL注 入”理念实现的&#xff0c;它能够通过创建一个新进…

SAP_MM_业务数据

在SAP的物料管理模块&#xff08;MM&#xff0c;Materials Management&#xff09;中&#xff0c;业务数据&#xff08;Transactional Data&#xff09;是日常业务操作所产生的动态数据。这些数据记录了与采购、库存和物料需求计划&#xff08;MRP&#xff09;相关的实际业务活…

微乐校园管理系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;管理员管理&#xff0c;基础数据管理&#xff0c;叫车管理&#xff0c;代跑管理&#xff0c;二手商品管理 司机账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;叫车管理&#xff0c…

MPE中environment.py复盘

1.__init__ 初始化函数参数&#xff1a; world: 一个包含环境信息和所有智能体的世界对象。reset_callback, reward_callback, observation_callback, info_callback, done_callback: 这些都是回调函数&#xff0c;用于在环境的特定事件发生时执行相应的操作。shared_viewer: 一…

邦芒面试:面试官“青睐”你的微妙信号

在激烈的面试过程中&#xff0c;你是否好奇过面试官是如何评估你的表现&#xff0c;以及哪些举动表明你有可能成为他们心仪的候选人&#xff1f;接下来&#xff0c;我们将揭示面试官“青睐”你的几个微妙信号。 1. 主动分享职位详情 当面试官不仅满足于询问你的工作经历&#…

【Linux进程篇】Linux内核——程序地址空间的初构

W...Y的主页 &#x1f60a; 代码仓库分享&#x1f495; 程序地址空间回顾 我们在讲C语言的时候&#xff0c;大家应该都见过这样的空间布局图&#xff1a; 为了更好的验证不同的数据在内存中的存储位置&#xff0c;下面这段代码我们可以去实验一下&#xff1a; #include<…

云WAF:守护网络安全的强大盾牌

随着互联网技术的飞速发展&#xff0c;网络安全问题已经成为全球性的难题。为了应对日益复杂的网络安全威胁&#xff0c;云WAF&#xff08;Web Application Firewall&#xff0c;即Web应用防火墙&#xff09;应运而生&#xff0c;并以其强大的功能和优势&#xff0c;成为网络安…

GPT-4o:人工智能交互的新纪元

GPT-4o作为OpenAI最新发布的模型&#xff0c;标志着人工智能领域的一大飞跃&#xff0c;特别是在自然语言处理和交互体验上。本文将概述GPT-4o的主要特点、技术改进以及它如何改变我们与AI互动的方式。 GPT-4o的诞生背景 在GPT-4o之前&#xff0c;用户通过Voice Mode与ChatGPT…

Spring如何管理Bean的生命周期呢?

我们都知道&#xff0c;在面试的过程中&#xff0c;关于 Spring 的面试题&#xff0c;那是各种各样&#xff0c;很多时候就会问到关于 Spring的相关问题&#xff0c;比如 AOP &#xff0c;IOC 等等&#xff0c;还有就是关于 Spring 是如何管理 Bean 的生命周期的相关问题&#…

基于高光谱数据集的创新点实现-高斯核函数卷积神经网络

一、高光谱数据集简介 1.1 数据集简介 数据集链接在这:高光谱数据集(.mat.csv)-科研学术 数据集包含下面三个文件&#xff1a; 文件中包含.mat与.csv,145x145x220, 其实主要使用avirissub.csv文件&#xff0c;在代码上只是将mat文件转成了csv文件。具体avirissub.csv如下&am…

计算机网络-BGP基础概念

一、BGP的基本概念 BGP是一种实现自治系统AS之间的路由可达&#xff0c;并选择最佳路由的矢量性协议。早期发布的三个版本分别是BGP-1&#xff08;RFC1105&#xff09;、BGP-2&#xff08;RFC1163&#xff09;和BGP-3&#xff08;RFC1267&#xff09;&#xff0c;1994年开始使用…

企业微信群发群消息:策略、技巧与效果优化

在数字化营销日益盛行的今天&#xff0c;企业微信已成为企业与客户、员工之间沟通的重要桥梁。其中&#xff0c;群发群消息功能更是企业快速传递信息、提升沟通效率的关键工具。本文将深入探讨企业微信群发群消息的策略、技巧以及效果优化方法。 一、企业微信群发群消息的重要…

TiDB-从0到1-MVCC

TiDB从0到1系列 TiDB-从0到1-体系结构TiDB-从0到1-分布式存储TiDB-从0到1-分布式事务TiDB-从0到1-MVCC 一、MVCC Multi-Version Concurrency Control 多版本并发控制&#xff0c;其主要解决了读并发的问题。 其维持一个数据的多个版本使读写操作没有冲突。也就是说数据元素X…

oracle linux7安装oracle11g0204

1、平时需要修改 /etc/redhat-release文件为Red Hat Enterprise Linux 7,这次不需要了。 2、关闭selinx nano /etc/selinux/config 改为disabled 3、nano /etc/hosts 修改解析 在oracle服务器中增加 /etc/hosts中一个对应 192.168.1.10 CLOUD-MC-SQL1 4、修改系统文件 /…

虚拟机报错:VMX 进程已提前退出。VMware Workstation 无法连接到虚拟机。

解决报错&#xff1a;VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录。 VMX 进程已提前退出。 解决方案&#xff1a;右键桌面图标进入VMware Workstation Pro的属性设置&#xff0c;兼容性–勾选“以管理员…