在Android中,加载和优化Bitmap的过程是重要的操作,以下是加载Bitmap并对其进行优化的示例代码:
//将资源作为Bitmap加载 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
//将文件作为Bitmap加载 File file = new File("path/to/file"); Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//将网络资源作为Bitmap加载 URL url = new URL("http://example.com/image.png"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(input);
//优化Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true); //改变Bitmap的大小 bitmap = bitmap.copy(Bitmap.Config.RGB_565, true); //将Bitmap的色彩模式改为RGB_565
//释放Bitmap的内存 bitmap.recycle();