赞
踩
最近有竖排文本的需求,整理了一些实现方式,最终自定义TextView绘制文本方式满足需求。
参考文档:
https://blog.csdn.net/Blue_Tong/article/details/123376013
英文纵向显示-竖版TextView_windroid之父的博客-CSDN博客
- <TextView
- android:text="我的应用"
- android:textSize="32sp"
- android:ems="1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
效果:显示中文
显示英文:
特点:
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.graphics.Path;
- import android.graphics.Typeface;
- import android.util.AttributeSet;
- import android.widget.TextView;
-
-
- public class VerticalTextView2 extends TextView {
-
- private final static String TAG = "VerticalTextView2 ";
-
- protected int mTextColor = 0xFFFF0000;
- private String mText;
- private Paint mPaint;
- private Rect mBound;//绘制时控制文本绘制的范围
- private Path path;
-
- public VerticalTextView2(Context context) {
- super(context);
- init();
- }
-
- public VerticalTextView2(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
-
- public VerticalTextView2(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
-
- private void init() {
- mPaint = new Paint();//创建画笔
- mText = "我 的 应 用";// 初始化文本
- mPaint.setColor(mTextColor);//初始化画笔颜色
- mPaint.setAntiAlias(true);
- mPaint.setTextSize(32);//初始化文字大小
- mBound = new Rect();//显示区域
- mPaint.getTextBounds(mText, 0, mText.length(), mBound);
- mPaint.clearShadowLayer();
- initDisplay();
- path = new Path();
- path.lineTo(0, 500);
- }
-
- protected void initDisplay() {
- setTypeface(Typeface.SERIF);//风格设置
- }
-
- public boolean isChinese(char str) {
- Character.UnicodeBlock ub = Character.UnicodeBlock.of(str);
- 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) {
- return true;
- }
- return false;
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- float a = mPaint.measureText("正正", 0, 1);
- float left = getWidth() / 2 - a / 2;
- float w;
- final int len = mText.length();
- float py = 0;
- for (int i = 0; i < len; i++) {
- char c = mText.charAt(i);
- w = mPaint.measureText(mText, i, i + 1);//获取字符宽度
-
- StringBuffer b = new StringBuffer();
- b.append(c);
- if (py > getHeight()) {//定义字的范围
- return;
- }
- if (isChinese(c)) {
- py += w;
- if (py > getHeight()) {
- return;
- }
- canvas.drawText(b.toString(), left, py, mPaint); //中文处理方法
- } else {
- canvas.drawTextOnPath(b.toString(), path, py, -left - 2, mPaint);//其他文字处理方法
- py += w;
- }
- }
- }
- }

xml中使用:
java中已经设置了文本,xml中就不再设置文本,否则会显示两个文本,也可以写到自定义属性中
- <com.example.myapplication.VerticalTextView2
- android:id="@+id/vertical_text_2"
- android:textSize="32dp" />
效果:显示中文
显示英文:
特点:
实现:
- package com.example.myapplication;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.text.TextUtils;
- import android.util.AttributeSet;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.TextView;
-
-
- public class VerticalTextView1 extends TextView {
-
- private static final String TAG = "VerticalTextView1";
- final boolean topDown;
-
- public VerticalTextView1(Context context, AttributeSet attrs) {
- super(context, attrs);
- final int gravity = getGravity();
- if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
- setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
- topDown = false;
- } else
- topDown = true;
- setEllipsize(TextUtils.TruncateAt.MARQUEE);
- setMarqueeRepeatLimit(-1);
- setFocusable(true);
- setFocusableInTouchMode(true);
- setSingleLine();
- setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(heightMeasureSpec, widthMeasureSpec);
- setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
- }
-
- @Override
- protected boolean setFrame(int l, int t, int r, int b) {
- return super.setFrame(l, t, l + (b - t), t + (r - l));
- }
-
- @Override
- public void onDraw(Canvas canvas) {
- if (topDown) {
- canvas.translate(getHeight(), 0);
- canvas.rotate(90);
- } else {
- canvas.translate(0, getWidth());
- canvas.rotate(-90);
- }
- canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
- super.onDraw(canvas);
- }
- }

效果:显示中文:
显示英文:
特点:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。