Android—TTF字体

Android中不支持layout的xml 直接使用TTF等自定义字体  

自定义字体必须放到asset目录下,需要调用context.getAssets()方法获取自定义字体的资源,由于android Widget是依赖于其他进程存在,故widget中无法使用字体设置的.setTypeface方法,因此在Widget中使用自定义字体,可以将字体转换为图片输出以后,用views.setImageViewBitmap方法可实现自定义功能,参考代码: 

static Bitmap buildUpdate(String time, Context context){     
        Bitmap myBitmap = Bitmap.createBitmap(240, 80, Bitmap.Config.ARGB_4444); 
        Canvas myCanvas = new Canvas(myBitmap); 
        Paint paint = new Paint(); 
        Typeface tf= Typeface.createFromAsset(context.getAssets(),"fonts/Clockopia.ttf"); 
        paint.setAntiAlias(true); 
        paint.setAlpha(110);//取值范围为0~255,值越小越透明 
        paint.setSubpixelText(true); 
        paint.setTypeface(tf); 
        paint.setStyle(Paint.Style.FILL); 
        paint.setColor(Color.WHITE); 
        paint.setTextSize(80); 
        paint.setTextAlign(Align.CENTER); 
        myCanvas.drawText(time, 100, 60, paint);         
        return myBitmap; 
    } 

//widget中调用     制作显示时间的桌面widget时调用
RemoteViews mViews = new RemoteViews(context.getPackageName(), R.layout.main); 
mViews.setImageViewBitmap(R.id.imageView1, buildUpdate(time,context)); 


PS:因为使用了画笔和图像来实现字体,所以布局时要用<ImageView>。 

 

PPS: 
widget 仅支持简单组件,比如ImageView,textvew,不支持高级复杂的组件,比如listview,Edittext。但是widget可以点击触发含有任何组件的activity。具体步骤是: 

[html]   代码需要放置在onUpdate( )方法中执行

  RemoteViews(context.getPackageName(),R.layout.main); 
 Intent intent = new Intent(context,被调用activity.class); 
 PendingIntent pend = PendingInent.getActivity(context,0,intent,0); 
 mRemoteViews.setOnClickPendingIntent(R.id.widget,pend);。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值