3

我正在创建一个动态壁纸,并且我在每次 Runnable.run() 调用时用不断变化的颜色在画布上绘画,我希望在顶部放置一个渐变,但我正在创建的渐变是可怕的带状。在谷歌搜索了几天后,我想出了 2 个解决方案:将抖动设置为 true 将画布位图设置为 ARGB_8888

我已经尝试在 getWallpaper() 访问器和 Paint 对象上执行第一个(将抖动设置为 true)但它没有帮助(我根本看不到任何抖动)所以我尝试更改画布位图但我'不知道如何实际显示它

// _canvasBmp = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);

_shadowPaint.setStyle(Paint.Style.FILL);
_shadowPaint.setShader(new RadialGradient(metrics.widthPixels / 2,
metrics.heightPixels / 2, metrics.heightPixels / 2, 0x00000000,0x33000000, Shader.TileMode.CLAMP));
_shadowPaint.setDither(true); // this hasn't seemed to have done anything to fix the banding

// my main rendering method is this (based on the Google live wallpaper example)
void drawFrame()
{
   final SurfaceHolder holder = getSurfaceHolder();

   Canvas c = null;
   try
   {
           c = holder.lockCanvas();
           // c.setBitmap(_canvasBmp);// this was my attempt to update the bitmap to one that was ARGB_8888 but it didn't render at all

           if (c != null)
           {
                   // draw something
                   drawBackground(c);
                   drawTouchPoint(c);
                   drawShading(c);
                   drawBorder(c);

                   getWallpaper().setDither(true); // yet another attempt to get some kind of dithering going to no avail
           }
   }
   finally
   {
           if (c != null)
                   holder.unlockCanvasAndPost(c);
   }

   _handler.removeCallbacks(_drawClock); // _drawClock is the Runnable object

   if (_isVisible)
   {
           _handler.postDelayed(_drawClock, 1000 / 25);
   }
}


private void drawShading(Canvas c)
{
    c.drawRect(_screenBounds, _shadowPaint); // _screenBounds is a Rect set to the _metrics width and height
}

在此先感谢您的时间

4

1 回答 1

4

在您的 PinupEngine 课程中...

@Override
public void onCreate(SurfaceHolder surfaceHolder) {
  super.onCreate(surfaceHolder);
  surfaceHolder.setFormat(android.graphics.PixelFormat.RGBA_8888);
}

我发现使用较大的位图像素类型时,LiveWallpaper 绘图速度较慢。我希望它也会使用更多的内存(超过双倍)。如果可以的话,如果可能的话,尝试限制 RGBA_8888 的使用可能是值得的。我认为默认是 RGB_565,对此不确定。

于 2011-02-23T02:44:10.083 回答