说一下思路,widget是一个RemoteView,更新方式有些特殊,我在网上找了很久,最终想起来一个同事做过这种效果,把他的源码拿过来看了下。他的思路是在布局里给LinearLayout设置动画属性,把自己想加载动画的视图放进去就行了。
我们这里实现的效果是垂直往下翻页。
其实就是一个图片缩放动画。这里为了更细致点,可以拆分成两个部分:上半页高度由0慢慢放大到100%,接着下半部分宽度由0慢慢放大到100%.
可以在布局里先写好动画:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="@integer/pageturning_layout_weight_top"> <!--固定高度比例,缩放时视图不会变形-->
<LinearLayout
android:id="@+id/topnewnum_h"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/la_pageturning_top" >
<ImageView
android:id="@+id/topnewhalf_h"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="@integer/pageturning_layout_weight_bottom" >
<LinearLayout
android:id="@+id/bottomnewnum_h"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/la_pageturning_bottom">
<ImageView
android:id="@+id/bottomnewhalf_h"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
重点在于中间的那行红色代码,在widget中,我们一般不能在代码中给视图添加动画,所以就使用这种方法变通一下。就可以达到效果了。至于动画文件,大家就自己定义吧。