问题描述
这是我的班级:
package com.adaptto.myapplication;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import java.io.IOException;
/*e the main app Activity
*
* The view also refers to attributes specified in the app res/values/attrs XML
*/
public class CustomDisplayView extends View {
//circle and text colors
private int circleCol, labelCol;
private int circleCol2, labelCol2;
//label text
private String circleText;
//paint for drawing custom view
private Paint circlePaint;
AssetManager assetMgr = getResources().getAssets();
/**
* Constructor method for custom view
* - calls superclass method and adds custom processing
*
* @param context
* @param attrs
*/
public CustomDisplayView(Context context, AttributeSet attrs){
super(context, attrs);
//paint object for drawing in onDraw
circlePaint = new Paint();
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomDisplayView, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
circleText = a.getString(R.styleable.CustomDisplayView_circleLabel);
circleCol = a.getInteger(R.styleable.CustomDisplayView_circleColor, 0);//0 is default
labelCol = a.getInteger(R.styleable.CustomDisplayView_labelColor, 0);
circleCol2 = a.getInteger(R.styleable.CustomDisplayView_circleColor2, 0);//0 is default
} finally {
a.recycle();
}
}
/**
* Override the onDraw method to specify custom view appearance using canvas
*/
@Override
protected void onDraw(Canvas canvas) {
//get half of the width and height as we are working with a circle
int viewWidthHalf = this.getMeasuredWidth()/2;
int viewHeightHalf = this.getMeasuredHeight()/2;
int viewWidthHalfS = this.getMeasuredWidth()/3;
int viewHeightHalfS = this.getMeasuredHeight()/3;
//get the radius as half of the width or height, whichever is smaller
//subtract ten so that it has some space around it
int radius = 0;
int radius2 = 0;
if(viewWidthHalf>viewHeightHalf)
{
radius=viewHeightHalf-10;
}
else
{
radius=viewWidthHalf-10;
}
radius2=radius/2;
//set drawing properties
circlePaint.setStyle(Style.FILL);
circlePaint.setAntiAlias(true);
//set the paint color using the circle color specified
circlePaint.setColor(circleCol);
//draw the circle using the properties defined
canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
//set the paint color using the circle color specified
circlePaint.setColor(circleCol2);
canvas.drawCircle(viewWidthHalfS, viewHeightHalfS, radius2, circlePaint);
//set the text color using the color specified
circlePaint.setColor(labelCol);
//set text properties
circlePaint.setTextAlign(Paint.Align.CENTER);
circlePaint.setTextSize(10);
Typeface plain = Typeface.createFromAsset(assetMgr, "fonts/O157CbnU.TTF");
//circlePaint.setTypeface(plain);
canvas.drawText("Sample text in bold Helvetica", viewWidthHalfS, viewHeightHalfS,circlePaint);
circlePaint.setTextSize(40);
//draw the text using the string attribute and chosen properties
canvas.drawText(circleText, viewWidthHalf, viewHeightHalf, circlePaint);
}
//each custom attribute should have a get and set method
//this allows updating these properties in Java
//we call these in the main Activity class
/**
* Get the current circle color
* @return color as an int
*/
public int getCircleColor(){
return circleCol;
}
/**
* Set the circle color
* @param newColor new color as an int
*/
public void setCircleColor(int newColor){
//update the instance variable
circleCol=newColor;
//redraw the view
invalidate();
requestLayout();
}
/**
* Get the current text label color
* @return color as an int
*/
public int getLabelColor(){
return labelCol;
}
/**
* Set the label color
* @param newColor new color as an int
*/
public void setLabelColor(int newColor){
//update the instance variable
labelCol=newColor;
//redraw the view
invalidate();
requestLayout();
}
/**
* Get the current label text
* @return text as a string
*/
public String getLabelText(){
return circleText;
}
/**
* Set the label text
* @param newLabel text as a string
*/
public void setLabelText(String newLabel){
//update the instance variable
circleText=newLabel;
//redraw the view
invalidate();
requestLayout();
}
}
}
如果这个字符串没有被注释:
//Typeface plain = Typeface.createFromAsset(assetMgr, "fonts/O157CbnU.TTF");
应用程序崩溃...
1楼
首先发布您的logcat 。需要确定错误的确切位置。
我假设它的Nullpointer
为assetMgr
。
更改字体名称O157CbnU
。
- 使用小写字母以获得更好的方法
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "fonts/Your.ttf");