Android setImageDrawable无法在没有资源图像的情况下正确缩放

Android setImageDrawable无法在没有资源图像的情况下正确缩放,android,Android,我正在尝试将存储在文件系统中的图像加载到imageButton中。但它不能正确缩放。该文件在可绘制资源和文件系统中都是相同的t700.png,但结果不同 这项工作很好: button.setImageDrawable(getResources().getDrawable(R.drawable.t700)); 此更改比例/大小并获得更大的图像: String filePath = VideoActivity.this.getFilesDir()+File.separator+EPGApiServ

我正在尝试将存储在文件系统中的图像加载到imageButton中。但它不能正确缩放。该文件在可绘制资源和文件系统中都是相同的t700.png,但结果不同

这项工作很好:

button.setImageDrawable(getResources().getDrawable(R.drawable.t700));
此更改比例/大小并获得更大的图像:

String filePath = VideoActivity.this.getFilesDir()+File.separator+EPGApiService.LOGOS_DIR+File.separator+channel.getLogoFile();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
BitmapDrawable imgDrawable = new BitmapDrawable( this.getResources(),bitmap);
button.setImageDrawable(imgDrawable);
我需要同样的结果

注: 如果我这样做

可绘制位图的大小不一样

logoBitmapDrawableFromDisk大小为410x123

logoBitmapDrawableFromDisk2大小为205x62

logoBitmapDrawableFromRes大小为820x246

真正的文件是相同的。这两种款式我都需要同样的尺码

BitmapDrawablegetResources,位图

从位图创建可绘制,根据资源的显示度量设置初始目标密度

它看起来更大,因为它使用资源对象将图像调整到屏幕的密度

要避免这种情况,请使用BitmapDrawablebitmap

或者调整png的大小,使其在mdpi设备中正确显示。 然后BitmapDrawablegetResources,位图将在所有设备中为您提供正确的外观

在所有设备中具有相同外观的正确方法是为每个设备密度创建不同的图标。 从mdpi密度的维度开始。应根据以下值计算其他密度的尺寸:

hdpi乘以1.5 xhdpi乘以2 xxhdpi乘以3

每个图标应放在单独的文件夹下,并根据密度使用适当的名称:

mdpi->res/drawable\u mdpi hdpi->res/drawable\U hdpi xhdpi->res/drawinable\u xhdpi xxhdpi->res/drawable\U xxhdpi

在xml中设置ImageButton源或使用getResources.getDrawableR.drawable.t700;设备将自动选择正确的设备


有关更多信息,请参见

您使用的android版本是什么?R.drawable.t700是一个文件或是另一个drawable资源的别名?它是指res/drawablein文档中的t700.png图像文件:BitmapDrawableResources res此构造函数在API级别18中被弃用。使用BitmapDrawableandroid.content.res.Resources、android.graphics.Bitmap来指定要绘制的位图。正如我从android文档中了解到的,如果图标位于res/drawable文件夹下,则使用BitmapDrawablebitmap和getResources.getDrawableR.drawable.t700;应相同。图标不在res/drawable文件夹中,因为正在运行时加载。这就是问题所在…我想控制缩放…不使用BitmapDrawablebitmap。徽标看起来更小,尺寸也更大。我需要调整图像,使其固定。但只能使用button.setImageDrawablegetResources.getDrawableR.drawable.t700工作;仅手动设置工作密度:logoBitmap.setDensity200;t700.png的实际宽度和高度是多少?实际尺寸是410x123。是的,我同意你们的观点,但问题是图标是使用外部JSON服务下载的。通过JSON获取URL,然后从该URL获取徽标文件。我不能修改WebService,但它不是我的。
BitmapDrawable logoBitmapDrawableFromDisk = new BitmapDrawable(VideoActivity.this.getResources(),logoBitmap);
BitmapDrawable logoBitmapDrawableFromDisk2 = new BitmapDrawable(logoBitmap);
BitmapDrawable logoBitmapDrawableFromRes = (BitmapDrawable) getResources().getDrawable(R.drawable.t700);