数字时钟如何使用自定义图像创建一个数字时钟android

时间:2012-03-22 00:51:42

标签: android clock

我想构建一个像这样的数字时钟:

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;                
        }

}

}

1 个答案:

答案 0 :(得分:0)

由于RemoteViews不支持为Textview设置字体,因此您需要绘制文本然后转换为Bitmap,如下所示:

第1步:使用自定义字体创建图片:

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)

请考虑使用此解决方案的内存