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;
}