在2.0中设置桌面壁纸if(bp != null){
try {
                long t1 = System.currentTimeMillis();
WallpaperManager.getInstance(ctx).setBitmap(bp);
                long t2 = System.currentTimeMillis();
Log.i("aaaaa","execute time:"+(t2-t1)/1000.0+" seconds");
} catch (IOException e) {
e.printStackTrace();
}
}或if(bp != null){
try {
                long t1 = System.currentTimeMillis();
G.getBaseInstance.setWallpaper(bp);
                long t2 = System.currentTimeMillis();
Log.i("aaaaa","execute time:"+(t2-t1)/1000.0+" seconds");
} catch (IOException e) {
e.printStackTrace();
}
}
这两个API来设置桌面壁纸,执行的时间竟然要3至5秒....有人遇到这个问题吗?请高手解答

解决方案 »

  1.   

    不错的实验!
    可以再实验下:
                   long t1 = System.currentTimeMillis();
            WallpaperManager.getInstance(ctx).setBitmap(bp);
            G.getBaseInstance.setWallpaper(bp);
                    long t2 = System.currentTimeMillis();
            Log.i("aaaaa","execute time:"+(t2-t1)/1000.0+" seconds");
    看看时间是不是2者的叠加!
      

  2.   

    08-05 01:48:01.452: INFO/aaaaa(262): execute time:0.166 seconds
    我测试的结果,楼主是不是还进行了其他操作吧?换个墙纸不应该要那么长时间的。
      

  3.   

    设置内置的墙纸不需要这么长时间,如果是其他图片,需要访问WallpaperManagerService,保存替换掉wallpaper这个文件,然后通知Launcher,确实时间挺长的。特别是图片比较大的时候。
      

  4.   

    我的bitmap是外部图片,不过在setwallpaper之前已经转换成bitmap了。。
    按照你的说法,有没有方法缩短这个时间呢?
      

  5.   

    但是我同样的图片,在android 1.6下设置壁纸却很快
      

  6.   


    有可能,因为在2.0之前setWallpaper是在applicationcontext里直接调用的,没有使用ipc;而在2.0之后添加了WallpaperManagerService这个远程调用,可能会耗时多一些。
      

  7.   

    晕,我搞错了。 2.0之前的好像也是rpc的,只不过没有WallpaperManager和WallpaperManagerService这两个类。
    使用Bitmap,应该是压缩bitmap的时候耗时多了。
    <code>
    public void setBitmap(Bitmap bitmap) throws IOException {
            try {
                ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
                if (fd == null) {
                    return;
                }
                FileOutputStream fos = null;
                try {
                    fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
                } finally {
                    if (fos != null) {
                        fos.close();
                    }
                }
            } catch (RemoteException e) {
            }
        }
    </code>
      

  8.   


    public void setStream(InputStream data) throws IOException {
            try {
                ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
                if (fd == null) {
                    return;
                }
                FileOutputStream fos = null;
                try {
                    fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
                    setWallpaper(data, fos);
                } finally {
                    if (fos != null) {
                        fos.close();
                    }
                }
            } catch (RemoteException e) {
            }
        }你可以试试调用WallpaperManager.setStream,直接设置流,看速度会不会有提升。
      

  9.   


    嗯,用setStream明显快了很多