android listview展示圖片


最近學習android開發,感觸頗多,和網站開發對比,還是有很大的差距,在這里記錄一下。

android listview展示圖片

在網站開發上,展示圖片非常簡單,一個HTML img標簽就搞定,加上服務器無非就是在view里動態展示,或者用ajax動態獲取,或者用vue等動態獲取js展示。

但在android上就非常麻煩,先要把網絡圖片下載,轉換成流文件,再轉換成drawable資源文件才能在app上顯示,里面的xml各種配置,對於一個寫慣HTML的人來說很煩。

主要步驟。

假設在一個Listshow的二級頁面展示,

首先要准備的東西,

Adapter,Adapter是連接后端數據和前端顯示的適配器接口,

一些自定義的類,下載圖片的類imageloader,緩存的類memorycache,存文件的類filecache

xml文件,主列表的xml,listview的xml,每次下載圖片覆蓋的drawable資源xml

具體代碼:

主要的activity

package com.example.huibeb;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.huibeb.model.ListmAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Listshow extends AppCompatActivity {
private List<Map<String, Object>> cats = new ArrayList<Map<String, Object>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listshow);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 4; i++) {
HashMap<String, String> map = new HashMap<String, String>();

map.put("listname", "暹羅貓"+i);
map.put("listimgs", "http://img.bjyiyoutech.com/appimage/5de78277d60ee.jpg");
songsList.add(map);
}

ListmAdapter adapter = new ListmAdapter(this,songsList);
((ListView) findViewById(R.id.listlist)).setAdapter(adapter);
}
}

ListmAdapter,Adapter是連接后端數據和前端顯示的適配器接口,
package com.example.huibeb.model;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.huibeb.R;
import com.example.huibeb.help.ImageLoader;
import java.util.ArrayList;
import java.util.HashMap;

public class ListmAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; //用來下載圖片的類,后面有介紹

public ListmAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d; //賦值列表
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.lists, null);

TextView listname = (TextView)vi.findViewById(R.id.listname); // 標題
ImageView listimg=(ImageView)vi.findViewById(R.id.listimg); // 縮略圖

HashMap<String, String> listm = new HashMap<String, String>();
listm = data.get(position);
// 設置ListView的相關值
String aa = listm.get("listname");// 標題
listname.setText(listm.get("listname"));// 縮略圖
imageLoader.DisplayImage(listm.get("listimgs"), listimg);//下載並返回圖片資源
return vi;

}

}
memorycache緩存MemoryCache
package com.example.huibeb.help;
import android.graphics.Bitmap;

import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MemoryCache {
private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());//軟引用

public Bitmap get(String id){
if(!cache.containsKey(id))
return null;
SoftReference<Bitmap> ref=cache.get(id);
return ref.get();
}

public void put(String id, Bitmap bitmap){
cache.put(id, new SoftReference<Bitmap>(bitmap));
}

public void clear() {
cache.clear();
}

}
file緩存FileCache
package com.example.huibeb.help;
import android.content.Context;

import java.io.File;

public class FileCache {

private File cacheDir;

public FileCache(Context context){
//找一個用來緩存圖片的路徑
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(context.getExternalCacheDir(),"huibenb");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}

public File getFile(String url){

String filename=String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
return f;

}

public void clear(){
File[] files=cacheDir.listFiles();
if(files==null)
return;
for(File f:files)
f.delete();
}


}

xml文件
主activity布局文件 activity_listshow.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Listshow">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

</LinearLayout>
</LinearLayout>

<ListView
android:id="@+id/listlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
listView內容文件lists.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:id="@+id/listimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>


<TextView
android:id="@+id/listname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>

</LinearLayout>
</LinearLayout>
每次下載圖片要替換的drawable資源文件listimgtest.xml,注意這個要放在drawable文件夾下,@drawable/ihblogo是一個jpg圖片,默認加載的圖片請自行添加
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ihblogo"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:mipMap="false"
android:tileMode="disabled"
/>
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM