Android设置imageView背景和来源

发布于 2024-12-22 15:10:46 字数 686 浏览 1 评论 0原文

我需要一些帮助来设置图像视图固定大小。这是我的情况。我希望根据设备的屏幕宽度和高度调整应用程序中所有图像视图的大小。例如,我希望列表视图中的图像的宽度 = 设备显示宽度的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-12-29 15:10:46

编辑:

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (width_in_dp * scale + 0.5f);

将 width_in_dp 替换为您想要的尺寸。
另外,您可以在这里查看: http://developer.android.com/guide/ Practices/screens_support.html

    DisplayMetrics dm = new DisplayMetrics();      
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);                  
    int screenWidth = dm.widthPixels; 

使用此 screenWidth,您可以设置视图或位图尺寸。

EDIT:

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (width_in_dp * scale + 0.5f);

Replace width_in_dp with your desired size.
Also, you can have a look here: http://developer.android.com/guide/practices/screens_support.html

    DisplayMetrics dm = new DisplayMetrics();      
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);                  
    int screenWidth = dm.widthPixels; 

With this screenWidth you can set view or bitmap sizes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文