论坛首页 移动开发技术论坛

Android 、BlackBerry 文本对齐方式对比

浏览 3744 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-02-22   最后修改:2010-02-22

版权所有,欢迎转载,转载请注明 : SinFrancis  http://mdev.cc

Android和BlackBerry文本对齐方式的写法不一样。

首先Android文本对齐有相对的概念,也就是说当使用文本对齐,你还需要设定相对哪个坐标点才能进行,否则的话就不能达到想要的效果,而BlackBerry则不需要,只要设置对齐方式,就会相对于所画区域的大小自动对齐。

 

Android是使用Paint和Canvas共同控制绘图,而BlackBerry只要Graphics就可以了。

请看Android的代码:

	int imageWidth=200;
		int imageHeight=200;
		Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		
		Paint p = new Paint();
		p.reset();
		p.setColor(Color.GRAY);
		p.setTextSize(24);
		
		p.setAntiAlias(true);//消除锯齿
		p.setTextAlign(Align.CENTER);//居中对齐
		//canvas.drawRect(0, 0, imageWidth, imageHeight, p);
		p.setColor(Color.BLACK);
		canvas.drawText("this is the demo", imageWidth>>1, 20, p);
		p.setTextAlign(Align.RIGHT);//右对齐
		canvas.drawText("this is  demo", imageWidth, 49, p);

 请注意canvas.drawText("this is the demo", imageWidth>>1, 20, p);
这句话的 x的坐标值为 : imageWidth>>1 ,也就是说整个图的中间位置,那么居中对齐的坐标点中心点在此位置

canvas.drawText("this is  demo", imageWidth, 49, p);

这句话则说明居右对齐的点在imageWidth的位置,这都是相对的概念。

 

 

 

下面是BlackBerry的代码:

Bitmap bitmap = new Bitmap(imageWidth, imageHeight);
		Graphics graphics = Graphics.create(bitmap);
		graphics.drawText("This is the demo", 0, 0, Graphics.HCENTER,
				Display.getWidth());

 

 

 graphics.drawText("This is the demo", 0, 0, Graphics.HCENTER,
Display.getWidth());

这段代码是说让文本居中对齐 Graphics.HCENTER,那么Display.getWidth()就是所画的区域宽度。

 

另外BlackBerry的对齐方式还能进行组合使用,比如:

graphics.drawText("This is the demo", 0, 0, Graphics.HCENTER|Graphics.VFULL,
				Display.getWidth());

 

所以Android和BB在文本对齐的方式上有很大的不同。

 

 

 

 

 

 

   发表时间:2010-02-24  
要是有个截图就好了
0 请登录后投票
   发表时间:2010-02-25  
fkpwolf 写道
要是有个截图就好了

本来是想截图的,不过后来觉得还是大家动手写写更好。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics