【问题标题】:How can I rotate an Android textview with background color that is not jagged (needs to be anti-aliased)如何旋转具有非锯齿状背景颜色的 Android 文本视图(需要抗锯齿)
【发布时间】:2015-04-04 01:24:40
【问题描述】:

我有一个带有背景颜色(看起来像打印标签)的静态 xml 布局,我想以某个角度显示。我遇到的问题是背景的边缘看起来像是在 1980 年代渲染的(锯齿状 - 没有抗锯齿)。

我怎样才能得到一个在某个角度看起来像标签的文本视图并让它看起来不错(即抗锯齿)?

布局:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:maxLines="1"
        android:singleLine="true"
        android:text="Sample Text"
        android:textAllCaps="true"
        android:textColor="#ffffff"
        android:textSize="12sp"
        android:background="#3db8eb"
        android:rotation="-3"
        android:paddingTop="1dp"
        android:paddingBottom="1dp"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"/>
</LinearLayout>

外观:

【问题讨论】:

  • 你的意思是你想要textView的锯齿形边框?
  • 不,我想要一个平滑的边框(抗锯齿)而不是像图片所示的锯齿形锯齿状边框。
  • 然后你可以使用一张图片进入背景。
  • 将背景设置为图像(如 9 补丁)仍然不起作用,边缘仍然是锯齿状的。

标签: android textview antialiasing


【解决方案1】:

根据我的related answersetFilterBitmap 对我来说不太适用。部分原因是我使用的是 9patch 背景而不是颜色。

方法#1:在视图上禁用硬件加速

起作用的是禁用相关视图及其父视图的硬件加速(单独的任何一个都不起作用):

private void fixAntiAlias(View viewFromThe80s) {
    if (Build.VERSION.SDK_INT > 10) {
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
        viewFromThe80s.setLayerType(View.LAYER_TYPE_SOFTWARE, p);
        ((View) viewFromThe80s.getParent()).setLayerType(View.LAYER_TYPE_SOFTWARE, p);
    }
}

编辑:[2/2/17]:我又遇到了这个问题,但这次只需要通过资源文件差异而不是代码/逻辑差异来解决它

方法 #2:基于 XML 的修复

有时,您无法编辑视图逻辑。例如,在我的例子中,两个视图共享同一个 RecyclerView.Adapter,一个有旋转视图,另一个没有。我需要视图/适配器逻辑保持不变,但 XML 布局和资源可能会有所不同。所以我采取了这种方法:

1.制作带有透明边框的形状 src/main/res/drawable/background_rotatable_color.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- transparent 1px border -->
    <item>
        <shape>
            <solid android:color="@android:color/transparent"/>
            <size android:width="16dp" android:height="16dp"/>
        </shape>
    </item>
    <!-- main color : inset by 1px so the transparent layer below shows so that when antialiasing is done, it blends better -->
    <item android:top="1px" android:right="1px" android:left="1px" android:bottom="1px">
        <shape>
            <solid android:color="@color/red"/>
        </shape>
    </item>
</layer-list>

2。应用形状和(可选):使用着色将其设置为所需的颜色
src/main/res/layout-land/item_product_details.xml

<TextView
    android:id="@+id/text_savings_banner"
    . . .
    android:background="@drawable/background_rotatable_color"
    android:backgroundTint="@color/blue_accent" /> <!-- optional but handy if you need to do this in a lot of places with different colors -->

【讨论】:

    【解决方案2】:

    在尝试了一些不同的事情之后,我终于找到了一些可行的方法,尽管有点令人费解。以下是如何在具有纯色背景的旋转文本视图上获得平滑边缘:

    第 1 步:使用颜色创建可绘制资源(即 ColorDrawable 或 Shape like a rectangle)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="@color/blue" />
    </shape>
    

    第 2 步:为该可绘制对象设置背景。

    <TextView
      android:id="@+id/my_textview"
      ...
      android:background="@drawable/blue_rectangle" />
    

    第三步:在代码调用中:

    TextView textView = (TextView)findViewById(R.id.my_textview);
    textView.getBackground().setFilterBitmap(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多