Widget使用SurfaceView

iuKa 发布于 2013/07/22 19:26
阅读 286
收藏 0
想要在widget上作出一个时钟的效果,就想用SurfaceView来画。但是Widget不支持SurfaceView,我就想用getDrawingCache来获取到它的bitmap,然后设置到widget上的ImageView上,但是SurfaceView没有在界面上画出来,返回值一直未Null啊,不知道大家有没有好的办法解决!!!下面是代码:
public class ClockWidgetProvider extends AppWidgetProvider {

	private ClockSurfaceView clock;

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.i("kevin.liu", "onEnabled");
		
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.i("kevin.liu", "onDisabled");
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.i("kevin.liu", "onUPdate");
		Log.i("kevin.liu", "Now: " + System.currentTimeMillis());
		if(clock == null) {
			clock = new ClockSurfaceView(context);
//			WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//			mgr.addView(clock, new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));
//			clock.setVisibility(View.INVISIBLE);
		}
//		clock.invalidate();
		clock.setDrawingCacheEnabled(true);
		clock.buildDrawingCache(true);
		if(clock.isDrawingCacheEnabled()) {
			Log.i("kevin.liu", "enable");
		}
		Bitmap bitmap = clock.getDrawingCache(); 
		Log.i("kevin.liu", "Bitmap: " + bitmap);
//		Bitmap bm = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
		for(int appWidgetId : appWidgetIds) {
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
			views.setImageViewBitmap(R.id.clock_widget_view, bitmap);
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
		clock.setDrawingCacheEnabled(false);
	}
	
	
}
加载中
0
iuKa
iuKa
没人自己顶
返回顶部
顶部