当前位置: 首页 > news >正文

Android中通过属性动画实现文字轮播效果

前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下"通俗易懂,风趣幽默",感觉非常有意思,忍不住分享一下给大家。
👉点击跳转到教程

一、创建一个自定义ProvinceView类,具体代码如下

/*** @Author: ly* @Date: 2024/2/22* @Description:*/
private val provinces = listOf("中原区","二七区","管城回族区","金水区","上街区","惠济区","中牟县","巩义市","荥阳市","新密市","新郑市","登封市"
)class ProvinceView(context: Context, attrs: AttributeSet) : View(context, attrs) {private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {textSize = 30.dptextAlign = Paint.Align.CENTER}var province = "中原区"set(value) {field = valueinvalidate()}init {paint.color = Color.parseColor("#FF0000")setLayerType(LAYER_TYPE_SOFTWARE, null)}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)canvas.drawText(province, width / 2f, height / 2f, paint)}
}class ProvinceEvaluator : TypeEvaluator<String> {override fun evaluate(fraction: Float, startValue: String, endValue: String): String {val startIndex = provinces.indexOf(startValue)val endIndex = provinces.indexOf(endValue)val currentIndex = startIndex + ((endIndex - startIndex) * fraction).toInt()return provinces[currentIndex]}
}

二、在对应的xml中引入该自定义View

<?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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.example.customviewanimation.view.ProvinceViewandroid:id="@+id/view"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

三、在Activity中通过属性动画来实现文字轮播效果

class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val animator = ObjectAnimator.ofObject(view, "province", ProvinceEvaluator(), "登封市")animator.startDelay = 1000animator.duration = 8000animator.start()}
}

具体效果如图所示:
在这里插入图片描述

http://www.lryc.cn/news/304522.html

相关文章:

  • 最长的回文串
  • 2023 H1 中国边缘公有云服务市场 Top2,百度智能云加速推动分布式云智能化升级
  • Emlog博客网站快速搭建并结合内网穿透实现远程访问本地站点
  • 力扣经典题目解析--旋转图像(字节二面)
  • 【ARMv8M Cortex-M33 系列 8.1 -- RT-Thread 堆内存 检查命令 free 实现及介绍】
  • milvus Delete API流程源码分析
  • CentOS使用Docker搭建Halo网站并实现无公网ip远程访问
  • 【JVM】垃圾回收算法
  • 如何和将原始request的Header中的值传递给openfeign请求的Header? 以及又如何获取openfeign请求中Header中的值
  • Flink 侧输出流(SideOutput)
  • C语言中关于#include的一些小知识
  • DSP芯片 机器码下载方法 【主要 “扯” 用Uniflash下载的方法】
  • 速盾网络:CDN用几天关了可以吗?安全吗?
  • MR混合现实情景实训教学系统在高空作业课堂中的应用
  • Windows系统中定时执行python脚本
  • HashMap 源码学习-jdk1.8
  • WebStorm 2023:让您更接近理想的开发环境 mac/win版
  • java面试题:数字与字母的映射表
  • Jmeter教程-JMeter 环境安装及配置
  • 十大基础排序算法
  • IP协议及相关技术协议
  • 小红书x-s算法及补环境 单旋转验证码
  • 代码检测规范和git提交规范
  • Elasticsearch:什么是搜索引擎?
  • 人工智能几个关键节点:深蓝,AlphaGo,ChatGPT,Sora
  • WordPres Bricks Builder 前台RCE漏洞复现(CVE-2024-25600)
  • 代码随想录算法训练营总结 | 慢慢总结,想起啥就先写上
  • 基于开源模型对文本和音频进行情感分析
  • SQL中为什么不要使用1=1
  • python 几种常见的音频数据读取、保存方式