2012-04-25 5 views
0

私のレイアウトの中にはフルスクリーンのテキストを描きたいのですが、私は正確なポイントを得ることができます。ここに私がコード化したものがあります:キャンバスでスクリーンコーナーに描く

DisplayMetrics display = getResources().getDisplayMetrics(); 
        int width = display.widthPixels; 
        String data_azi = DateFormat.getDateTimeInstance().format(new Date()); 
        cnv.drawText(data_azi, width - p.measureText(data_azi), 40,p); 

答えて

1

あなたは先を測定し、その測定を描画時間中に行う必要があります。たとえば、起動時にmeasureText()に電話をかけることができます。このメソッドは、現在の日付の幅を測定し、測定された幅に20%を加算します。

private final Rect mBounds = new Rect(); 
private final Date mDate = new Date(); 


private void measureText(final Paint p){ 
    final String dummy = DateFormat.getDateTimeInstance().format(mDate); 
    p.getTextBounds(dummy, 0, dummy.length(), mBounds); 
    final int inset = (int) (mBounds.width()*0.1f); //add safety margin 
    mBounds.inset(-inset, 0); 
} 

次に、あなたは次のように描く:

mDate.setTime(System.currentTimeMillis()); 
String data_azi = DateFormat.getDateTimeInstance().format(mDate); 
cnv.drawText(data_azi, width - mBounds.width(), mBounds.height(),p); 

また、あなたのペイントオブジェクトがp.setTextAlign(Align.LEFT);

を揃え残されていることを確認してください
関連する問題