drawBitmap takes too long

隐身守侯 提交于 2019-12-24 19:03:44

问题


Hello here is the code:

        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;
            //}

Now what happens is that drawBitmap is blocking since 5 minutes... Any ideas?


回答1:


You might get better performance by creating a scaled bitmap in the first place.

Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath());
bmp = bmp.createScaledBitmap(bmp, width, height, true);

Then you won't have to use a matrix when drawing it out to the screen either.



来源:https://stackoverflow.com/questions/6558725/drawbitmap-takes-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!