2012-03-22 57 views
1

數字時鐘如何創建自定義圖像我想建立這樣一個digitalclock數字時鐘小時的Android

https://play.google.com/store/apps/details?id=com.bacastudio.lcd

我嘗試使用本教程中添加字體,但它不工作: https://github.com/browep/AndroidCustomFontWidgets/blob/master/AndroidManifest.xml

感謝所有幫助:)

我有問題的字體,在Eclipse中一切正常,任何錯誤,但是當我運行它什麼都沒有發生。

package org.me; 



    public class Euro2012cc extends AppWidgetProvider { 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    context.startService(new Intent(context, UpdateService.class)); 
} 

public static class UpdateService extends Service { 
    @Override 
    public void onStart(Intent intent, int startId) { 
     // Build the widget update for today 
     RemoteViews view = buildUpdate(this); 
     // Push update for this widget to the home screen 
     ComponentName thisWidget = new ComponentName(this, Euro2012cc.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(thisWidget, view); 
    } 

     public RemoteViews buildUpdate(Context context) { 
      RemoteViews view = null; 
      view = new RemoteViews(context.getPackageName(), R.layout.main); 
      String time = "blablabla"; 
      view.setImageViewBitmap(R.id.test, buildUpdateTime(time)); 
      return view; 
     }   
     @Override 
     public IBinder onBind(Intent intent) { 
      // We don't need to bind to this service 
      return null; 
      }//ibinder 
     public Bitmap buildUpdateTime(String time) { 
      Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);; 
      Canvas myCanvas = new Canvas(myBitmap); 
      Paint paint = new Paint(); 
      Typeface clock = Typeface.createFromAsset(this.getAssets(),"TechnoHideo.ttf"); 
      paint.setAntiAlias(true); 
      paint.setSubpixelText(true); 
      paint.setTypeface(clock); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.WHITE); 
      paint.setTextSize(15); 
      paint.setTextAlign(Align.CENTER); 
      myCanvas.drawText(time, 80, 60, paint); 
      return myBitmap;     
     } 

} 

}

回答

0

因爲RemoteViews不支持設置字體爲TextView的,所以你需要繪製文本,然後轉換爲位圖是這樣的:

第一步:自定義字體創建圖像:

public static synchronized Bitmap createBitmapWithTypeface(Typeface tf, String strMyText) { 
    Paint paint = new Paint(); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.RED); 
    paint.setTextSize(CUSTOM_TEXT_SIZE); 
    paint.setAntiAlias(true); 
    You can load you custom typeface here 
    paint.setTypeface(tf); 

    Bitmap mybitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ALPHA_8); 
    Canvas c = new Canvas(mybitmap); 
    c.drawColor(Color.WHITE);c.drawText(strMyText, 0, 0, paint); 

    return mybitmap; 
} 

步驟2: 設置圖像到RemoteViews

setImageViewBitmap(int viewId, Bitmap bitmap) 

請考慮有關內存使用此解決方案

+0

我如何可以加載字體? getAsset不能在AppWidget中工作:( – Azor 2012-03-22 14:31:52