2015-10-19 47 views
2

這是我的課:如何在View類中打開自定義字體? O_0

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的第一。需要以確定到底哪裏錯誤。 我假設它的NullpointerassetMgr

更改字體名稱O157CbnU

  1. 使用更好的方法小信

字樣平原= Typeface.createFromAsset(的getContext()getAssets(), 「字體/ Your.ttf」。);

+1

Your.TTF - > Your.ttf - 救救我吧!謝謝! 這是我在Java上的第一個應用程序(我認爲是最後一個)。 「發佈您的logcat第一」 - 我有一個與BT連接的服務,所以我運行我的應用程序在我的平板電腦..沒有Gentmotion BT模擬(開始出現錯誤)。 –

+0

沒問題。很高興幫助。向前移動 –

+1

將'plain'的實例化作爲類字段移動到CustomDisplayView的構造函數可能會更好,因此它可能會被onDraw重用(而不是每次創建onDraw都被調用) – petey