android TextView实现文字字符不同方向显示

实现方案:通过TextView的rotation属性将文字或者字符进行任意角度旋转

  1. 判断是否是中文
java 复制代码
    /**
     * 判断是否是中文字符
     */
    fun isChinese(str: Char): Boolean {
        val ub = Character.UnicodeBlock.of(str)
        return 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 //ASCII
    }

2.判断是否是中文标点符号

Kotlin 复制代码
    fun isPunctuation(str: Char): Boolean {
        val ub = Character.UnicodeBlock.of(str)
        return ub === Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub === Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

    }

3.设置文字任意旋转角度

Kotlin 复制代码
    /**
     * 设置文字任意角度
     */
    fun setDirectionRotate(rotate: Float) {
        mDirection = DIRECTION_OTHER
        mRotate = rotate
        invalidate()
    }

4.绘制旋转文字

Kotlin 复制代码
private var mLastTextOther = false//非中文   
private var mRotate = 0f  //旋转角度 
override fun onDraw(canvas: Canvas) {
        //属于正常显示不需要绘制
        if (isDefault) {
            super.onDraw(canvas)
            return
        }

        paint?.let {
            canvas.withSave {

                val len = text.length
                var py = left.toFloat()

                var h = height / 2f
                if (h <= 0) {
                    h = mHeight / 2f
                }

                val textHeight = it.textSize / 2
                for (i in 0..<len) {
                    val c = text[i]
                    val charWidth = it.measureText(c.toString())

                    if (isPunctuation(c)) {
                        mLastTextOther = false
                        canvas.drawText(c.toString(), py + charWidth / 2, h + textHeight, it)
                        py += charWidth
                    } else if (isChinese(c)) {
                        mLastTextOther = false
                        val textWidth = it.measureText(c.toString())
                        canvas.withRotation(mRotate, py + textWidth, h + textHeight) {
                            drawText(c.toString(), py + textWidth, h + textHeight, it)
                        }
                        py += textWidth
                    } else {
                        if (!mLastTextOther) {//上一个为中文
                            canvas.drawText(c.toString(), py + charWidth, h + textHeight, it)
                            py += charWidth * 2
                        } else {   // 非中文字符正常绘制
                            canvas.drawText(c.toString(), py, h + textHeight, it)
                            py += charWidth
                        }
                        mLastTextOther = true
                    }
                }
            }
        }
    }

根据上面代码即可实现对中文跟标点符号进行区分然后绘制任意角度,也可以不做判断对任何文字进行绘制。下面是最终效果

相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo4 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077004 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼4 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone4 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen4 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone4 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui