2010-12-07 50 views
1

我有一個數字時鐘小部件,並且我想使用自定義字體來顯示時間。我知道它不能在遠程視圖中完成,因此我已經獲得了一些代碼,可以將自定義字體呈現爲位圖,然後通過遠程視圖將其推送到圖像視圖。但是,我無法讓它工作。這是我的代碼到目前爲止:使用自定義字體顯示時間的問題

 public Bitmap buildUpdate(String time) 
    { 
      RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.main); 
      Bitmap myBitmap = Bitmap.createBitmap(100, 50, Bitmap.Config.ARGB_4444); 
      Canvas myCanvas = new Canvas(myBitmap); 
      Paint paint = new Paint(); 
      Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.ttf"); 
      paint.setAntiAlias(true); 
      paint.setSubpixelText(true); 
      paint.setTypeface(clock); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.WHITE); 
      paint.setTextSize(15); 
      myCanvas.drawText(time, 0, 20, paint); 
      views.setImageViewBitmap(R.id.TimeView, myBitmap); 
      return myBitmap; 
      } 

    private void update() { 
     mCalendar.setTimeInMillis(System.currentTimeMillis()); 
     final CharSequence date = DateFormat.format(mDateFormat, mCalendar); 
     final CharSequence day = DateFormat.format(mDayFormat, mCalendar); 
//  final CharSequence time = DateFormat.format(mTimeFormat, mCalendar); 
     String time = (String) DateFormat.format(mTimeFormat, mCalendar); 
     RemoteViews views = new RemoteViews(getPackageName(), R.layout.main); 
     views.setTextViewText(R.id.Day, day); 
     views.setTextViewText(R.id.Date, date); 
//  views.setTextViewText(R.id.Time, time); 
     buildUpdate(time); 
     ComponentName widget = new ComponentName(this, DigiClock.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(widget, views); 
    } 

任何幫助表示讚賞。

+1

「但是,我無法使它工作。」 - 如果你真的希望得到幫助,你需要更好地解釋你的問題。 – CommonsWare 2010-12-07 16:12:44

回答

3

我現在已經找到了(在一個有經驗的開發人員的幫助下)如何讓它工作。 現在的最終代碼是:

private void update() { 
    mCalendar.setTimeInMillis(System.currentTimeMillis()); 
    final CharSequence date = DateFormat.format(mDateFormat, mCalendar); 
    final CharSequence day = DateFormat.format(mDayFormat, mCalendar); 
    String time = (String) DateFormat.format(mTimeFormat, mCalendar); 
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.main); 
    views.setTextViewText(R.id.Day, day); 
    views.setTextViewText(R.id.Date, date); 
    views.setImageViewBitmap(R.id.TimeView, buildUpdate(time)); 
    ComponentName widget = new ComponentName(this, DigiClock.class); 
    AppWidgetManager manager = AppWidgetManager.getInstance(this); 
    manager.updateAppWidget(widget, views); 
} 

    public Bitmap buildUpdate(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(),"Clockopia.ttf"); 
     paint.setAntiAlias(true); 
     paint.setSubpixelText(true); 
     paint.setTypeface(clock); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.WHITE); 
     paint.setTextSize(65); 
     paint.setTextAlign(Align.CENTER); 
     myCanvas.drawText(time, 80, 60, paint); 
     return myBitmap; 
}