Android设置imageView背景和来源
我需要一些帮助来设置图像视图固定大小。这是我的情况。我希望根据设备的屏幕宽度和高度调整应用程序中所有图像视图的大小。例如,我希望列表视图中的图像的宽度 = 设备显示宽度的 25%。现在我可以这样做:
Bitmap bmp=BitmapFactory.decodeResource(acontext.getResources(), R.drawable.transparent_bg);
int width=(int) RPCCommunicator.getPlaceHolderWidth(acontext, 25);
int height=(int) RPCCommunicator.getPlaceHolderHeight(acontext, 20);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
mainImg.setImageBitmap(resizedbitmap);
在这个示例代码中,我只是将透明图像设置为我的列表视图项目上的默认图像,并具有我想要的尺寸。但我想做的是,设置图像视图的大小,如果我从服务器收到的图像小于默认大小,则不适合,只需保持在这个“占位符”的中心即可。如果它很大,我就必须将它放入图像视图中。
所以基本上我的问题是:如何以编程方式设置 ImageView 的宽度和高度而不适合他的内容。希望你得到我想要的。
提前致谢!
I need a little help with setting an imageview fixed size. Here is the situation that I have. I want all the imageview's size in my application to be resized depending on device's screen width and height. For example I want my image in listview to be with width = 25% from device's display width. For now i can do that like this :
Bitmap bmp=BitmapFactory.decodeResource(acontext.getResources(), R.drawable.transparent_bg);
int width=(int) RPCCommunicator.getPlaceHolderWidth(acontext, 25);
int height=(int) RPCCommunicator.getPlaceHolderHeight(acontext, 20);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
mainImg.setImageBitmap(resizedbitmap);
In this sample code I'm just setting a transparent image as default image on my listview item with the sizes which I want. But the thing that I want to do is, to set the size of the imageview, and if image which I receive from server is smaller than the default size,not to fit, just stay centered in this "placeholder". If it's big I'll have to fit it in imageview.
So basically my question is : How can I set ImageView's width and height programmatically without fitting his content. Hope you got what I want.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
将 width_in_dp 替换为您想要的尺寸。
另外,您可以在这里查看: http://developer.android.com/guide/ Practices/screens_support.html
使用此 screenWidth,您可以设置视图或位图尺寸。
EDIT:
Replace width_in_dp with your desired size.
Also, you can have a look here: http://developer.android.com/guide/practices/screens_support.html
With this screenWidth you can set view or bitmap sizes.