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

一、前言

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

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

咳咳,扯远了,说回正题

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

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

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

二、代码实现

1、新建小组件

 2、修改界面样式

主要会生成3个关键文件(文件名根据你设置的来)
①、APPWidget  类,继承于 AppWidgetProvider,本质是一个 BroadCastReceiver

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

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

扫描二维码关注公众号,回复: 14719170 查看本文章

同时会在 AndroidManifest中注册好

类似如下代码:

     <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 的布局,不是就可以间接播放动画了么?

关键代码:

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)
    }

小组件布局:

<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保持一致,因为要统一设置点击事件!!

<?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 version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
android:animation="@anim/scale_anim"/>

动画文件:(scale_anim.xml)

<?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" />

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

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

猜你喜欢

转载自blog.csdn.net/u013700040/article/details/128416435
今日推荐