Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android字体中的数字有不正确的measureText_Android_Graphics_Fonts_Fontmetrics - Fatal编程技术网

android字体中的数字有不正确的measureText

android字体中的数字有不正确的measureText,android,graphics,fonts,fontmetrics,Android,Graphics,Fonts,Fontmetrics,measureText()使用画布中的默认字体为“0”返回与为“1”返回的值相同的值。有没有合适的字体 程序的输出: 12-15 22:36:53.572 28453-28453/acme.measure I/System.out: character: '0' has text bounds: Rect(5, -68 - 48, 1), or 43x69, measure: 53.0 12-15 22:36:53.573 28453-28453/acme.measure I/System.ou

measureText()使用画布中的默认字体为“0”返回与为“1”返回的值相同的值。有没有合适的字体

程序的输出:

12-15 22:36:53.572 28453-28453/acme.measure I/System.out: character: '0' has text bounds: Rect(5, -68 - 48, 1), or 43x69, measure: 53.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out: character: '1' has text bounds: Rect(7, -67 - 34, 0), or 27x67, measure: 53.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out: character: 'A' has text bounds: Rect(1, -68 - 61, 0), or 60x68, measure: 62.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out: character: 'I' has text bounds: Rect(8, -67 - 17, 0), or 9x67, measure: 25.0
12-15 22:36:53.574 28453-28453/acme.measure I/System.out: 01AI bounds are: Rect(5, -68 - 185, 1), or 180x69

package acme.measure;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.graphics.drawable.shapes.*;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;

import static java.lang.Math.round;
class DrawableView extends View {
    public DrawableView(Context context) {
        super(context);
        setBackgroundColor(Color.TRANSPARENT);
        size=100;
        text="01AI";
        shapeDrawable=new ShapeDrawable(new RectShape());
        shapeDrawable.getPaint().setColor(0xff000000);
        shapeDrawable.setBounds(0,0,text.length()*size,size);
        shapeDrawable.getPaint().setColor(0xffffffff);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        shapeDrawable.draw(canvas);
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(size*95/100);
        Rect r=new Rect();
        for(int i=0;i<text.length();i++) {
            paint.getTextBounds(text,i,i+1,r);
            Paint.FontMetrics fontMetrics=paint.getFontMetrics();
            System.out.println("character: '"+text.charAt(i)+"' has text bounds: "+r+", or "+r.width()+"x"+r.height()+", measure: "+paint.measureText(text,i,i+1));
        }
        paint.getTextBounds(text,0,text.length(),r);
        System.out.println(text+" bounds are: "+r+", or "+r.width()+"x"+r.height());
        int x=(text.length()*size-r.width())/2, y=(size-r.height())/2;
        canvas.drawText(text,x,size-y,paint);
    }
    final int size;
    final String text;
    final ShapeDrawable shapeDrawable;
}
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        drawableView=new DrawableView(this);
        layout.addView(drawableView);
        setContentView(layout);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();
        if(id==R.id.action_settings)
            return true;
        return super.onOptionsItemSelected(item);
    }
    DrawableView drawableView;
}
12-15 22:36:53.572 28453-28453/acme.measure I/System.out:字符:“0”具有文本边界:Rect(5,-68-48,1)或43x69,measure:53.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out:字符:“1”具有文本边界:Rect(7,-67-34,0)或27x67,measure:53.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out:字符:“A”具有文本边界:Rect(1,-68-61,0)或60x68,度量值:62.0
12-15 22:36:53.573 28453-28453/acme.measure I/System.out:字符:“I”具有文本边界:Rect(8,-67-17,0)或9x67,measure:25.0
12-15 22:36:53.574 28453-28453/acme.measure I/System.out:01AI边界为:Rect(5,-68-185,1)或180x69
包装acme.measure;
导入android.content.*;
导入android.graphics.*;
导入android.graphics.drawable.*;
导入android.graphics.drawable.shapes.*;
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.view;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.*;
导入静态java.lang.Math.round;
类DrawableView扩展了视图{
公共DrawableView(上下文){
超级(上下文);
setBackgroundColor(颜色:透明);
尺寸=100;
text=“01AI”;
shapeDrawable=新的shapeDrawable(新的RectShape());
shapeDrawable.getPaint().setColor(0xff000000);
shapeDrawable.setBounds(0,0,text.length()*size,size);
shapeDrawable.getPaint().setColor(0xffffffff);
}
@凌驾
受保护的void onDraw(画布){
可成型的可拆卸。绘制(画布);
油漆=新油漆();
绘制.设置样式(绘制.样式.填充);
油漆。setTextSize(尺寸*95/100);
Rect r=新的Rect();

对于(int i=0;i logcat说什么?getTextBounds返回什么?这很奇怪:显然,
getTextBounds
给出了更好的结果,但是对于“”(空格),它返回
Rect(0,0-0,0)
这是一个很深的谜。。。