无法将GridView中的ImageView设置为从Drawable转换的位图

时间:2016-07-27 11:47:32

标签: java android gridview bitmap imageview

我有一个 GridView ImageView ,当我构建GUI时,我目前想要将一些库存drawable转换为位图,并将ImageViews设置为这些。他们目前只是空白,但据我所知,代码是正确的。

GridView XML :(随着更多元素的添加而扩展)

        <com.example.tristan.studentshare.ExpandingGridView
            android:id="@+id/achievement_grid"
            android:layout_below="@+id/profile_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:columnWidth="100dp"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp">
        </com.example.tristan.studentshare.ExpandingGridView>

row_grid_achievements XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <ImageView
        android:id="@+id/achievement_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_person_black_24dp"/>

    <TextView
        android:id="@+id/achievement_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="20sp"/>

</LinearLayout>

Achievement中显示GridView

public class Achievement {

private String id;
private String name;
private String description;
private Integer progress;
private Bitmap icon;

public Achievement(String id, String name, String description, Integer progress, Bitmap icon) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.progress = progress;
    this.icon = icon;
}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

public String getDescription() {
    return description;
}

public Integer getProgress() {
    return progress;
}

public Bitmap getIcon() {
    return icon;
}

}

GridView适配器覆盖:

static class RecordHolder {
    TextView txtTitle;
    ImageView imageItem;

    public RecordHolder(TextView txtTitle, ImageView imageItem) {
        this.txtTitle = txtTitle;
        this.imageItem = imageItem;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;

    if (row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder(
                (TextView)row.findViewById(R.id.achievement_text),
                (ImageView)row.findViewById(R.id.achievement_image));

        row.setTag(holder);
    }
    else holder = (RecordHolder)row.getTag();

    Achievement achievement = data.get(position);
    holder.txtTitle.setText(achievement.getName());
    holder.imageItem.setImageBitmap(achievement.getIcon());
    return row;
}

Activity中的实施:

ExpandingGridView gridView;
ArrayList<Achievement> gridArray = new ArrayList<Achievement>();
AchievementGridViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    //TODO:Actual Achievement details from server
    Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_person_black_24dp);
    for(int i = 0; i < 5; i++)
    {
        gridArray.add(new Achievement("0", "Achv" + i, "test", 50, icon));
    }

    gridView = (ExpandingGridView) findViewById(R.id.achievement_grid);
    adapter = new AchievementGridViewAdapter(this, R.layout.row_grid_achievements, gridArray);
    gridView.setAdapter(adapter);
}

视觉效果: 网格项目上面应该有一个图标,类似于&#34;个人资料图片&#34; Damn.

2 个答案:

答案 0 :(得分:0)

在适配器的getView()方法中,更改以下行

holder.imageItem.setImageBitmap(achievement.getIcon());

根据以下

holder.imageItem.setBackgroundDrawable(achievement.getIcon());

我希望它有效

答案 1 :(得分:0)

首先,我使用了RecyclerView,而不是使用GridView,使用了GridLayoutManager(http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

为了让我想要的元素显示在网格中,RecyclerView使用适配器并使用自定义布局对每个网格元素进行膨胀,在以下链接中,他们使用CardView作为布局根(http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/

要从Resources获取Drawable实例,我使用以下代码:

    Drawable d1 = ResourceCompat.getDrawable(getResources(), R.drawable.picture, null);
    //or , depending on where your Drawable is,
    Drawable d2 = ResourceCompat.getDrawable(getResources(), R.mipmap.picture, null);

在实际将ImageView设置为特定Drawable的代码中,我使用了代码:

    //Using the Resource ID
    myImageView.setImageResource(myResourceID);
    //Using a Drawable instance
    myImageView.setImageDrawable(myDrawable);

请注意,在上面的AndroidHive链接中,上述两行代码中的任何一行都将在适配器类中使用,在Overrided方法“ onBindViewHolder