Android新浪微博开发(四)主UI的完善

1 如何通过http协议从网上下载图片

  通过地址构造URL对象,同过URL对象得到HttpURLConnection 对象,然后得到InputStream ,再用InputStream 构造Bitmap 对象。

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;
	}

将返回的Bitmap对象作为参数传入ImageView的setImageBitmap中,就可以在ImageView控件中显示图像了。

3 每个微博条目布局的xml文件怎么写

左边一个头标的图片,右边一堆;右边最上边是昵称加时间,然后下边是内容,有图片的话还会加上图片,然后下边是个框,框住转发的东西包括昵称,内容加图片。

转发的内容有的多有的少啊,这个框该多大呢?还好Android提供了个工具可以制作自由拉伸的图片.9.png图片

3.1 可拉伸的图片,.9.png图片的实现

 在Android的sdk的tools文件夹中,有个draw9patch.bat工具,打开一个png文件,然后有鼠标在左和上方画黑线,则可以看出水平绿色的区域表示当上下拉伸时扩展的区域,垂直的表示左右拉伸时扩展的区域,没被包含的表示拉伸时保持不变

3.2 布局界面的实现

 

<?xml version="1.0" encoding="utf-8"?>
<!-- 全局是个LinearLayout,左边是头像,右边是一堆别的东西 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:orientation="horizontal">
	<!-- 左边的头像+v认证 -->
	<RelativeLayout android:layout_width="55.0dip"
		android:layout_height="55.0dip" android:layout_weight="0.0">
		<!--头像, android:scaleType="fitCenter",变大小居中显示 -->
		<ImageView android:id="@+id/home_headicon"
			android:background="@drawable/headicon" android:layout_width="45.0dip"
			android:layout_height="45.0dip" android:scaleType="fitCenter" />
		<!-- v认证,将其置于父窗口的右下脚 -->
		<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>
	<!-- 右边的一堆,如同表格似的一行一行的表示 -->
	<LinearLayout android:orientation="vertical" android:id="@+id/lyRightLayout"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:layout_margin="5.0dip">
		<!-- 用来显示昵称时间的最上面一行 -->
		<LinearLayout android:orientation="horizontal"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:layout_marginRight="3.0dip">
			<!--左边显示昵称 -->
			<TextView android:textSize="15.0sp" android:textColor="#ffff00"
				android:id="@+id/ltmUserName" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="昵称" />
			<!-- 时间还有一个小图片 -->
			<RelativeLayout android:gravity="right"
				android:layout_width="fill_parent" android:layout_height="wrap_content"
				android:layout_marginRight="3.0dip" android:layout_weight="1.0">
				<!--时间 -->
				<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="时间"
					android:layout_alignParentRight="true" />
				<!--时间图片 -->
				<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>
		<!--微博的主要内容 -->
		<TextView android:textSize="20.0sp" android:textColor="#fffff0"
			android:id="@+id/lsmContent" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:layout_marginTop="10.0dip"
			android:text="这里显示微博的内容" />
		<!-- 微博的图片,如果有的话 -->
		<ImageView android:id="@+id/lsmweiboPic" android:visibility="gone"
			android:layout_width="wrap_content" android:layout_height="wrap_content" />
		<!-- 如果是转发的微博的话,源微博显示在下面,背景就是可伸缩的图片 -->
		<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">
			<!--源微博的内容 -->
			<TextView android:textSize="18.0sp" android:textColor="#00ffff"
				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="转发微博的源微博" />
			<!-- 源微博的图片,如果有的话 -->
			<ImageView android:id="@+id/subweiboPic"
				android:layout_width="wrap_content" android:layout_height="wrap_content" />
		</LinearLayout>
	</LinearLayout>
</LinearLayout>

 

将getView中的代码替换如下:

@Override
	public 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;

	}


看看效果:

就是背景黑了点,字体大了点.......但该有的倒是都有了啊!

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值