首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawBitmap耗时太长

drawBitmap耗时太长
EN

Stack Overflow用户
提问于 2011-07-03 02:08:39
回答 1查看 926关注 0票数 1

你好,这是代码:

代码语言:javascript
复制
        URL uri = new URL(photoUrl);
            URLConnection connection = uri.openConnection();
            Log.i(TAG, "connecting...");
            connection.connect();
            Log.i(TAG, "connected");
            Log.i(TAG, "building Bitmap...");

            InputStream is = connection.getInputStream();
            //BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);

            File myfile = new File(getApplicationContext().getCacheDir(), "wallpaper.tmp");
            myfile.createNewFile();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(myfile));

            byte buf[]=new byte[1024];
            int len;
            while((len=is.read(buf))>0)
            out.write(buf,0,len);
            out.close();
            is.close();

            Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath());
            //Bitmap bmp = BitmapFactory.decodeStream(bis);

            Log.i(TAG, "builded Bitmap");               
            Log.i(TAG, "showing bitmap...");


            //int scale;
            Matrix matrix = new Matrix();
            matrix.setScale(0.1F, 0.1F);
            //if (bmp.getWidth() < bmp.getHeight()){
            //  scale = canvas.getWidth()/bmp.getWidth();
            //}else{
            //  scale = canvas.getHeight()/bmp.getHeight();
            //}
            //matrix.postScale(scale, scale, bmp.getWidth(), bmp.getHeight());
            //matrix.postScale(0.5F, canvas.getWidth()/bmp.getWidth());

            //Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, canvas.getWidth(), canvas.getHeight(), true);

            //Paint p = new Paint();
            //p.setFilterBitmap(true);


            //try{
            canvas.drawBitmap(bmp, matrix, null);
            //}catch(NullPointerException exc){
            //  //why do we get this??
            //  Log.d(TAG, "NullPointerException drawing canvas. why?");
            //  return;
            //}

现在发生的情况是,drawBitmap在5分钟内被阻塞...有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-03 02:46:31

首先创建缩放的位图可能会获得更好的性能。

代码语言:javascript
复制
Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath());
bmp = bmp.createScaledBitmap(bmp, width, height, true);

然后,在将其绘制到屏幕上时,您也不必使用矩阵。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6558725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档