好吧,所以我对此处偏头痛,感觉无论我尝试做什么我都会反对。
如何在动态数量的列和行中获取图像视图的网格视图(使用下面的数组适配器从位图中覆盖)并排显示它们之间没有间距?
public class GameAdapter extends BaseAdapter {
//Variable setup
private Context context;
private Bitmap[] values;
//Construct the image parameter, get context, the array of images, and the height/width of them
public GameAdapter(Context context, Bitmap[] _values) {
this.context = context;
this.values = _values;
}
//Convert Dp to pixels for modularity
private int dpToPx(int dp)
{
float density = context.getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int i) {
return values[i];
}
@Override
public long getItemId(int i) {
return values[i].getGenerationId();
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
imageView.setImageBitmap(values[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// int pad = dpToPx(1);
// imageView.setPadding(pad,pad,pad,pad);
// Log.w("Derp", "" + "Height- "+values[i].getHeight());
// Log.w("Derp", "" + "Width- "+values[i].getWidth());
imageView.setLayoutParams(new GridView.LayoutParams(values[i].getWidth(), values[i].getHeight()));
return imageView;
}
}
答案 0 :(得分:2)
请参阅以下网格视图图像:
<强> grid_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
<强> ImageAdapter.java 强>
package com.imagesample;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// all Images in array
public Integer[] mThumbIds = {
R.drawable.p1, R.drawable.p2,
R.drawable.p3, R.drawable.p4,
R.drawable.p5, R.drawable.p6,
R.drawable.p7, R.drawable.p8,
R.drawable.p9, R.drawable.p10,
R.drawable.p11, R.drawable.p12,
R.drawable.p13, R.drawable.p14,
R.drawable.p15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
} }
<强> GridActivity.java 强>
package com.imagesample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class GridActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
希望它可以帮到你很多。
答案 1 :(得分:0)
要在运行时获得最佳性能,您应该执行以下小技巧:使AsyncTask
加载ImageView
并将其设置为null。
final ImageView imageView = (ImageView)
view.findViewById(R.id.image);
// Set the ImageView to null
imageView.setImageBitmap(null);
new AsyncTack<String, Void, Bitmap>() {
protected Bitmap doInBackground(String... params) {
String url = params[0];
Bitmap bitmap = downloadBitmap(url);
return bitmap;
}
// Set the ImageView with the Bitmap downloaded
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}.
}.execute(url);
要动态制作GridView
,您应该阅读这个问题:SO
Android dynamic GridView
GridView grid = new GridView(this);
grid.setId(215236);
grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
grid.setBackgroundColor(Color.WHITE);
grid.setColumnWidth(GridView.AUTO_FIT);
grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
如果要在每个行和列上动态删除间距(垂直和水平),您应该尝试:
gridview.setVerticalSpacing(0);
gridview.setHorizontalSpacing(0);
要使用ImageView
的宽度/高度来设置行/列的宽度/高度,可以将GridView
放入LinearLayout
(设置MATCH_PARENT)并获取此布局以了解其中包含ImageViews
个。{
检查这些答案,了解有关此技巧的更多信息:
Android: How does GridView auto_fit find the number of columns?
希望这就是你要找的。 p>