import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.widget.RemoteViews;
public class TimeWidgetProvider extends AppWidgetProvider {
private final String TAG = "TimeWidgetProvider";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.main);
Date dates = new Date();
int hour = dates.getHours() + 8;
int min = dates.getMinutes();
int sec = dates.getSeconds();
Log.v(TAG, "sec=" + sec);
updateViews.setImageViewBitmap(R.id.im2,
getBitmap(context, hour, R.drawable.appwidget_clock_hour));
updateViews.setImageViewBitmap(R.id.im3,
getBitmap(context, min, R.drawable.appwidget_clock_minute));
updateViews.setImageViewBitmap(R.id.im4,
getBitmap(context, sec, R.drawable.appwidget_clock_minute));
// updateViews.setImageViewResource(R.id.im1,
// R.drawable.appwidget_clock_dial);
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private Bitmap getBitmap(Context context, int angle, int id) {
// 加载需要操作的图片,这里是eoeAndroid的logo图片
Bitmap bitmapOrg = BitmapFactory.decodeResource(context.getResources(),
id);
// 获取这个图片的宽和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
Log.v(TAG, "width=" + width);
Log.v(TAG, "height=" + height);
Log.v(TAG, "angle=" + angle);
// bitmapOrg.ge
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 旋转图片 动作
matrix.postRotate(360 / 12 * angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
private Bitmap getBitmaps(Context context, int angle, int id) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(context.getResources(),
id);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(360 / 60 * angle);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
private Bitmap getBitmapss(Context context, int angle, int id) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(context.getResources(),
id);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(360 / 60 * angle);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
Log.v("TAG", "onEnabled");
}
}