Android PNG渐变背景图片失真问题

最近一个困扰很久的问题,渐变效果的png图片,设置为控件图片或background时,在eclipse上看着没有什么问题,但是在设备上运行时,可以看到明显的一圈圈的轮廓线,图片严重失真。

在网上google了一下似乎这个问题很多人遇到,找到一种解释是Android设备display默认是采用16-bits color palette来表示所有颜色,因此对于带alpha值的32位png图片会出现显示失真。

 

这个问题有两种解决方法:


1.第一种方法最简单直接(推荐),设置需要显示Activity的PixelFormat,
getWindow().setFormat(PixelFormat.RGBA_8888);

PS:在onCreate()中直接加

 

RGBA_8888为android的一种32位颜色格式,R,G,B,A分别用八位表示,Android默认格式是PixelFormat.OPAQUE,其是不带Alpha值的。设置之后可以看到图片的显示效果就和在PC上看到一样,不会出现带状的轮廓线了。

 

2.第二种方法比较麻烦,就是将你需要要显示的view设置为一个surfaceview,这样也可以达到同样的显示效果,但这种方法代价较大,不推荐。

 

 

The artefact you are seeing is known as "banding" and it is a consequence of your display being 16bits per pixel.(On Android 2.2 and lower the default pixel format is 16-bit (565/PixelFormat.OPAQUE ).)

The best way to resolve this is to add some "noise" to your image asset in Photoshop or Paint.NET.

Alternatively you can set your window to be 32bpp with the following line added to your activity'sonCreate(), between super.onCreate() and setContentView(). :

    getWindow().setFormat(PixelFormat.RGBA_8888);或者PixelFormat.TRANSLUCENT

posted @ 2011-12-03 11:52  灰太狼_lilongmin  阅读(1658)  评论(0编辑  收藏  举报