【问题标题】:Android ImageView ScaleType *FIT_TOP*Android ImageView ScaleType *FIT_TOP*
【发布时间】:2011-01-25 17:28:47
【问题描述】:

我正在尝试实现一个可以容纳横向或纵向图像的 ImageView。 这些图像应该适合图像视图的宽度(如果是横向)或高度(如果是纵向),但在任何情况下,它们都必须与视图的顶部对齐,并且没有边距或填充。

我想要实现类似于android:scaleType="fitStart" 但在纵向图像的情况下居中或在横向图像的情况下与顶部对齐。

添加:

现在我正在使用这么丑陋的代码,这似乎可以工作,但不确定它是否是最好的解决方案:

<com.custom.layout.MyImageView 
        android:id="@+id/detail_view_image" 
        android:src="@drawable/logo" 
        android:background="#fff"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:cropToPadding="false"
        android:adjustViewBounds="false"
/>

然后在我的课堂上,我做的扩展 ImageView:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int imgWidth = getDrawable().getIntrinsicWidth();
        int imgHeight = getDrawable().getIntrinsicHeight();

        if(imgWidth>imgHeight)
            setScaleType(ScaleType.FIT_START);
        else
            setScaleType(ScaleType.FIT_CENTER);

        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);      

        setMeasuredDimension(width, height);
    }

【问题讨论】:

  • 我想做类似的事情,你有没有找到解决方案?
  • 到目前为止没有运气,我最终使用了我上面发布的代码......
  • 好的,感谢代码!

标签: android imageview drawable


【解决方案1】:

您可以在 onCreate() 方法中放置一个 sn-p,而不是覆盖本机视图,该方法根据手机的方向更改 ImageView 的 scaleType。

一些示例 sn-p(我不确定它是否像那样工作,但可以理解), 在 onCreate 方法中:

ImageView imgView = findViewById(R.id.myImage);
//portrairt orientation
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    imgView.setScaleType(ScaleType.FIT_CENTER);
} else { //landscape orientation
    imgView.setScaleType(ScaleType.FIT_START);
}

因此,当方向发生变化时,将再次调用 onCreate 并更改视图的比例类型,如果您要覆盖 onConfigurationChanged(),那么您应该在任何您想要的地方再次添加上面的 sn-p。 试试看,让我知道它是否有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多