自定义View Paint设置Typeface(字型)

一.简介

Typeface(字型),由字义,我们大概可以猜到,这个 API是用来设置字体以及字体风格的,使用起来也非常的简单。

 

 

 

 

 

二.具体实现

 

View实现类

public class MyView extends View{


    private Paint mPaint1,mPaint2,mPaint3,mPaint4,mPaint5;
    private Context mContext;


    public MyView(Context context) {
        this(context,null);
    }


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }


    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void init(){
        mPaint1 = new Paint();
        mPaint2 = new Paint();
        mPaint3 = new Paint();
        mPaint4 = new Paint();
        mPaint5 = new Paint();


        mPaint1.setColor(Color.RED);
        mPaint2.setColor(Color.BLUE);
        mPaint3.setColor(Color.BLACK);
        mPaint4.setColor(Color.YELLOW);
        mPaint5.setColor(Color.GRAY);




        mPaint1.setTextSize(30);
        mPaint2.setTextSize(30);
        mPaint3.setTextSize(30);
        mPaint4.setTextSize(30);
        mPaint5.setTextSize(30);




        mPaint1.setTypeface(Typeface.DEFAULT_BOLD);
        mPaint2.setTypeface(Typeface.MONOSPACE);
        mPaint3.setTypeface(Typeface.SANS_SERIF);
        mPaint4.setTypeface(Typeface.SERIF);
        mPaint5.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "font/FuturaCondensed.ttf"));


    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText("Typeface设置字体", 100, 100, mPaint1);
        canvas.drawText("Typeface设置字体", 100, 200, mPaint2);
        canvas.drawText("Typeface设置字体", 100, 300, mPaint3);
        canvas.drawText("Typeface设置字体", 100, 400, mPaint4);
        canvas.drawText("Typeface设置字体", 100, 500, mPaint5);
    }


}

 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.wjn.sqlitedemo.view.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

 

效果

 

 

mPaint5.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "font/FuturaCondensed.ttf")); 使用自定的字体

 

 

可选字体对象

 

 

字体的可选风格

 

 

 

Android TextView设置字体风格:Android开发之动态设置字体的样式和粗细

 

 

附:Typeface官方链接

http://androiddoc.qiniudn.com/reference/android/graphics/Typeface.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值