Android动画效果Widget(飞行模式)
1. 功能:
创建Widget后,若为飞行模式,点击Widget,取消飞行模式,否则设置为飞行模式。在变化过程中有动画效果。
2. 实现思想:
对应main.xml布局文件,Widget由三张图片组成,pane上层左边为airplane,右边为switch点击桌面widget时图标的时候,左边airplane四张图标动态的在切换,直到设置为飞行模式或者取消飞行模式状态完成时,airplane切换停止,同时右边switch图片切换settrings_switch_on或者settrings_switch_off。
3. 程序中用到图片资源文件如下:
settings_icon_airplane_01.png
settings_icon_airplane_02.png
settings_icon_airplane_03.png
settings_icon_airplane_04.png
settings_pane_off.png
settings_switch_off.png
settings_switch_on.png
widget_icon_airplane.png
code:
布局文件main.xml
<?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">
<ImageView android:id="@+id/setting_pane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/settings_pane_off"/>
<ImageView android:id="@+id/setting_airplane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="6dip"
android:src="@drawable/settings_icon_airplane_04"/>
<ImageView android:id="@+id/setting_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/setting_airplane"
android:layout_centerVertical="true"
android:layout_marginLeft="4dip"
android:src="@drawable/settings_switch_off"/>
</RelativeLayout>
在res下新建xml文件夹,然后新建widget_provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="50dip" <-指定宽度度->
android:minHeight="50dip" <-指定高度->
android:initialLayout="@layout/main"/>
新建AirplaneModeEnabler类
public class AirplaneModeEnabler{
//此方法判断是否为飞行模式
public static boolean isAirplaneModeOn(Context context){
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,0)!=0;
}
//此方法设置或取消飞行模式
public static void setAirplaneModeOn(Context context,boolean enabling){
Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabling?1:0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", enabling);
context.sendBroadcast(intent);
}
}
建MyWidget 类,继承AppWidgetProvider
public class MyWidget extends AppWidgetProvider {
private Context mContext = null;
public RemoteViews remoteViews = new RemoteViews("com.archermind.widget", R.layout.main);
public final static String SET_AIRPLANE_ACTION = "com.archermind.set_airplane_mode";
private static final int START_ANIMATION = 0;
private static final int END_ANIMATION = 1;
private static Boolean mRunAnimation = false;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == START_ANIMATION) {
Log.i("state", "START_ANIMATION: mRunAnimation = " + mRunAnimation);
if (!mRunAnimation) {
return;
}
remoteViews.setImageViewResource(R.id.setting_airplane, mDrawable[msg.arg1]);
AppWidgetManager appWidgetManger = AppWidgetManager
.getInstance(mContext);
int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
mContext, MyWidget.class));
for (int id: appIds) {
appWidgetManger.updateAppWidget(id, remoteViews);
}
// send message for next frame
Message message = mHandler.obtainMessage(START_ANIMATION);
message.arg1 = (msg.arg1 + 1) % 4;
mHandler.sendMessageDelayed(message, 200);
} else if (msg.what == END_ANIMATION) {
Log.i("state", "END_ANIMATION: mRunAnimation = " + mRunAnimation);
AppWidgetManager appWidgetManger = AppWidgetManager
.getInstance(mContext);
int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
mContext, MyWidget.class));
for (int id: appIds) {
updateAppWidget(mContext, appWidgetManger, id);
}
}
Log.i("hThreadId",Long.toString(Thread.currentThread().getId()));
}
};
private int[] mDrawable = {
R.drawable.settings_icon_airplane_01,
R.drawable.settings_icon_airplane_02,
R.drawable.settings_icon_airplane_03,
补充:移动开发 , Android ,