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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Recent Announcements
Recent Announcements
Vercel News
Vercel News
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
G
Google Developers Blog
罗磊的独立博客
爱范儿
爱范儿
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 翻滚的咸鱼

Ops权限行为记录源码解析 TaskView方案框架原理 视频解码器问题 常用命令 扫光动效 引导记录 性能优化 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

简单,且实用