1
這個程序目前正在完美地取出並顯示圖像,但是當我嘗試添加其他方法在ImageListAdapter類滑翔像override()
或centerCrop()
或transform()
或placeholder()
,它不起作用。我的意思是它甚至沒有讓我添加這些,它顯示爲紅色,沒有類似的建議,但許多教程建議使用這些方法來修改大小,爲什麼它不起作用。難道我做錯了什麼?謝謝。Transfrom()或重寫()或centerCrop()現在滑翔機器人工作
MainActivity.java
package com.example.imnobody.photosearch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] eatFoodyImages = {
"http://i.imgur.com/rFLNqWI.jpg",
"http://i.imgur.com/C9pBVt7.jpg",
"http://i.imgur.com/rT5vXE1.jpg",
"http://i.imgur.com/aIy5R2k.jpg",
"http://i.imgur.com/MoJs9pT.jpg",
"http://i.imgur.com/S963yEM.jpg",
"http://i.imgur.com/rLR2cyc.jpg",
"http://i.imgur.com/SEPdUIx.jpg",
"http://i.imgur.com/aC9OjaM.jpg",
"http://i.imgur.com/76Jfv9b.jpg",
"http://i.imgur.com/fUX7EIB.jpg",
"http://i.imgur.com/syELajx.jpg",
"http://i.imgur.com/COzBnru.jpg",
"http://i.imgur.com/Z3QjilA.jpg",
};
GridView gridView = (GridView) findViewById(R.id.gridview);
ImageListAdapter imageListAdapter = new ImageListAdapter(MainActivity.this,eatFoodyImages);
gridView.setAdapter(imageListAdapter);
}
}
ImageListAdapter.java
package com.example.imnobody.photosearch;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
/**
* Created by imnobody on 5/8/17.
*/
public class ImageListAdapter extends ArrayAdapter{
private Context context;
private LayoutInflater inflater;
private String[] imageUrls;
public ImageListAdapter(Context context, String[] imageUrls) {
super(context, R.layout.image_main, imageUrls);
this.context = context;
this.imageUrls = imageUrls;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.image_main, parent, false);
}
Glide
.with(context)
.load(imageUrls[position])
.into((ImageView) convertView);
return convertView;
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.imnobody.photosearch.MainActivity">
<GridView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="@+id/gridview"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:gravity="center"
android:stretchMode="columnWidth"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp">
</GridView>
</android.support.constraint.ConstraintLayout>
image_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/imagemain"/>