gpt4 book ai didi

android - 如何在 View 类中打开自定义字体? O_0

转载 作者:行者123 更新时间:2023-11-29 01:27:22 25 4
gpt4 key购买 nike

这是我的类(class):

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");

应用程序崩溃...

最佳答案

先发布你的logcat。需要确定错误的确切位置。我假设它的 Nullpointer 用于 assetMgr

更改字体名称 O157CbnU

  1. 使用小写字母以获得更好的方法

Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "fonts/Your.ttf");

关于android - 如何在 View 类中打开自定义字体? O_0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33217999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com