编辑一个:
根据约瑟夫的回答做出的改变:
在bytesToDrawable(byte [] imageBytes)中:
更改了以下内容:使用BitmapDrawable(资源res,位图位图)而不是BitmapDrawable(位图位图):
return new BitmapDrawable(ApplicationConstants.ref_currentActivity.getResources(),BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options));
Run Code Online (Sandbox Code Playgroud)
这是这种变化的结果:略有不同的问题:

题:
如果我正在使用新的构造函数来绘制位图并将图像缩放到所需的目标密度,那么我还需要使用我的calculateSampleSize方法吗?
原始问题:
嗨朋友们,
我的应用程序是基于模块的,因此特定于该模块的图像仅从包含它们的jar(模块)加载,而不是从主应用程序加载.
每个模块都有自己的ModularImageLoader - 它基本上允许我根据jar中找到的图像名称来获取Drawables.
构造函数接收zipFile(模块A)和文件名列表(zip中以".png"结尾的任何文件).
研究实施:
我使用了以下内容:有效加载位图链接到开发人员页面
最初我是为每个密度创建大小的图像,但现在我只有一组大小为96x96的图像图标.
如果屏幕密度小于xhdpi,我会加载96x96图像的较小采样大小 - 36x36(对于ldpi),48x48(对于mdpi),72x72(对于hdpi).否则我只返回96x96图像.(查看方法calculateSampleSize()和bytesToDrawable())
我认为用代码更容易理解这个概念:所以这里是ModularImageLoader
码:
public class ModularImageLoader
{
public static HashMap<String, Drawable> moduleImages = new HashMap<String, Drawable>();
public static int reqHeight = 0;
public static int reqWidth = 0;
public ModularImageLoader(ZipFile zip, ArrayList<String> fileNames)
{
float sdpi = ApplicationConstants.ref_currentActivity.getResources().getDisplayMetrics().density;
if(sdpi == 0.75)
{
reqHeight = 36;
reqWidth = 36;
}
else if(sdpi == …Run Code Online (Sandbox Code Playgroud) 我想要实现的是能够从输入流计算位图的高度和宽度,而无需实际更改BitmapFactory.Options
这就是我做的:
private Boolean testSize(InputStream inputStream){
BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
Bitmp_Options.inJustDecodeBounds = true;
BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
int currentImageHeight = Bitmp_Options.outHeight;
int currentImageWidth = Bitmp_Options.outWidth;
if(currentImageHeight > 200 || currentImageWidth > 200){
Object obj = map.remove(pageCounter);
Log.i("Page recycled", obj.toString());
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在这里的主要问题是它将BitmapFactory.Options更改为无法正确解码的状态.
我的问题是有另一种重置BitmapFactory.Options的方法吗?或其他可能的解决方案
另一种方法:(注意:当应用top方法时,originalBitmap为null)
这是我原来的代码:
Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);
Run Code Online (Sandbox Code Playgroud)
应用Deev和Nobu Game的建议:(没有变化)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream,null,options);
Run Code Online (Sandbox Code Playgroud) 我已经采取并修改了一些代码,用于获得较大(500x500)位图图像的高质量抗锯齿小版本。显然,使用 Matrix 比使用 createScaledBitmap() 产生更高的质量。结果不是很令人印象深刻..我怀疑也许我犯了一个错误,特别是我不确定是否真的使用了期权。所以我将其更改inSampleSize = 1;为inSampleSize = 50;期望看到质量急剧下降,但没有任何变化。所以现在我怀疑选项被忽略了。这段代码能救出来吗?
我希望也许能找到某个版本的 createBitmap,它接受了 abitmap和一个options参数,但找不到。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferQualityOverSpeed = true;
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), R.drawable.bigicon);
Matrix matrix = new Matrix();
matrix.postScale(.1f,.1f);
Bitmap scaledBitmap = Bitmap.createBitmap(bmpSource, 0, 0, bmpSource.getWidth(), bmpSource.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud) 我在Android开发中绝对是新的,我发现使用BitmapFactory.decodeResource()方法进入实用程序类(不是活动类的类)的问题.
所以我正在对我的代码进行一些重构,我必须移动这个代码行:
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);
Run Code Online (Sandbox Code Playgroud)
从活动类方法到我在实用程序类中声明的方法.
它在活动类中运行良好,但将int移入实用程序类方法,如下所示:
public class ImgUtility {
public Bitmap createRankingImg(int difficulty) {
// Create a Bitmap image starting from the star.png into the "/res/drawable/" directory:
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);
return myBitmap;
}
}
Run Code Online (Sandbox Code Playgroud)
我在getResources()方法上获取了一条IDE错误消息,IDE对我说:
无法解析方法'getResources()'
我认为这是因为getResources()方法检索与活动类相关的内容.
查看旧代码,我可以看到getResources()方法返回一个ContextThemeWrapper对象.我试图在AppCompatActivity类中搜索(因为我的原始活动扩展了它),但我找不到.
所以我的怀疑是:
1)主要问题是:如何在我的previus ImgUtility clas中正确使用BitmapFactory.decodeResource()方法?
2)为什么当我使用BitmapFactory.decodeResource()方法时,它将ContextThemeWrapper对象(由getResources()方法返回)作为参数?究竟什么代表了这个对象?它与活动类有什么关系?在哪里申报?
TNX
java android bitmapfactory android-activity android-resources
在这段代码中:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image1).copy(Bitmap.Config.ARGB_8888, true);
Run Code Online (Sandbox Code Playgroud)
有什么作用copy(Bitmap.Config.ARGB_8888, true)?