开发者

Write a text in the pre-define path

I would like to write text along a path shaped like a capital Z letter.

I wrote the following code. It draws the text along a rectangle.

What do I need to do to create a Z-shaped path and have the text follow it?

protected void onCreate(Bundle savedInstanceState) 
      {
        super.onCreate(savedInstanceState);
        Display display = getWindowManager().getDefaultDisplay();
        setContentView(new SampleView(this,display.getWidth(),display.getHeight()));
      }

      private static class SampleView extends View 
      {
        int width;
        int height;
        public SampleView(Context context,int w,int h) 
        {
          super(context);
          width=w;
          height=h;
        }

        @Override
        protected void onDraw(Canvas canvas) 
        { 
            Path mypath = new Path();

            mypath.addRect(width/2,height/2,width,height,Path.Direction.CCW);

            // set the color and font size
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setTextSize(30);
            paint.setAntiAlias(true);

            // draw the text along the circle
            canvas.drawTextOnPath("Hi This Is Sample String",mypath, 0, 30, paint);
        }
      }
开发者_StackOverflow中文版    }


You have to use a canvas object. With FontMetrics you can calculate a rectangle around your text which helps you to arrange the text better at your "path".

Canvas: http://developer.android.com/reference/android/graphics/Canvas.html

FontMetrics: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/FontMetrics.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜