我想准备StaggeredGridView。我准备好以下代码:
MainActivity:
public class MainActivity extends AppCompatActivity {
private StaggeredGridLayoutManager gaggeredGridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
List<ItemData> gaggeredList = getListItemData();
MyStaggeredGridViewAdapter rcAdapter = new MyStaggeredGridViewAdapter(MainActivity.this, gaggeredList);
recyclerView.setAdapter(rcAdapter);
}
private List<ItemData> getListItemData() {
List<ItemData> listViewItems = new ArrayList<ItemData>();
listViewItems.add(new ItemData(1, R.drawable.scene_1, "Scene 1", "Description", 11, 30));
listViewItems.add(new ItemData(2, R.drawable.scene_2, "Scene 2", "Description", 11, 30));
listViewItems.add(new ItemData(3, R.drawable.scene_3, "Scene 3", "Description", 11, 30));
listViewItems.add(new ItemData(4, R.drawable.scene_4, "Scene 4", "Description", 11, 30));
listViewItems.add(new ItemData(5, R.drawable.scene_5, "Scene 5", "Description", 11, 30));
listViewItems.add(new ItemData(6, R.drawable.scene_6, "Scene 6", "Description", 11, 30));
listViewItems.add(new ItemData(7, R.drawable.scene_7, "Scene 7", "Description", 11, 30));
listViewItems.add(new ItemData(8, R.drawable.scene_8, "Scene 8", "Description", 11, 30));
listViewItems.add(new ItemData(9, R.drawable.scene_9, "Scene 9", "Description", 11, 30));
return listViewItems;
}
}
activity_main.xml中
<android.support.v7.widget.RecyclerView
android:id="@+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
item_xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/ivItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/tvItemPrimaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Primary Text"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/tvItemSecondaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvItemPrimaryText"
android:text="Secondary Text"
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:layout_below="@+id/tvItemSecondaryText"
android:gravity="center_vertical">
<ImageView
android:id="@+id/ivItemFavourites"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginRight="8dp"
android:src="@drawable/ic_favorite_24dp" />
<TextView
android:id="@+id/tvItemLikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="23" />
<ImageView
android:id="@+id/ivItemComments"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginRight="8dp"
android:src="@drawable/ic_comment_24dp" />
<TextView
android:id="@+id/tvItemComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="23" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
MyStaggeredGridViewAdapter
public class MyStaggeredGridViewAdapter extends RecyclerView.Adapter<GridViewItemHolder> {
private List<ItemData> itemList;
private Context context;
public MyStaggeredGridViewAdapter(Context context, List<ItemData> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public GridViewItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xml, null);
GridViewItemHolder rcv = new GridViewItemHolder(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(GridViewItemHolder holder, int position) {
ItemData itemData = itemList.get(position);
holder.getmImageView().setImageResource(itemData.getImageUrl());
holder.getmImageView().setImageBitmap(scaleToActualAspectRatio(holder.getmImageView().getWidth(), itemData.getImageUrl()));
holder.getmTextViewPrimaryText().setText(itemData.getPrimaryText());
holder.getmTextViewSecondaryText().setText(itemData.getSecondaryText());
holder.getmTextViewLikes().setText(String.valueOf(itemData.getLikes()));
holder.getmTextViewComments().setText(String.valueOf(itemData.getComments()));
}
@Override
public int getItemCount() {
return this.itemList.size();
}
public Bitmap scaleToActualAspectRatio(int width, int resId) {
}
}
我正在寻找一些逻辑。
要求:我会根据imageView
的宽度设置两列,并设置图像的高度。图像的纵横比不应受到干扰。
那么,为什么要用我的scaleToActualAspectRatio
方法编写什么逻辑?
简而言之,我想要一个从我这里获取width和resourceId并相应地缩放Bitmap而不会丢失高宽比的方法。我已经在使用像android:adjustViewBounds="true"
和android:scaleType="fitCenter"
这样的ImageView属性
ellipse(mouseX, mouseY, radius, radius);
但希望有效地加载大图像和大量图像。
感谢。