TextView中文本倾斜

作者: Love零O | 来源:发表于2018-11-21 14:46 被阅读17次

需求:使TextView中的文字倾斜一定的角度。如下图所示:

如何实现呢?自定义View?这可能是大多数同学产生的第一个想法。的确,自定义View可以实现这个需求。我也找过网上自定义view的方法,大多数只是继承TextView,在onDraw()方法中将画布旋转:

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
        canvas.rotate(degrees, this.getWidth() / 2f, this.getHeight() / 2f);
//        canvas.rotate(degrees, 0, 0);
        super.onDraw(canvas);
        canvas.restore();
    }

其中canvas.rotate(degrees, this.getWidth() / 2f, this.getHeight() / 2f);方法中degrees就是需要旋转的角度。


今天介绍一种更简单的方法:
View中有一个属性:rotation,可以实现旋转View。因为TextView是继承的View,所以也可以使用这个属性。
XML中设置
android:rotation="45"

java代码中设置
mTextView.setRotation(45);

View的相关源码:

/**
     * Sets the degrees that the view is rotated around the pivot point. Increasing values
     *设置视图围绕轴心点旋转的度数。
     * result in clockwise rotation.
     *顺时针旋转
     * @param rotation The degrees of rotation.旋转的角度。
     * @see #getRotation()
     * @see #getPivotX()
     * @see #getPivotY()
     * @see #setRotationX(float)
     * @see #setRotationY(float)
     *
     * @attr ref android.R.styleable#View_rotation
     */
    public void setRotation(float rotation) {
        if (rotation != getRotation()) {
            // Double-invalidation is necessary to capture view's old and new areas
            invalidateViewProperty(true, false);
            mRenderNode.setRotation(rotation);
            invalidateViewProperty(false, true);

            invalidateParentIfNeededAndWasQuickRejected();
            notifySubtreeAccessibilityStateChangedIfNeeded();
        }
    }

其实,其他继承View的控件都可以使用这个属性进行旋转。并不影响自身的点击事件。

相关文章

  • TextView中文本倾斜

    需求:使TextView中的文字倾斜一定的角度。如下图所示: 如何实现呢?自定义View?这可能是大多数同学产生的...

  • SpannableString和SpannableStringB

    安卓开发中TextView 是我们最常用的控件之一,我们用TextView为我们在页面中展示文本。展示普通文本当然...

  • 八、Flutter的基础Widget

    一. 文本Widget 在Android中,我们使用TextView,iOS中我们使用UILabel来显示文本; ...

  • Flutter的基础Widget

    一. 文本Widget 在Android中,我们使用TextView,iOS中我们使用UILabel来显示文本; ...

  • Flutter(六)基础Widget

    1. 文本Widget 在Android中,我们使用TextView,iOS中我们使用UILabel来显示文本; ...

  • Flutter开发 -- [03 - 文本Widget]

    1. 文本Widget 在Android中,我们使用TextView,iOS中我们使用UILabel来显示文本; ...

  • 倾斜的TextView

    不废话,直接上代码: 然后在布局文件里面正常用就可以了 搞定!老规矩,上图

  • TextView常用方法

    TextView 字符串替换 textview 设置的文本信息中可能带有变量,使用android的XLIFF可轻松...

  • Flutter学习笔记04-文本Widget

    1.Text 在Android中,使用TextView显示文本,iOS中使用UILabel来显示文本;Flutte...

  • 关于Android界面组件的基本用法

    1.文本框(TextView)和编辑框(EditText)文本框(TextView)不允许用户编辑文本内容,而编辑...

网友评论

    本文标题:TextView中文本倾斜

    本文链接:https://www.haomeiwen.com/subject/zpfcqqtx.html