惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
H
Help Net Security
小众软件
小众软件
N
Netflix TechBlog - Medium
C
Check Point Blog
量子位
Last Week in AI
Last Week in AI
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
D
DataBreaches.Net
Project Zero
Project Zero
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Security Latest
Security Latest
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
I
Intezer
L
Lohrmann on Cybersecurity
Cyberwarzone
Cyberwarzone
博客园_首页
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
P
Palo Alto Networks Blog
V
V2EX
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
F
Full Disclosure
博客园 - 司徒正美
Recent Announcements
Recent Announcements
IT之家
IT之家
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Attack and Defense Labs
Attack and Defense Labs
Cloudbric
Cloudbric
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog

博客园 - 翻滚的咸鱼

视频解码器问题 常用命令 扫光动效 引导记录 性能优化 View共享动效 常用脚本文件 Launcher 卡片框架多模块集成 Launcher 桌面源码笔记二 Launcher 桌面源码笔记一 氛围灯动态屏保取色方案二 氛围灯动态屏保取色方案一 自定义最近任务管理器 anr问题 TV RecyclerView 焦点处理笔记 多类型适配器 多屏下字体自动取色 阻尼拉伸方案 图片区域点击处理 RecyclerView设置默认焦点跟多页面焦点抢占 TV RecyclerView 滑动后操作保持落焦在左侧第一个View Fragment翻盖动效
当个View下,使用Drawable入场退场动画
翻滚的咸鱼 · 2024-04-29 · via 博客园 - 翻滚的咸鱼

效果图,简单的入场退场动效,一般情况是不同view之间去添加动画,某些条件下显然并不符合需求,需要在单个ImageView下进行的

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <ImageView
        android:id="@+id/iv_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="100dp"
        android:text="点击切换背景"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

该view下,背景存在不同资源,png,drawable,svg,color,本质其实都是drawable

drawable下使用入场退场动效,需要使用到 TransitionDrawable,utils类如下

object TransitionDrawableUtils {

    private var topDrawable: Drawable? = null

    fun setDrawable(bgView: ImageView, drawable: Drawable) {
        if (topDrawable == null) {
            bgView.setImageDrawable(drawable)
        } else {
            val drawables = arrayListOf<Drawable>()
            topDrawable?.let { drawables.add(it) }
            drawables.add(drawable)
            val transition = TransitionDrawable(drawables.toTypedArray())
            bgView.setImageDrawable(transition)
            transition.startTransition(1000)
        }
        topDrawable = drawable
    }

}

要使用TransitionDrawable至少需要两个drawable资源,然后定义drawableList,进行背景切换,切换时TransitionDrawable会对资源进行过渡

package com.example.page

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable

class TransitionDrawableActivity : AppCompatActivity() {

    private var index = 0

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transition)

        val ivBg = findViewById<ImageView>(R.id.iv_bg)
        val btnClick = findViewById<Button>(R.id.btn_click)

        val bgList = arrayListOf<Drawable>(
            ColorDrawable(Color.CYAN),
            resources.getDrawable(R.drawable.bg_def, this.theme),
            toTransitionDrawable(resources.getDrawable(R.drawable.bg1, this.theme)),// svg图片需要转换一下
            toTransitionDrawable(resources.getDrawable(R.drawable.bg2, this.theme)),
            toTransitionDrawable(resources.getDrawable(R.drawable.bg3, this.theme)),
            toTransitionDrawable(resources.getDrawable(R.drawable.bg4, this.theme))
        )
        TransitionDrawableUtils.setDrawable(ivBg, bgList[index])
        btnClick.setOnClickListener {
            index++
            if (index >= bgList.size) {
                index = 0
            }
            TransitionDrawableUtils.setDrawable(ivBg, bgList[index])
        }
    }

    private fun toTransitionDrawable(drawable: Drawable): Drawable {
        return drawable.toBitmap(1920, 1920, null).toDrawable(resources)
    }
}  

需要注意的是,如果是svg图片,需要转换一下,否则svg不支持TransitionDrawable

简单,且实用