【问题标题】:Image height stretched (SimpleDraweeView)拉伸图像高度 (SimpleDraweeView)
【发布时间】:2017-02-20 20:15:56
【问题描述】:

最近我在玩一些流行的 android 库,我需要 StaggeredGridLayout 和 Fresco 的帮助。

让我告诉你我做错了什么

As you can see, these images height is streched

我尝试在 XML/通过 java 中更改 layout_height 参数,但没有得到好的结果。

Everything is up on my Github repo

一些sn-ps

public class GiphyView
    extends RecyclerView
    implements GiphyPresenter.ViewBind {

@Inject
public GiphyPresenter mGiphyPresenter;

private GiphyAdapter mAdapter;

public GiphyView(Context context) {
    super(context);
    initView(context);
}

public GiphyView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

public GiphyView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView(context);
}

private void initView(Context context) {
    CustomApplication.get(context).getGiphyComponent().inject(this);

    mAdapter = new GiphyAdapter();
    setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    setAdapter(mAdapter);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    mGiphyPresenter.attachVu(this);
}

@Override
protected void onDetachedFromWindow() {
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    imagePipeline.clearCaches();

    mGiphyPresenter.detachVu();

    super.onDetachedFromWindow();
}

@Override
public void notifyRangeInserted(int start, int count) {
    mAdapter.notifyItemRangeInserted(start, count);
}

private class GiphyAdapter extends RecyclerView.Adapter<GiphyViewHolder> {
    @Override
    public GiphyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GiphyViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.giphy_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(GiphyViewHolder holder, int position) {
        holder.setGif(mGiphyPresenter.getItemImage(position));
    }

    @Override
    public int getItemCount() {
        return mGiphyPresenter.getSize();
    }
}

class GiphyViewHolder extends ViewHolder {
    @BindView(R.id.gif)
    SimpleDraweeView mGif;

    GiphyViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    void setGif(String url) {
        /*ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setLowestPermittedRequestLevel(ENCODED_MEMORY_CACHE)
                .build();*/

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(url)
                .setAutoPlayAnimations(true)
                //.setImageRequest(request)
                .build();
        mGif.setController(controller);
    }
}

}

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="6dp"
    card_view:cardUseCompatPadding="true">

    <!-- 4:3 aspect ratio
           fresco:viewAspectRatio="1.33" -->
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/gif"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        fresco:failureImage="@drawable/ic_error_black_24dp"
        fresco:placeholderImage="@drawable/ic_android_black_24dp"/>
</android.support.v7.widget.CardView>

PSA:我已经尝试过使用 wrap_content,但没有成功(是的,我知道 Fresco 不支持它)。

【问题讨论】:

  • 你想达到什么目标?
  • 我想得到一个“堆栈”图像,as you can see in this screenshot,即使使用 layout_height="wrap_content", fresco:viewAspectRatio="1.33",每个 Cardview 之间也有很长的间隔跨度>

标签: android fresco staggeredgridlayout


【解决方案1】:

我猜您需要将父卡视图的尺寸设置为wrap_content,而您不需要父卡视图LinearLayout。只需使用类似的东西:

<android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/card_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  card_view:cardCornerRadius="6dp"
  card_view:cardUseCompatPadding="true">

  <com.facebook.fresco.SimpleDraweeView ... />
</android.support.v7.widget.CardView>

我在此处添加了一个示例应用程序,该应用程序展示了如何在具有网格布局的回收站视图中使用 CardViewhttps://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java

【讨论】:

  • 感谢您的回复,但我已经尝试过了。并且设置 layout_width="wrap_content" 不起作用,因为 Fresco 不支持包装内容(好吧,我可以添加它指定 4:3 视图)
  • 您必须更改父 CardView 以使用包装内容并保持 Drawee 视图具有 match_parent 和纵横比。
  • 你是否设置了纵横比并设置了 Drawee 高度以包裹内容?
  • 是的,奇怪的是,像 Picasso 或 Glide 这样的其他库都显示得很好
猜你喜欢
  • 2014-03-10
  • 2017-02-25
  • 2018-05-30
  • 2013-05-06
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多