• <bdo id='I3BM2'></bdo><ul id='I3BM2'></ul>

    <small id='I3BM2'></small><noframes id='I3BM2'>

    <i id='I3BM2'><tr id='I3BM2'><dt id='I3BM2'><q id='I3BM2'><span id='I3BM2'><b id='I3BM2'><form id='I3BM2'><ins id='I3BM2'></ins><ul id='I3BM2'></ul><sub id='I3BM2'></sub></form><legend id='I3BM2'></legend><bdo id='I3BM2'><pre id='I3BM2'><center id='I3BM2'></center></pre></bdo></b><th id='I3BM2'></th></span></q></dt></tr></i><div id='I3BM2'><tfoot id='I3BM2'></tfoot><dl id='I3BM2'><fieldset id='I3BM2'></fieldset></dl></div>

    <legend id='I3BM2'><style id='I3BM2'><dir id='I3BM2'><q id='I3BM2'></q></dir></style></legend>
        <tfoot id='I3BM2'></tfoot>
      1. 如何在小部件中使用自定义字体?

        How to use a custom typeface in a widget?(如何在小部件中使用自定义字体?)
      2. <i id='nJEfo'><tr id='nJEfo'><dt id='nJEfo'><q id='nJEfo'><span id='nJEfo'><b id='nJEfo'><form id='nJEfo'><ins id='nJEfo'></ins><ul id='nJEfo'></ul><sub id='nJEfo'></sub></form><legend id='nJEfo'></legend><bdo id='nJEfo'><pre id='nJEfo'><center id='nJEfo'></center></pre></bdo></b><th id='nJEfo'></th></span></q></dt></tr></i><div id='nJEfo'><tfoot id='nJEfo'></tfoot><dl id='nJEfo'><fieldset id='nJEfo'></fieldset></dl></div>

            • <bdo id='nJEfo'></bdo><ul id='nJEfo'></ul>
              <tfoot id='nJEfo'></tfoot>

                <tbody id='nJEfo'></tbody>
              <legend id='nJEfo'><style id='nJEfo'><dir id='nJEfo'><q id='nJEfo'></q></dir></style></legend>
            • <small id='nJEfo'></small><noframes id='nJEfo'>

                • 本文介绍了如何在小部件中使用自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数字时钟小部件.如何使用 assets/fonts 中的自定义字体作为显示时钟的 textview 中的默认字体?

                  I have a digital clock widget. How can I use a custom font from assets/fonts as the default font in the textview showing the clock?

                  这是我的代码:

                      package android.tristan.widget.digiclock;
                  
                  import java.util.Calendar;
                  
                  import android.app.PendingIntent;
                  import android.app.Service;
                  import android.appwidget.AppWidgetManager;
                  import android.appwidget.AppWidgetProvider;
                  import android.content.BroadcastReceiver;
                  import android.content.ComponentName;
                  import android.content.Context;
                  import android.content.Intent;
                  import android.content.IntentFilter;
                  import android.os.IBinder;
                  import android.os.Vibrator;
                  import android.text.format.DateFormat;
                  import android.widget.RemoteViews;
                  
                  
                  public class DigiClock extends AppWidgetProvider {
                  
                      @Override
                      public void onDisabled(Context context) {
                          super.onDisabled(context);
                          context.stopService(new Intent(context, UpdateService.class));
                      }
                      public void onReceive(Context context, Intent intent)
                      {
                          super.onReceive(context, intent);
                  
                          if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK"))
                          {
                            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(50);           
                              final Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN, null);
                              alarmClockIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                              final ComponentName cn = new ComponentName("com.android.deskclock", "com.android.deskclock.AlarmClock");
                              alarmClockIntent.setComponent(cn);
                              alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                              context.startActivity(alarmClockIntent);
                          }
                          if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK_2"))
                          {
                            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(50);
                             final Intent calendarIntent = new Intent(Intent.ACTION_MAIN, null);
                             calendarIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                              final ComponentName cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
                              calendarIntent.setComponent(cn);
                              calendarIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                              context.startActivity(calendarIntent);       
                          }
                      }
                  
                      @Override
                      public void onEnabled(Context context) {
                          super.onEnabled(context);
                          context.startService(new Intent(UpdateService.ACTION_UPDATE));
                      }
                  
                      @Override
                      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
                          super.onUpdate(context, appWidgetManager, appWidgetIds);
                          context.startService(new Intent(UpdateService.ACTION_UPDATE));
                          final int Top = appWidgetIds.length;
                          final int Bottom = appWidgetIds.length;
                          for (int i=0; i<Top; i++)
                          {
                              int[] appWidgetId = appWidgetIds;
                              RemoteViews top=new RemoteViews(context.getPackageName(), R.layout.main);
                              Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK");
                              PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
                              top.setOnClickPendingIntent(R.id.TopRow, pendingIntentClick);
                              appWidgetManager.updateAppWidget(appWidgetId, top);
                          }
                          for (int i=0; i<Bottom; i++)
                          {
                              int[] appWidgetId = appWidgetIds;
                              RemoteViews bottom=new RemoteViews(context.getPackageName(), R.layout.main);
                              Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK_2");
                              PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
                              bottom.setOnClickPendingIntent(R.id.BottomRow, pendingIntentClick);
                              appWidgetManager.updateAppWidget(appWidgetId, bottom);
                          }
                  }
                  
                      public static final class UpdateService extends Service {
                  
                         static final String ACTION_UPDATE = "android.tristan.widget.digiclock.action.UPDATE";
                  
                          private final static IntentFilter sIntentFilter;
                  
                          private final static String FORMAT_12_HOURS = "h:mm";
                          private final static String FORMAT_24_HOURS = "kk:mm";
                  
                          private String mTimeFormat;
                          private String mDateFormat;
                          private String mDayFormat;
                          private Calendar mCalendar;
                  
                          static {
                              sIntentFilter = new IntentFilter();
                              sIntentFilter.addAction(Intent.ACTION_TIME_TICK);
                              sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
                              sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
                          }
                  
                          @Override
                          public void onCreate() {
                              super.onCreate();
                              reinit();
                              registerReceiver(mTimeChangedReceiver, sIntentFilter);
                           }
                  
                          @Override
                          public void onDestroy() {
                              super.onDestroy();
                              unregisterReceiver(mTimeChangedReceiver);
                          }
                  
                          @Override
                          public void onStart(Intent intent, int startId) {
                              super.onStart(intent, startId);
                  
                              if (ACTION_UPDATE.equals(intent.getAction())) {
                                  update();
                              }
                          }
                  
                          @Override
                          public IBinder onBind(Intent intent) {
                              return null;
                          }
                  
                  
                          private void update() {
                              mCalendar.setTimeInMillis(System.currentTimeMillis());
                              final CharSequence time = DateFormat.format(mTimeFormat, mCalendar);
                              final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
                              final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
                  
                              RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
                              views.setTextViewText(R.id.Time, time);
                              views.setTextViewText(R.id.Day, day);
                              views.setTextViewText(R.id.Date, date);
                  
                              ComponentName widget = new ComponentName(this, DigiClock.class);
                              AppWidgetManager manager = AppWidgetManager.getInstance(this);
                              manager.updateAppWidget(widget, views);
                          }
                  
                          private void reinit() {
                              mDayFormat = getString(R.string.day_format);
                              mDateFormat = getString(R.string.date_format);
                              mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS;
                              mCalendar = Calendar.getInstance();
                          }
                  
                          private static boolean is24HourMode(final Context context) {
                              return android.text.format.DateFormat.is24HourFormat(context);
                          }
                  
                          private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() {
                              @Override
                              public void onReceive(Context context, Intent intent) {
                                  final String action = intent.getAction();
                  
                                  if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                                      action.equals(Intent.ACTION_TIMEZONE_CHANGED))
                                  {
                                      reinit();
                                  }
                  
                                  update();
                              }
                          };
                      }
                  }
                  

                  推荐答案

                  需要将字体渲染到画布上,然后将其传递给位图并将其分配给 ImageView.像这样:

                  What is needed is to render the font onto a canvas, and then pass it on to a bitmap and assign that to an ImageView. Like so:

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

                  那是做字体到图像的部分,这就是它的使用方法:

                  That's the part doing the font to image thingie, and this is how to use it:

                  String time = (String) DateFormat.format(mTimeFormat, mCalendar);
                  RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
                  views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));
                  

                  您可能会注意到,此代码仅在图像视图中显示当前时间,但可以根据需要轻松调整.

                  As you might notice, this code just shows the current time in the imageview, but it can easily be adjusted to whatever needs.

                  如文档中所述,ARGB_4444 已被 ARGB_8888 弃用

                  ARGB_4444 is deprecated for ARGB_8888 as stated in the documentation

                  此字段在 API 级别 13 中已弃用.由于此配置的质量较差,建议改用 ARGB_8888.

                  This field was deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead.

                  这篇关于如何在小部件中使用自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                  cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                  Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                  SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                  Android file copy(安卓文件拷贝)
                  Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)

                      • <bdo id='PTJAB'></bdo><ul id='PTJAB'></ul>

                        <small id='PTJAB'></small><noframes id='PTJAB'>

                        <tfoot id='PTJAB'></tfoot>
                          <tbody id='PTJAB'></tbody>
                        <i id='PTJAB'><tr id='PTJAB'><dt id='PTJAB'><q id='PTJAB'><span id='PTJAB'><b id='PTJAB'><form id='PTJAB'><ins id='PTJAB'></ins><ul id='PTJAB'></ul><sub id='PTJAB'></sub></form><legend id='PTJAB'></legend><bdo id='PTJAB'><pre id='PTJAB'><center id='PTJAB'></center></pre></bdo></b><th id='PTJAB'></th></span></q></dt></tr></i><div id='PTJAB'><tfoot id='PTJAB'></tfoot><dl id='PTJAB'><fieldset id='PTJAB'></fieldset></dl></div>
                          <legend id='PTJAB'><style id='PTJAB'><dir id='PTJAB'><q id='PTJAB'></q></dir></style></legend>