在andorid中获得CardView视图的高度

2014-11-1 22:56:53     人阅读    

下面是我的代码

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ....
    int width = holder.itemView.getWidth();
    ....
    //do some calculations
}

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="#ffffff"
    card_view:cardCornerRadius="3dp">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/includedPost"
            layout="@layout/post_details" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
includedPost:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/postImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/commenterImage"
        android:minHeight="120dp"
        android:scaleType="centerCrop" />

</RelativeLayout>

处理方法

你可以使用Picasso Transformation来达到需求

import android.graphics.Bitmap;
import android.view.View;

import com.squareup.picasso.Transformation;

/**
 
 * Picasso Transformation class to fit image to target View size
 
 */
public class FitToTargetViewTransformation implements Transformation {
  private View view;

  public FitToTargetViewTransformation(View view) {
    this.view = view;
  }

  @Override
  public Bitmap transform(Bitmap source) {
    int targetWidth = view.getWidth();

    double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
    int targetHeight = (int) (targetWidth * aspectRatio);
    if (source.getHeight() >= source.getWidth()) {
      return source;
    }
    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
    if (result != source) {
      // Same bitmap is returned if sizes are the same
      source.recycle();
    }
    return result;
  }

  @Override
  public String key() {
    return "transformation" + " desiredWidth";
  }
}

在 onBindViewHolder 中添加如下代码

Picasso.with(context)
    .load(AVATAR_ENDPOINT)
    .transform(new FitToTargetViewTransformation(feedViewHolder.icAvatar))
    .into(feedViewHolder.icAvatar);


原文地址:http://www.itmmd.com/201411/103.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

android 布局,在包含layout xml文件的时候如何设置ID?   上一篇
下一篇  android 线程间同步问题
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!