在以下代码中:
Paint tPaint = new Paint(aPaint);
tPaint.setTypeface(typeFace);
aPaint = new Paint(tPaint);
观察到 typeFace 设置为 tPaint,但是 typeFace 没有被复制到 aPaint 中。
这不完全是一个答案,而是一种解决方法。
我替换了: aPaint = new Paint(tPaint); 与:aPaint = newPaint(tPaint); 并创建了方法:
public Paint newPaint(Paint fromPaint){
Typeface tf = fromPaint.getTypeface();
Paint rPaint = new Paint(fromPaint);
rPaint.setTypeface(tf);
return rPaint;
}
这行得通。
好的,这就是我的代码的样子。看看能不能找到什么。
主视图.java
public class MainView extends View {
Context context;
public MainView(Context context) {
super(context);
this.context = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint apaint = new Paint();
apaint.setTextSize(100);
apaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/sketchblock.ttf"));
canvas.drawText("Dusean", canvas.getWidth()/2, canvas.getHeight()/2, apaint);
Paint tpaint = new Paint(apaint);
canvas.drawText("Dusean", canvas.getWidth()/3, canvas.getHeight()/3, tpaint);
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainView(this));
}
}
和资产文件夹assets/fonts/font.ttf中的字体