在我的应用程序中,我使用gridView显示从api获取的图像... 图像的大小完全适合模拟器,但在我的设备中,它们是重叠的,并没有完全显示....我也使用
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
imageView.setLayoutParams(new GridView.LayoutParams((int)(metrics.density*180f),(int)(metrics.density*250f)));
这是我的完整代码......
activity_movie_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="2"
android:id="@+id/gridView"
tools:context="com.example.arti.movieapp.MovieGrid">
MovieGrid.java
public class MovieGrid extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<Movie>> {
private static final int EARTHQUAKE_LOADER_ID = 1;
private String URL_request;
ImageAdapter imageAdapter;
GridView gridView;
DisplayMetrics metrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_grid);
metrics = this.getResources().getDisplayMetrics();
Bundle extras=getIntent().getExtras();
if(extras!=null){
URL_request=extras.getString("url");
}
Log.v("BookListActivity",URL_request+"&");
getSupportLoaderManager().initLoader(EARTHQUAKE_LOADER_ID, null, this);
}
@Override
public Loader<ArrayList<Movie>> onCreateLoader(int id, Bundle args) {
return new MovieLoader(this,URL_request);
}
@Override
public void onLoadFinished(Loader<ArrayList<Movie>> loader, ArrayList<Movie> data) {
Log.v("MovieGrid","onLoadFinish"+data.size());
imageAdapter=new ImageAdapter(this,data,metrics);
Log.v("MovieGrid","onLoadFinish"+data.size());
gridView=(GridView)findViewById(R.id.gridView);
gridView.setAdapter(imageAdapter);
}
@Override
public void onLoaderReset(Loader<ArrayList<Movie>> loader)
{
//gridView.setAdapter(null);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
Context mContext;
// references to our images
private ArrayList<Movie> arraylist;
DisplayMetrics metrics;
public ImageAdapter(Context context, ArrayList<Movie> arraylist,DisplayMetrics metrics) {
mContext=context;
this.arraylist=arraylist;
this.metrics=metrics;
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// getResource() is not recognize here so i pass metrics from MovieGrid to here by constructor
//DisplayMetrics metrics = getResources().getDisplayMetrics();
ImageView imageView;
if (view == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((int)(metrics.density*180f),(int)(metrics.density*250f)));
Log.v("height",(int)(metrics.density*250f)+"");
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) view;
}
String path = arraylist.get(i).getPosterPath();
Log.v("ImageAdapter", arraylist.get(i).getPosterPath());
Picasso.with(mContext).load(path).placeholder(R.drawable.image_not_avail).into(imageView);
return imageView;
}
}
仿真器
设备
我无法理解这里的问题是什么......