androidpaint字體
Ⅰ android 用paint寫字,請問有什麼辦法能設置 字間距嗎
這個方法就可以,那個1.3f就是字間距,可以調節
public static void getText(Canvas canvas,String str,float x,float y){
final TextPaint paint1 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint1.setColor(Color.WHITE);
paint1.setAntiAlias(true);
paint1.setTextSize(23);
paint1.setTextAlign(Align.LEFT);
final StaticLayout layout = new StaticLayout(
str, paint1,
(int) (MySurfaceView.screenW * 0.8),
Layout.Alignment.ALIGN_NORMAL, 1.3f, 0, true);
// layout是默認畫在Canvas的(0,0)點的,如果需要調整位置只能在draw之前移Canvas的起始坐標
canvas.translate(x, y);
layout.draw(canvas);
}
Ⅱ android 怎樣在代碼中給widget設置字體
android給控制項widget設置自定義字體的方式如下:
自定義字體必須放到asset目錄下,需要調用context.getAssets()方法獲取自定義字體的資源,由於android Widget是依賴於其他進程存在,故widget中無法使用字體設置的.setTypeface方法,因此在Widget中使用自定義字體,可以將字體轉換為圖片輸出以後,用views.setImageViewBitmap方法可實現自定義功能,參考代碼:
java">staticBitmapbuildUpdate(Stringtime,Contextcontext){
BitmapmyBitmap=Bitmap.createBitmap(240,80,Bitmap.Config.ARGB_4444);
CanvasmyCanvas=newCanvas(myBitmap);
Paintpaint=newPaint();
Typefacetf=Typeface.createFromAsset(context.getAssets(),"fonts/Clockopia.ttf");
paint.setAntiAlias(true);
paint.setAlpha(110);//取值范圍為0~255,值越小越透明
paint.setSubpixelText(true);
paint.setTypeface(tf);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(80);
paint.setTextAlign(Align.CENTER);
myCanvas.drawText(time,100,60,paint);
returnmyBitmap;
}
//widget中調用
RemoteViewsmViews=newRemoteViews(context.getPackageName(),R.layout.main);
mViews.setImageViewBitmap(R.id.imageView1,buildUpdate(time,context));
Ⅲ android canvas的drawText方法 如何設置字體大小和格式。
Canvas相當於畫布,字體的大小格式在Paint上設置才正確, Paint 相當於畫筆。代碼如下,沒有具體參數:
Paint paint = new Paint();
paint.setTextSize(textSize);//設置字體大小
paint.setTypeface(typeface);//設置字體類型
canvas.drawText(text, x, y, paint);//使用畫筆paint
@Override
public void onDraw (Canvas canvas) {
Rect targetRect = new Rect(50, 50, 1000, 200);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(3);
paint.setTextSize(80);
String testString = "測試:ijkJQKA:1234";
paint.setColor(Color.CYAN);
canvas.drawRect(targetRect, paint);
paint.setColor(Color.RED);
FontMetricsInt fontMetrics = paint.getFontMetricsInt();
(3)androidpaint字體擴展閱讀:
Screen Space - Camera
此模式類似Screen Space - Overlay,但區別是此模式將Canvas放置於某個Camera前固定距離。此Camera負責渲染所有UI元素,則攝像機參數(Camera Settings)直接影響UI表現。
比如Camera是透視模式(Perspective),則UI元素會基於Field of View的值而扭曲變形。同樣的,若屏幕解析度變更,或者視覺平截體(CameraFrustrum)改變,則Canvas自動調整自身尺寸作自適應。
Ⅳ Android 如何實現豎排文字顯示
在android.graphics.Canvas類中有個沿路徑畫字的方法
void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along the specified path.
void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along the specified path.
Test.java代碼://需要在layout中定義Test,且設置背景,在java代碼中設置test Text
public class Test extends View {
private Paint paint;
private Path path;
private Matrix matrix;
private int width = -1;
private int height = -1;
private float left = 3;
private float top = 18;
private String title = "";
BitmapDrawable drawable = (BitmapDrawable) getBackground();
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.WHITE);//定義字體顏色
paint.setTextSize(14);//定義字體大小
path = new Path();
path.lineTo(0,500);//定義字元路徑
matrix = new Matrix();
Log.v("onMeasure", "2");
}
@Override
protected void onDraw(Canvas canvas) {
//畫背景
Bitmap b = Bitmap.createBitmap(drawable.getBitmap(),0,0,width,height);
canvas.drawBitmap(b, matrix, paint);
//畫字
showText(canvas, title);
}
private void showText(Canvas canvas, String text){
float w;
final int len = text.length();
float py = 0 + top;
for(int i=0; i<len; i ++){
char c = text.charAt(i);
w = paint.measureText(text, i, i+1);//獲取字元寬度
StringBuffer b = new StringBuffer();
b.append(c);
if(py > 81){//定義字的范圍
return;
}
if(isChinese(c)){
py += w;
if(py > 81){
return;
}
canvas.drawText(b.toString(), left, py, paint); //中文處理方法
}else {
canvas.drawTextOnPath(b.toString(), path, py, -left-2, paint);//其他文字處理方法
py += w;
}
}
}
public void setText(String title){
this.title = title;
}
public String getText(){
return title;
}
private boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
//重寫View大小方法,使view大小為背景圖片大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (null != getBackground()) {
int h = drawable.getIntrinsicHeight();
int w = drawable.getIntrinsicWidth();
Log.v("onMeasure", "null != getBackground() h:" + h + " w:" + w);
width = w;
height = h;
setMeasuredDimension(w, h);
} else {
width = widthMeasureSpec;
height = heightMeasureSpec;
super.measure(widthMeasureSpec, heightMeasureSpec);
}
}
}
在Android中,若要通過程序改變屏幕顯示的方向,必須要覆蓋setRequestedOrientation()方法,而若要取得目前的屏幕方向,則需要訪問getRequestedOrientation()方法。本範例為求簡要示範更改做法,設計了一個按鈕,當單擊按鈕的同時,判斷當下的屏幕方向,例如豎排(PORTRAIT),則將其更改為橫排(LANDSCAPE);若為橫排(LANDSCAPE),則將其更改為豎排(PORTRAIT)
Ⅳ Android canvas 如何繪制字體大小為25的一行文本
Canvas相當於畫布,字體的大小格式在Paint上設置才正確, Paint 相當於畫筆。代碼如下,沒有具體參數:希望能幫到你 Paint paint = new Paint(); paint.setTextSize(textSize);//設置字體大小 paint.setTypeface(typeface);//設置字體類型
Ⅵ Android中如何讓字體旋轉
自定義View, 繼承Textview ,在onDraw 函數調用super方法之前 使用 canvas.translate 進行旋轉操作。
封裝一下就可以外部設置旋轉角度了。