Android ImageView缩放冲突

时间:2015-02-11 20:25:57

标签: android imageview relativelayout scaling bounds

我在代码中创建ImageView

ImageView vlogo = new ImageView(v.getContext());
        RelativeLayout.LayoutParams vlogoParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        vlogoParams.addRule(RelativeLayout.ALIGN_TOP,tv_visitor.getId());
        vlogoParams.addRule(RelativeLayout.ALIGN_LEFT, tv_visitor.getId());
        vlogoParams.addRule(RelativeLayout.ALIGN_BOTTOM, tv_visitor.getId());
        vlogo.setLayoutParams(vlogoParams);
        vlogo.setImageResource(R.drawable.star);
        rl.addView(vlogo);

通过将图像与先前创建的tv_visitor视图的TOPBOTTOM对齐(已添加到相对布局),可以“缩放”图像。但我认为它还没有布局(?)。 .requestLayout就在这段代码没有改变之前。  Picture

通过这样做,我将ImageView的高度设置为tv_visitor视图的高度。哪个可以。然而,宽度似乎保持原始。

这就是问题所在。宽度保持不缩放。 我这样做的方法是使 .setAdjustViewBounds(true); 无效。我该如何处理?

根据评论中的要求,我提供了更多信息:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/news_scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000"
    android:scrollbars="none">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragment_news2">
    <View
        android:id="@+id/anchor_n"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffbb00"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>

视图作为参数传递,布局通过以下方式获得:

RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fragment_news2);

1 个答案:

答案 0 :(得分:2)

问题是由于Imageview的缩放,因为默认情况下imageview使用的是ImageView.ScaleType.FIT_CENTER。根据文档,FIT_CENTER执行以下操作:

  

计算将保持原始src宽高比的比例,但是   还将确保src完全适合dst。至少一个轴   (X或Y)将完全适合。 结果集中在dst

由于图像被调整大小并居中,因此您在左侧和右侧都看到了额外的空格。为了避免将ScaleType设置为ImageView.ScaleType.FIT_END而将结果右对齐将解决问题。

vlogo.setScaleType(ImageView.ScaleType.FIT_END);