结果截图:
文章图片
包含三个类文件:CommonUri.java 、DownIoadImage.java 、 MainActivity.java
两个layout文件:activity_main.xmlitem.xml
activity_main.xml文件:
item.xml文件:
【Android之图文混排 (二)源代码】
CommonUri类用于存储Uri
public class CommonUri { public static String PRODUCT_URL = "";
public static String PRODUCT_IMG = "";
}
DownloadImage类用于从服务器下载图片
public class DownloadImage { private String image_path;
public DownloadImage(String image_path) {
// TODO Auto-generated constructor stub
this.image_path = image_path;
}
public void loadImage(final ImageCallBack callBack)
{
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
callBack.getDrawable((Drawable)msg.obj);
}
};
new Thread(new Runnable() {@Override
public void run() {
// TODO Auto-generated method stub
try {
Drawable drawable = Drawable.createFromStream(new URL(image_path).openStream(), "");
Message message = Message.obtain();
message.obj = drawable;
handler.sendMessage(message);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
//接口的回调方式
public interface ImageCallBack
{
public void getDrawable(Drawable drawable);
}}
MainActivity类:
//图文混排,先保证文字出现
public class MainActivity extends Activity { private MyAdapter adapter = null;
private ListView listView = null;
private ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView1);
adapter= new MyAdapter(this);
dialog = new ProgressDialog(this);
dialog.setTitle("提示下载信息");
dialog.setMessage("正在下载,请稍后...");
new MyTask().execute(CommonUri.PRODUCT_URL);
} public class MyAdapter extends BaseAdapter
{
Context context = null;
private LayoutInflater layoutInflater ;
private List
推荐阅读