<!-- 使用默认的sans字体--> <TextView android:id="@+id/sans" android:text="Hello,World" android:textSize="20sp" android:typeface="sans" /> <!-- 使用默认的serifs字体--> <TextView android:id="@+id/serif" android:text="Hello,World" android:textSize="20sp" android:typeface="serif" /> <!-- 使用默认的monospace字体--> <TextView android:id="@+id/monospace" android:text="Hello,World" android:textSize="20sp" android:typeface="monospace" />
//获取textView实例 TextView textView = findViewById(R.id.textview);
//设置serif字体 textView.setTypeface(Typeface.SERIF); //设置sans字体 textView.setTypeface(Typeface.SANS_SERIF); //设置monospace字体 textView.setTypeface(Typeface.MONOSPACE);
//实例化TextView TextView textView = findViewById(R.id.textview); //得到AssetManager AssetManager mgr=getAssets(); //根据路径得到Typeface Typeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf"); //设置字体 textView.setTypeface(tf);
TextPaint paint = outlineTextView.getPaint(); //实例化TextPaint对象 paint.setStrokeWidth(15); //设置描边的宽度 paint.setStyle(Paint.Style.STROKE);//设置画笔属性为描边 strokeTextView.setTextColor(Color.parseColor(“#000000”)); //设置描边的颜色(不能与文本颜色一致)
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;
public StrokeTextView(Context context) {
super(context);
outlineTextView = new TextView(context);
init();
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
outlineTextView = new TextView(context, attrs);
init();
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
outlineTextView = new TextView(context, attrs, defStyle);
init();
}
public void init() {
TextPaint paint = outlineTextView.getPaint();
paint.setStrokeWidth(3); //描边宽度
paint.setStyle(Style.STROKE);
outlineTextView.setTextColor(Color.parseColor("#000000")); //描边颜色
outlineTextView.setGravity(getGravity());
}
@Override
public void setLayoutParams (ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
outlineTextView.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//设置轮廓文字
CharSequence outlineText = outlineTextView.getText();
if (outlineText == null || !outlineText.equals(this.getText())) {
outlineTextView.setText(getText());
postInvalidate();
}
outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
outlineTextView.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
outlineTextView.draw(canvas);
super.onDraw(canvas);
}
}
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;
private TextPaint strokePaint;
public StrokeTextView(Context context) {
super(context);
outlineTextView = new TextView(context);
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
outlineTextView = new TextView(context, attrs);
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
outlineTextView = new TextView(context, attrs, defStyle);
}
@Override
public void setLayoutParams (ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
outlineTextView.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
AssetManager manager = context.getAssets();
String path = "fonts/new_text.ttf";
Typeface type = Typeface.createFromAsset(manager, path);
//设置轮廓文字
CharSequence outlineText = outlineTextView.getText();
if (outlineText == null || !outlineText.equals(this.getText())) {
outlineTextView.setText(getText());
outlineTextView.setTypeface(type);
setTypeface(type);
postInvalidate();
}
outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
outlineTextView.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
AssetManager manager = context.getAssets();
String path = "fonts/new_text.ttf";
Typeface type = Typeface.createFromAsset(manager, path);
if (strokePaint == null) {
strokePaint = new TextPaint();
}
//复制原来TextViewg画笔中的一些参数
TextPaint paint = getPaint();
strokePaint.setTextSize(paint.getTextSize());
strokePaint.setTypeface(type);
strokePaint.setFlags(paint.getFlags());
strokePaint.setAlpha(paint.getAlpha());
//自定义描边效果
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.parseColor("#000000"));
strokePaint.setStrokeWidth(4);
String text = getText().toString();
//在文本底层画出带描边的文本
canvas.drawText(text, (getWidth() - strokePaint.measureText(text)) / 2,
getBaseline(), strokePaint);
super.onDraw(canvas);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有