« Android アプリストア まとめ | メイン | 抜き色が倒せない »

2011年01月25日

Android:: 袋文字の描画

    

// 袋文字描画
// @param canvas 描画対象キャンバス
// @param text 描画文字列
// @param start 描画文字列中の描画開始文字位置
// @param end 描画文字列中の描画終了文字位置
// @param x 文字描画位置X座標値
// @param y 文字描画位置Y座標値
// @param color 描画文字色
// @param bgcolor 縁取り色
// @param width 縁取り太さ
// @param size フォントサイズ
private void drawOutlineText( Canvas canvas, String text, int start, int end, int x, int y, int color, int bgcolor, int width, int size ) {
    // これらは毎回生成せずに使い回した方がよい
    Paint paint = new Paint();
    Path textPath = new Path();

    paint.setAntiAlias( true );
    paint.setTextSize( size );
    paint.setTextAlign( Paint.Align.LEFT );

    // まずは後ろの影を書く
    // テキストのパスを取得する
    paint.getTextPath( text, start, end, x, y, textPath );
    paint.setColor( bgcolor );
    // ストロークで文字の縁を width 幅で描画
    paint.setStyle( Paint.Style.STROKE );
    paint.setStrokeWidth( width );
    canvas.drawPath( textPath, paint );

    // 文字を書く
    paint.setColor( color );
    paint.setStyle( Paint.Style.FILL );
    canvas.drawText( text, start, end, x, y, paint );
}


他の方法があるかもしれないが、とりあえず上記の方法で描画出来る。
ただ、少し重いのでゲーム等のリアルタイム性のあるアプリでは文字のキャッシングなどの必要がある。
TextView では影付き文字が描画出来るが、袋文字はない。
影の指定次第で近いものは出来るが、縁がブラーをかけたようなものになるので、視認性で劣る。
上記関数はあくまでもサンプルコード。



投稿者 Takenori : 2011年01月25日 19:24




comments powered by Disqus
Total : Today : Yesterday : なかのひと