smooth gradient on different devices

2020-06-08 13:43发布

In my app i have gradient as drawable which i am using as background and i wan't it to make it look as smooth as possible. After googling and trying by myself i came up with the following. On nexus one if you call only setDither(true) your gradient is still banding so you have to set PixelFormat like this Window.setFormat(PixelFormat.RGBA_8888). But on the other side G1 does not support RGBA_8888 so calling it make the gradient even uglier than before so Window.setFormat(PixelFormat.RGBA_8888) will not work well on devices that don't support it.

What is the correct way smooth my gradient on all devices on which my app will run.

PS: i found some related topics

How to draw a smooth/dithered gradient on a canvas in Android

Is it possible to dither a gradient drawable?

4条回答
姐就是有狂的资本
2楼-- · 2020-06-08 13:52

If the dither and RGBA_888 setting don't help on some phones, the solution can be to set the element layerType to software, to disable the hardware acceleration

android:layerType="software"

This fixed my problem on a Samsung S5 phone.

查看更多
够拽才男人
3楼-- · 2020-06-08 13:53

When loading your image, set the pixelFormat to RGB_565 and setDither to true, if you are drawing it straight to the screen with no other modification.

The displays typically aren't 24bit, that should typically do it good enough, at least pretty consistently all around.

You don't need to use 8888 if you don't need the alpha and won't be modifying the image data. It will displayed on 99% of mobile displays as 16bit color and not 24bit, since that is what the panels support.

N1 will always have issues close up because the matrix they use for the panel has double the green resolution.

查看更多
一纸荒年 Trace。
4楼-- · 2020-06-08 13:56

Open your image in Photoshop (or Gimp or Paint.NET or whatever) and add a small quantity of noise. Then you don't have to do anything in code.

查看更多
5楼-- · 2020-06-08 14:07

You can use a custom shape (put it in /drawable/gradient.xml or something like that):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#000000"
    android:endColor="#ffffff"
    android:angle="90"/>
</shape>

This way it will be painted by the os and it should be perfectly smooth.

查看更多
登录 后发表回答