drawBitmap需要太长时间

时间:2011-07-02 18:08:39

标签: java android android-canvas drawbitmap

你好,这是代码:

        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分钟...... 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

首先,您可以通过创建缩放的位图来获得更好的性能。

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

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

相关问题