Android-桌面小组件RemoteViews播放动画

一、前言

前段时间什么比较火?当然是木鱼了,木鱼一敲,烦恼全消~在这个节奏越来越快的社会上,算是一个不错的解压利器!

我们也紧跟时事,推出了 我要敲木鱼(各大市场均可以下载哦~)

咳咳,扯远了,说回正题

我们在后台收到大量反馈,说是希望添加桌面组件敲木鱼功能。好嘛,用户的话就是圣旨,那必须要安排上,正好我也练练手。

老规矩,先来看下我实现的效果

这个功能看着很简单对吧,却也花了我一天半的时间。主要用来实现敲击动画了!!

二、代码实现

1、新建小组件

2、修改界面样式

主要会生成3个关键文件(文件名根据你设置的来)

①、APPWidget 类,继承于 AppWidgetProvider,本质是一个 BroadCastReceiver

②、layout/widget.xml ,小组件布局文件

③、xml/widget_info.xml ,小组件信息说明文件

同时会在 AndroidManifest中注册好

类似如下代码:

ini 复制代码
     <receiver
            android:name=".receiver.MuyuAppWidgetBig"
            android:exported="false">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.fyspring.bluetooth.receiver.action_appwidget_muyu_knock" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/app_widget_info_big" />
        </receiver>

3、添加敲木鱼逻辑代码

通过 APPWidget 的模板代码我们知道,内部通过 RemoteViews 来进行更新View,而我们都知道 RemoteViews 是无法通过 findViewById 来转成对应的 view,更无法对其添加 Animator。那么我们该怎么办来给桌面木鱼组件添加一个 缩放动画呢?

给你三秒时间考虑下,这里我可花了一天时间来研究....

通过 layoutAnimation !!!

layoutAnimation 是在 ViewGroup 创建之后,显示时作用的,作用时间是:ViewGroup 的首次创建显示,之后再有改变就不行了。

虽然 RemoteViews 不能执行 findViewById,但它提供了两个关键方法: remoteViews.removeAllViews 和 remoteViews.addView 。如果我们在点击时,向组件布局中添加一个带有 layoutAnimation 的布局,不是就可以间接播放动画了么?

关键代码:

kotlin 复制代码
private fun doAnimation(context: Context?, remoteViews: RemoteViews?) {
        remoteViews?.removeAllViews(R.id.muyu_rl)
        val remoteViews2 = RemoteViews(context?.packageName, R.layout.anim_layout)
        remoteViews2.setImageViewResource(R.id.widget_muyu_iv, R.mipmap.ic_muyu)
        remoteViews?.addView(R.id.muyu_rl, remoteViews2)
    }

小组件布局:

ini 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.BlueToothDemo.AppWidget.Container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shape_round_bg"
    android:theme="@style/Theme.BlueToothDemo.AppWidgetContainer">

    <LinearLayout
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/appwidget_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:contentDescription="测试桌面木鱼"
            android:text="已敲0次"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/muyu_rl"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/widget_muyu_iv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_margin="15dp"
                android:src="@mipmap/ic_muyu" />

        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

添加替换的动画布局(anim_layout.xml),注意两边的木鱼ImgView 的 ID保持一致,因为要统一设置点击事件!!

ini 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layoutAnimation="@anim/muyu_anim">

    <ImageView
        android:id="@+id/widget_muyu_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_muyu2" />
</RelativeLayout>

动画文件:(muyu_anim.xml)

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
android:animation="@anim/scale_anim"/>

动画文件:(scale_anim.xml)

ini 复制代码
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromXScale="0.9"
    android:fromYScale="0.9"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1" />

关键动画代码就是以上这些,如果有问题欢迎私信。希望大家在新的一年里,木鱼一敲,烦恼全消~

欢迎体验下我做的木鱼,记得搜 我要敲木鱼 哦~~

相关推荐
游戏开发爱好者81 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码20351 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥1 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓2 小时前
[JDBC]元数据
android
独行soc2 小时前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能2 小时前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿2 小时前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc2 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20353 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106323 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview