Android ImageView adjusting parent#39;s height and fitting width(Android ImageView调整父级高度和适配宽度)
问题描述
更新:我使用 这个答案
我有点纠结这个问题,我认为应该很简单.
I'm a bit stuck with this issue, which I think should be pretty simple.
所以我的应用程序下载了一张图片,并在 ImageView(RelativeLayout 的一个子元素)中呈现位图.我希望 ImageView 适合父宽度,并调整它的大小以保持纵横比.
So my app downloads an image, and renders the bitmap in an ImageView, a child element of a RelativeLayout. I would like the ImageView to fit the parent width, and to adapt it's size to keep the aspect ratio.
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
还有代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner);
ImageView imgV = new ImageView(this);
imgV.setScaleType(ImageView.ScaleType.CENTER_CROP);
// I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect...
imgV.setBackgroundColor(0x00FFFF00);
imgV.setAdjustViewBounds(Color.BLUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
banner.addView(imgV,params);
// Some code downloading the image stream
bitmap = BitmapFactory.decodeStream(stream);
imgV.setImageBitmap(bitmap);
}
期望:
结果:
推荐答案
感谢@Julien 和@js.这是 ImageView 的完整解决方案,即使位图小于 ImageView,也会拉伸位图高度保持纵横比.
Thanks to @Julien and @js. Here is complete solution of ImageView that will stretch bitmap height preserving aspect ratio even if bitmap is smaller than ImageView.
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
你可以在你的 xml 布局中使用这个类来代替 ImageView.
You can use this class in your xml layouts instead ImageView.
<com.example.ResizableImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/banner" />
这篇关于Android ImageView调整父级高度和适配宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android ImageView调整父级高度和适配宽度
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01