1. How to download images from the Internet through HTTP
Construct a URL object through an address, get the httpurlconnection object from a URL object, get inputstream, and use inputstream to construct a bitmap object.
public Bitmap getBitmapFromUrl(String url) {Bitmap bitmap = null;URL BitmapUrl=null;try {BitmapUrl = new URL(url);} catch (MalformedURLException e1) {e1.printStackTrace();}try {HttpURLConnection conn = (HttpURLConnection) BitmapUrl.openConnection();conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (IOException e) {e.printStackTrace();}return bitmap;}
Pass the returned bitmap object as a parameter to the setimagebitmap of imageview to display the image in the imageview control.
3. How to Write XML files for each Weibo Entry Layout
There is a pile of pictures with a header on the left, and a pile of pictures on the right. The top of the right is the nickname plus time, and the bottom is the content. If there is an image, a film will be added, and a frame will be shown below, box the forwarded items, including nicknames, content and images.
There are only a few forwarded content. What is the size of this box? Also, Android provides a tool to make images that can be stretched freely. 9.png
3.1. 9.png Implementation of extensible Images
In the tools folder of the android SDK, there is a draw9patch. bat tool, open a PNG file, and draw a black line with the mouse on the left and top, you can see that the horizontal green area indicates the area that expands when it is stretched up and down, vertical refers to the area expanded during Left and Right stretching, and the area not included indicates that the area remains unchanged during stretching.
3.2 Implementation of the layout Interface
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Global is a linearlayout, the left is the Avatar, the right is a bunch of other things --> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "horizontal"> <! -- Avatar on the left + V authentication --> <relativelayout Android: layout_width = "55.0dip" Android: layout_height = "55.0dip" Android: layout_weight = "0.0"> <! -- Avatar, Android: scaletype = "fitcenter", center-sized display --> <imageview Android: Id = "@ + ID/home_headicon" Android: background = "@ drawable/headicon" Android: layout_width = "45.0dip" Android: layout_height = "45.0dip" Android: scaletype = "fitcenter"/> <! -- V authentication: place it in the lower right foot of the parent window --> <imageview Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: id = "@ + ID/vertify" Android: src = "@ drawable/V" Android: layout_alignparentright = "true" Android: layout_alignparentbottom = "true"> </imageview> </relativelayout> <! -- A bunch of data on the right, represented by a row like a table --> <linearlayout Android: Orientation = "vertical" Android: Id = "@ + ID/lyrightlayout" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margin = "5.0dip"> <! -- Display the top line of the nickname time --> <linearlayout Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_marginright = "3.0dip"> <! -- Display nickname on the left --> <textview Android: textsize = "15.0sp" Android: textcolor = "# FFFF00" Android: Id = "@ + ID/ltmusername" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "nickname"/> <! -- There is another small image --> <relativelayout Android: gravity = "right" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_marginright = "3.0dip" Android: layout_weight = "1.0"> <! -- Time --> <textview Android: textsize = "12.0sp" Android: textcolor = "# FFFF00" Android: layout_gravity = "right" Android: id = "@ + ID/ltmdate" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "3.0dip" Android: text = "time" Android: layout_alignparentright = "true"/> <! -- Time Image --> <imageview Android: Id = "@ + ID/ltmtimepic" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "3.0dip" Android: src = "@ drawable/timepic" Android: layout_toleftof = "@ ID/ltmdate" Android: layout_aligntop = "@ ID/ltmdate"/> </relativelayout> </linearlayout> <! -- Main content of Weibo --> <textview Android: textsize = "20366sp" Android: textcolor = "# fffff0" Android: Id = "@ + ID/lsmcontent" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margintop = "10.0dip" Android: text = "Weibo content displayed here"/> <! -- Weibo images, if any --> <imageview Android: Id = "@ + ID/lsmweibopic" Android: visibility = "gone" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <! -- If it is a forwarded microblog, the source microblog is shown below, and the background is a scalable image --> <linearlayout Android: Orientation = "vertical" Android: id = "@ + ID/sublayout" Android: Background = "@ drawable/popup" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_marginbottom = "4.0dip"> <! -- Content of the source Weibo --> <textview Android: textsize = "18366sp" Android: textcolor = "#00 FFFF" Android: Id = "@ + ID/lsmretweedweibo" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margintop = "3.0dip" Android: layout_marginbottom = "3.0dip" Android: TEXT = "Forward Weibo source Weibo"/> <! -- Source Weibo image, if any --> <imageview Android: Id = "@ + ID/subweibopic" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>
Replace the code in getview with the following:
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {/*Log.i("myweibo", "listadpter--->getView position:" + position);TextView tv1 = new TextView(ctx);tv1.setText(adplist.get(position).getUser().getName() + ":"+ adplist.get(position).getText() + "\npic:"+ adplist.get(position).getBmiddle_pic());return tv1;*/Status tmpstu = adplist.get(position);convertView = inflater.inflate(R.layout.homelistitem, null);TextView tv1 = (TextView)convertView.findViewById(R.id.ltmUserName);tv1.setText(tmpstu.getUser().getName());TextView tv2 = (TextView)convertView.findViewById(R.id.lsmContent);tv2.setText(tmpstu.getText());ImageView iv1 = (ImageView)convertView.findViewById(R.id.home_headicon);if(!(tmpstu.getUser().getProfileImageURL()).equals("")){iv1.setImageBitmap(WeiboUtil.getBitmapFromUrl(tmpstu.getUser().getProfileImageURL()));}ImageView iv2 = (ImageView)convertView.findViewById(R.id.lsmweiboPic);if(tmpstu.getThumbnail_pic()!=""){iv2.setImageBitmap(WeiboUtil.getBitmapFromUrlString(tmpstu.getThumbnail_pic()));iv2.setVisibility(View.VISIBLE);}LinearLayout sublay = (LinearLayout)convertView.findViewById(R.id.subLayout);Status retsta = tmpstu.getRetweeted_status();if(retsta==null){sublay.setVisibility(View.GONE);}else{TextView tv3 = (TextView)convertView.findViewById(R.id.lsmretweedweibo);tv3.setText("@"+retsta.getUser().getName()+":"+retsta.getText());if(retsta.getThumbnail_pic()!=""){ImageView iv3 = (ImageView)convertView.findViewById(R.id.subweiboPic);iv3.setImageBitmap(WeiboUtil.getBitmapFromUrlString(retsta.getThumbnail_pic()));}}return convertView;}
See the results:
The background is black, and the font is a little bigger... but you should have it all!