(转)Android 、BlackBerry 文本对齐方式对比

本文详细解析了Android和BlackBerry在文本对齐方式上的差异,包括使用Paint和Canvas与Graphics的区别,以及如何实现居中对齐和右对齐。通过对比代码示例,展示了两者在实现文本对齐时的不同概念与方法。

http://tech.cncms.com/shouji/android/77024_2.html

 

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

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

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

请看Android的代码:

Java代码

 

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);

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的位置,这都是相对的概念,Android BlackBerry 文本对齐方式对比(2),下面是BlackBerry的代码:Java代码

 

Bitmap bitmap =  new Bitmap(imageWidth, imageHeight);

Graphics graphics = Graphics.create(bitmap);

graphics.drawText("This is the demo", 0, 0, Graphics.HCENTER,

Display.getWidth());

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的对齐方式还能进行组合使用,比如:

Java代码

graphics.drawText("This is the demo", 0, 0,  Graphics.HCENTER|Graphics.VFULL,

Display.getWidth());

graphics.drawText("This is the demo", 0, 0,  Graphics.HCENTER|Graphics.VFULL,

Display.getWidth());

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

 




 

Bitmap bitmap = new Bitmap(imageWidth, imageHeight);

Graphics graphics = Graphics.create(bitmap);

graphics.drawText("This is the demo", 0, 0, Graphics.HCENTER,

Display.getWidth());

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的对齐方式还能进行组合使用,比如:

Java代码

graphics.drawText("This is the demo", 0, 0,  Graphics.HCENTER|Graphics.VFULL,

Display.getWidth());

graphics.drawText("This is the demo", 0, 0,  Graphics.HCENTER|Graphics.VFULL,

Display.getWidth());

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

-

转载于:https://www.cnblogs.com/Jessy/archive/2012/02/22/2363526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值