某些设备上的形状渐变不平滑

时间:2013-11-06 07:09:00

标签: android gradient android-shapedrawable

我正在尝试使用ShapeDrawable在文本上方添加渐变。目标是淡化屏幕左侧和右侧的文本。布局如下:

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Lorem ipsum dolor sit amet"
        android:textColor="#000"
        android:textSize="18sp"/>

    <View
        android:layout_height="wrap_content"
        android:layout_width="60dp"
        android:layout_alignTop="@id/text_view"
        android:layout_alignBottom="@id/text_view"
        android:background="@drawable/gradient_shape"/>

    <View
        android:id="@+id/line"
        android:layout_height="2dp"
        android:layout_width="30dp"
        android:layout_below="@id/text_view"
        android:background="#00f"/>

    <View
        android:layout_height="2dp"
        android:layout_width="60dp"
        android:layout_below="@id/line"
        android:background="#f00"/>

</RelativeLayout>

渐变ShapeDrawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<gradient
    android:startColor="#ffffffff"
    android:endColor="#00ffffff"
    android:type="linear"
    />

</shape>

问题是我的HTC Desire HD渐变并不顺畅。在屏幕上它看起来像这样:(抱歉质量,在照片编辑器中略微调整图像,使效果更加明显)

gradient photo

我发现,这条渐变线显示在渐变可绘制的中心。渐变下方的蓝线显示了这一点。 任何人都可以建议我如何解决这个问题?在设备上这看起来很糟糕。

我试图在模拟器和Ritmix-RMP-470上运行此应用程序 - 一切看起来都很好。渐变光滑美观。但在HTC Desire HD问题上存在问题。 另外,我尝试使用DDMS从设备截取屏幕截图。截图一切看起来都很好:

gradient screenshot using ddms

我的活动尝试在照片编辑器中制作渐变PNG图片,然后使用此png代替ShapeDrawable。结果仍然相同。

有什么方法可以避免这种渐变的视觉问题?

1 个答案:

答案 0 :(得分:0)

尝试这样做:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.RGBA_8888);
    ...
}

有些手机(尤其是HTC)使用另一款默认的PixelFormat。它没有显示干净的颜色表面,但任何渐变或阴影的东西看起来都会错误。

相关问题