问题描述
**嗨,我正在尝试从 url 加载图像,如下所示:link
如您所见,图像具有不同的大小。例如,第一张图片的大小为 1920x1280。第二张图片大小为 720x11523,最后一张图片大小为 720x5420。
但是,这在我的应用程序中看起来像这样:
1- The first image is cut off
2- The rest of the images are not centered and do not occupy the entire width of the screen
如您所见,图像具有不同的大小。例如,第一张图片的大小为 1702x1080。第二张图片大小为 1170x1618,最后一张图片大小为 986x1238。
在我的应用程序中看起来像这样:同样的问题,图像被裁剪,没有居中并且没有占据屏幕的整个宽度
这是我的适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
style="@style/CardView.Light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:background="@color/blanco">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/blanco">
<ImageView
android:layout_gravity="center_horizontal"
android:id="@+id/ivPaginas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
我的适配器类:
public class TMOnlineLectorAdaptador extends RecyclerView.Adapter<TMOnlineLectorAdaptador.ViewHolder> {
private ArrayList<TMOLectorClase> tmoLectorClases;
private Context context;
public TMOnlineLectorAdaptador(ArrayList<TMOLectorClase> tmoItems,Context context) {
this.tmoLectorClases = tmoItems;
this.context = context;
}
@NonNull
@Override
public TMOnlineLectorAdaptador.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adaptador_lectortmo,parent,false);
return new TMOnlineLectorAdaptador.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final TMOnlineLectorAdaptador.ViewHolder holder,int position) {
TMOLectorClase tmoLectorClase = this.tmoLectorClases.get(position);
Picasso.get().load(tmoLectorClase.getImg()).into(holder.iv);
}
@Override
public int getItemCount() {
return tmoLectorClases.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView iv;
public ViewHolder(@NonNull View view) {
super(view);
iv = view.findViewById(R.id.ivPaginas);
view.setonClickListener(this);
}
@Override
public void onClick(View view) {
}
}
public void setFilter(ArrayList<TMOLectorClase> newList) {
tmoLectorClases = new ArrayList<>();
tmoLectorClases.addAll(newList);
notifyDataSetChanged();
}
public void updateData(ArrayList<TMOLectorClase> items) {
this.tmoLectorClases = items;
}
}
我的活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Activities.TMO.TMOnlineLector"
android:background="@color/blanco"
android:orientation="horizontal"
android:gravity="center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCapitulosSeleccion"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
我的活动课:
private String url = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tmonline_lector);
url = getIntent().getStringExtra("url");
recyclerView = findViewById(R.id.rvCapitulosSeleccion);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new linearlayoutmanager(this));
adapter = new TMOnlineLectorAdaptador(tmoLectorClases,TMOnlineLector.this);
recyclerView.setAdapter(adapter);
Content content = new Content();
content.execute();
}
private class Content extends AsyncTask<Void,Void,ArrayList<TMOLectorClase>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<TMOLectorClase> items) {
super.onPostExecute(items);
//Actualizar información
adapter.updateData(items);
adapter.notifyDataSetChanged();
}
@Override
protected ArrayList<TMOLectorClase> doInBackground(Void... voids) {
tmoLectorClases.clear();
try {
String nuevaUrl = Jsoup.connect(url).followRedirects(true).execute().url().toExternalForm();
Log.d("Items","Url: " + nuevaUrl);
if(nuevaUrl.contains("/paginated")){
nuevaUrl = nuevaUrl.replaceAll("/paginated","/cascade");
Log.d("items","doInBackground: " + nuevaUrl);
Document doc = Jsoup.connect(nuevaUrl).get();
Log.d("Items","Url: " + doc);
Elements data = doc.select("div.img-container.text-center");
for (Element e : data){
String imgurl = "";
if(e.select("div.img-container.text-center").size() > 0)
imgurl = e.select("img").get(0).attr("data-src");
Log.d("TAG","doInBackground: " + imgurl);
//String imgurl = e.select("img").attr("data-src");
tmoLectorClases.add(new TMOLectorClase(imgurl));
}
}else{
Document doc = Jsoup.connect(nuevaUrl).get();
Elements data = doc.select("div.img-container.text-center");
for (Element e : data){
String imgurl = e.select("img").attr("data-src");
Log.d("items","doInBackground: "+ imgurl);
tmoLectorClases.add(new TMOLectorClase(imgurl));
}
}
} catch (IOException e) {
e.printstacktrace();
}
return tmoLectorClases;
}
}
我该如何解决上述问题?
解决方法
尝试将 Picasso 的 fit()
函数与 centerCrop()
或 centerInside()
结合使用。您可以阅读这些函数 here。
尝试将 onBindViewHolder
更改为以下内容:
public void onBindViewHolder(@NonNull final TMOnlineLectorAdaptador.ViewHolder holder,int position) {
TMOLectorClase tmoLectorClase = this.tmoLectorClases.get(position);
Picasso.with(context)
.load(tmoLectorClase.getImg())
.fit()
.centerCrop()
.into(holder.iv);
}