【发布时间】:2012-01-30 13:44:01
【问题描述】:
我这里有一些奇怪的东西。我以两种不同的方式加载了相同的图像:
代码如下:
if(imageAddress.startsWith("drawable")){
btn.setImageResource(this.getResources().getIdentifier(imageAddress, null, this.getPackageName()));
}else{
btn.setImageURI(Uri.parse(new File(imageAddress).toString()));
}
正如你在上面看到的,我测试图像地址是否以“drawable”开头,如果是,那么我将图像加载为drawable。如果没有,那么它是来自 sdcard 的图像,然后我通过它的 URI 加载它。
这是 ImageButton 的代码:
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/roundcorners"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight=".95">
</ImageButton>
下面是这个结果:
上图显示加载为 DRAWABLE 时的 ImageButton (imageAddress = "drawable/eu_quero")
上图显示通过 IMAGE URI (imageAddress = "/mnt/sdcard/myapp/images/eu_quero.png") 加载时的 ImageButton
我需要的是相同大小的图像,无论我将其加载为可绘制的还是通过 ImageURI 加载。
谁能帮帮我吗?
重要提示:图片完全相同!!!大小一样!!
【问题讨论】:
-
有趣的问题,您可以尝试将 sd 卡图像加载为可绘制对象:developer.android.com/reference/android/graphics/drawable/…,然后使用
btn.setImageDrawable()添加到您的图像按钮,看看是否有帮助。
标签: android imagebutton