Android(安卓)如何实现竖排文字显示?
16lz
2022-07-24
http://blog.csdn.net/gf771115/article/details/31354265
http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265
Android 如何实现竖排文字显示?
分类:Android/OMS 2014-06-16 10:41 78人阅读 评论(0) 收藏 举报转自:http://blog.csdn.net/liaoyp_ios_android/article/details/6949768
- 在android.graphics.Canvas类中有个沿路径画字的方法
- voiddrawTextOnPath(Stringtext,Pathpath,floathOffset,floatvOffset,Paintpaint)
- Drawthetext,withoriginat(x,y),usingthespecifiedpaint,alongthespecifiedpath.
- voiddrawTextOnPath(char[]text,intindex,intcount,Pathpath,floathOffset,floatvOffset,Paintpaint)
- Drawthetext,withoriginat(x,y),usingthespecifiedpaint,alongthespecifiedpath.
- Test.java代码://需要在layout中定义Test,且设置背景,在java代码中设置testText
- publicclassTestextendsView{
- privatePaintpaint;
- privatePathpath;
- privateMatrixmatrix;
- privateintwidth=-1;
- privateintheight=-1;
- privatefloatleft=3;
- privatefloattop=18;
- privateStringtitle="";
- BitmapDrawabledrawable=(BitmapDrawable)getBackground();
- publicTest(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- paint=newPaint();
- paint.setColor(Color.WHITE);//定义字体颜色
- paint.setTextSize(14);//定义字体大小
- path=newPath();
- path.lineTo(0,500);//定义字符路径
- matrix=newMatrix();
- Log.v("onMeasure","2");
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- //画背景
- Bitmapb=Bitmap.createBitmap(drawable.getBitmap(),0,0,width,height);
- canvas.drawBitmap(b,matrix,paint);
- //画字
- showText(canvas,title);
- }
- privatevoidshowText(Canvascanvas,Stringtext){
- floatw;
- finalintlen=text.length();
- floatpy=0+top;
- for(inti=0;i<len;i++){
- charc=text.charAt(i);
- w=paint.measureText(text,i,i+1);//获取字符宽度
- StringBufferb=newStringBuffer();
- b.append(c);
- if(py>81){//定义字的范围
- return;
- }
- if(isChinese(c)){
- py+=w;
- if(py>81){
- return;
- }
- canvas.drawText(b.toString(),left,py,paint);//中文处理方法
- }else{
- canvas.drawTextOnPath(b.toString(),path,py,-left-2,paint);//其他文字处理方法
- py+=w;
- }
- }
- }
- publicvoidsetText(Stringtitle){
- this.title=title;
- }
- publicStringgetText(){
- returntitle;
- }
- privatebooleanisChinese(charc){
- Character.UnicodeBlockub=Character.UnicodeBlock.of(c);
- if(ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
- ||ub==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
- ||ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
- ||ub==Character.UnicodeBlock.GENERAL_PUNCTUATION
- ||ub==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
- ||ub==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
- returntrue;
- }
- returnfalse;
- }
- //重写View大小方法,使view大小为背景图片大小
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- //super.onMeasure(widthMeasureSpec,heightMeasureSpec);
- if(null!=getBackground()){
- inth=drawable.getIntrinsicHeight();
- intw=drawable.getIntrinsicWidth();
- Log.v("onMeasure","null!=getBackground()h:"+h+"w:"+w);
- width=w;
- height=h;
- setMeasuredDimension(w,h);
- }else{
- width=widthMeasureSpec;
- height=heightMeasureSpec;
- super.measure(widthMeasureSpec,heightMeasureSpec);
- }
- }
- }
- 在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖setRequestedOrientation()方法,而若要取得目前的屏幕方向,则需要访问getRequestedOrientation()方法。本范例为求简要示范更改做法,设计了一个按钮,当单击按钮的同时,判断当下的屏幕方向,例如竖排(PORTRAIT),则将其更改为横排(LANDSCAPE);若为横排(LANDSCAPE),则将其更改为竖排(PORTRAIT)
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android上一些方法的区别和用法的注意事项
- android实现字体闪烁动画的方法
- Android中dispatchDraw分析
- Android四大基本组件介绍与生命周期
- Android(安卓)MediaPlayer 常用方法介绍
- 在Fragment中设置控件点击方法,执行失败。