新闻客户端(二) 通过自定义view显示新闻信息

 

实例:ConstomTextView

实现步骤:

1. 定义一个继承LinearLayout的类:ConstomTextView

2. 在ConstomTextView类中自定义setText()方法

3.在setText方法中,通过TypedArray来获取自定义属性,来设置组件相应的参数

4.如果要在布局中显示出图片就应该定义ImageView,显示出文本就定义TextView,以此类推

5. 最后要将组件通过addView()方法添加到布局当中。

6. 要实现图片异步加载,需要定义一个线程类,通过Handler来进行数据交互,来达到UI的更新

项目运行效果:

      2秒过后。。。

 

 

源代码:MainActivity.java

  1. package com.wwj.textView;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.os.Bundle;  
  7. import android.app.Activity;  
  8.   
  9. public class MainActivity extends Activity {  
  10.   
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.           
  16.         /**********************测试数据**********************/  
  17.         ArrayList<HashMap<String, String>> datas = new ArrayList<HashMap<String,String>>();  
  18.         HashMap<String, String> hashMap1 = new HashMap<String, String>();  
  19.         hashMap1.put("type""image");  
  20.         hashMap1.put("value""http://www.taoqao.com/uploads/allimg/111216/1-111216101257.png");  
  21.         HashMap<String, String> hashMap2 = new HashMap<String, String>();  
  22.         hashMap2.put("type""text");  
  23.         hashMap2.put("value", newsbody);  
  24.         HashMap<String, String> hashMap3 = new HashMap<String, String>();  
  25.         hashMap3.put("type""image");  
  26.         hashMap3.put("value""http://www.taoqao.com/uploads/allimg/111216/1-111216101257.png");  
  27.         datas.add(hashMap1);  
  28.         datas.add(hashMap2);  
  29.         datas.add(hashMap3);  
  30.       /*************************************************************************/  
  31.         //获取自定义组件的引用  
  32.         ConstomTextView view = (ConstomTextView) findViewById(R.id.textView);  
  33.         //调用ConstomTextView自定义的setText方法  
  34.         view.setText(datas);  
  35.     }  
  36.       
  37.     //新闻信息  
  38.     private final String newsbody = " <p>  今年浙江卫视凭《中国好声音》一举做大" +  
  39.             ",其巨大的影响力直接波及到了各家卫视“跨年晚会”的战略部署。日前" +  
  40.             ",“跨年晚会”概念的鼻祖湖南卫视率先表示“退出跨年烧钱大战”。" +  
  41.             "但据湖南卫视内部人士透露,即使如此,今年的湖南跨年晚会也将会掂出“跨年季”这个概念" +  
  42.             ",“也就是从12月27日到12月31日,连续五天,我们将相继用《百变大咖秀》、《快乐大本营》" +  
  43.             "、《女人如歌》、《天天向上》的特别节目来连续打造这个”季“的概念,直到12月31日的那场晚会。”</p>";  
  44. }  


 

源代码:ConstomTextView.java

  1. package com.wwj.textView;  
  2.   
  3. import java.net.URL;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6.   
  7. import android.content.Context;  
  8. import android.content.res.TypedArray;  
  9. import android.graphics.drawable.Drawable;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.os.SystemClock;  
  13. import android.text.Html;  
  14. import android.util.AttributeSet;  
  15. import android.view.Gravity;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.TextView;  
  19.   
  20. public class ConstomTextView extends LinearLayout{  
  21.   
  22.     //上下文对象  
  23.     private Context mContext;  
  24.     //声明TypedArray的引用  
  25.     private TypedArray mTypedArray;  
  26.     //布局参数  
  27.     private LayoutParams params;  
  28.       
  29.     public ConstomTextView(Context context) {  
  30.         super(context);  
  31.     }  
  32.       
  33.     public ConstomTextView(Context context, AttributeSet attrs) {  
  34.         super(context, attrs);  
  35.         this.mContext = context;  
  36.         this.setOrientation(LinearLayout.VERTICAL);  
  37.         //从attrs.xml文件中那个获取自定义属性  
  38.         mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.constomTextView);  
  39.     }  
  40.       
  41.     public void setText(ArrayList<HashMap<String, String>> datas) {  
  42.         //遍历ArrayList  
  43.         for(HashMap<String, String> hashMap : datas) {  
  44.             //获取key为"type"的值  
  45.             String type = hashMap.get("type");  
  46.             //如果value=imaeg  
  47.             if(type.equals("image")){  
  48.                 //获取自定义属性属性  
  49.                 int imagewidth = mTypedArray.getDimensionPixelOffset(R.styleable.constomTextView_image_width, 100);  
  50.                 int imageheight = mTypedArray.getDimensionPixelOffset(R.styleable.constomTextView_image_height, 100);  
  51.                 ImageView imageView = new ImageView(mContext);  
  52.                 params = new LayoutParams(imagewidth, imageheight);  
  53.                 params.gravity = Gravity.CENTER_HORIZONTAL; //居中  
  54.                 imageView.setLayoutParams(params);  
  55.                 //显示图片  
  56.                 imageView.setImageResource(R.drawable.ic_constom);  
  57.                 //将imageView添加到LinearLayout当中  
  58.                 addView(imageView);  
  59.                 //启动异步线程更新异步显示图片信息  
  60.                 new DownloadPicThread(imageView, hashMap.get("value")).start();  
  61.             }  
  62.             else {  
  63.                 float textSize = mTypedArray.getDimension(R.styleable.constomTextView_textSize, 16);  
  64.                 int textColor = mTypedArray.getColor(R.styleable.constomTextView_textColor, 0xFF0000FF);  
  65.                 TextView textView = new TextView(mContext);  
  66.                 textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));  
  67.                 textView.setText(Html.fromHtml(hashMap.get("value")));  
  68.                 textView.setTextSize(textSize);     //设置字体大小  
  69.                 textView.setTextColor(textColor);   //设置字体颜色  
  70.                 addView(textView);  
  71.             }  
  72.         }  
  73.     }  
  74.       
  75.     private Handler handler = new Handler() {  
  76.         public void handleMessage(android.os.Message msg) {  
  77.             @SuppressWarnings("unchecked")  
  78.             HashMap<String, Object> hashMap = (HashMap<String, Object>) msg.obj;  
  79.             ImageView imageView = (ImageView) hashMap.get("imageView");  
  80.             LayoutParams params = new LayoutParams(msg.arg1, msg.arg2);  
  81.             params.gravity = Gravity.CENTER_HORIZONTAL; //居中  
  82.             imageView.setLayoutParams(params);  
  83.             Drawable drawable = (Drawable) hashMap.get("drawable");  
  84.             imageView.setImageDrawable(drawable);       //显示图片  
  85.         };  
  86.     };  
  87.       
  88.     /** 
  89.      * 定义一个线程类,异步加载图片 
  90.      * @author Administrator 
  91.      * 
  92.      */  
  93.     private class DownloadPicThread extends Thread {  
  94.         private ImageView imageView;  
  95.         private String mUrl;  
  96.           
  97.           
  98.         public DownloadPicThread(ImageView imageView, String mUrl) {  
  99.             super();  
  100.             this.imageView = imageView;  
  101.             this.mUrl = mUrl;  
  102.         }  
  103.   
  104.   
  105.         @Override  
  106.         public void run() {  
  107.             // TODO Auto-generated method stub  
  108.             Drawable drawable = null;  
  109.             int newImgWidth = 0;  
  110.             int newImgHeight = 0;  
  111.             try {  
  112.                 drawable = Drawable.createFromStream(new URL(mUrl).openStream(), "image");  
  113.                 //对图片进行缩放  
  114.                 newImgWidth = drawable.getIntrinsicWidth() / 3;  
  115.                 newImgHeight = drawable.getIntrinsicHeight() / 3;  
  116.             } catch (Exception e) {  
  117.                 // TODO: handle exception  
  118.                 e.printStackTrace();  
  119.             }  
  120.             //让线程休眠2秒  
  121.             SystemClock.sleep(2000);  
  122.             //使用Handler更新UI  
  123.             Message msg = handler.obtainMessage();  
  124.             HashMap<String, Object> hashMap = new HashMap<String, Object>();  
  125.             hashMap.put("imageView", imageView);  
  126.             hashMap.put("drawable", drawable);  
  127.             msg.obj = hashMap;  
  128.             msg.arg1 = newImgWidth;  
  129.             msg.arg2 = newImgHeight;  
  130.             handler.sendMessage(msg);  
  131.         }  
  132.     }  
  133.   
  134. }  


自定义属性:/values/attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="constomTextView">  
  4.         <attr name="image_width" format="dimension"/>  
  5.         <attr name="image_height" format="dimension"/>  
  6.         <attr name="textColor" format="color"/>  
  7.         <attr name="textSize" format="dimension"/>  
  8.     </declare-styleable>  
  9. </resources>  


 

布局文件:main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:wwj="http://schemas.android.com/apk/res/com.wwj.textView"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/LinearLayout1"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <com.wwj.textView.ConstomTextView  
  10.         android:id="@+id/textView"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"   
  13.         wwj:image_width="200dip"  
  14.         wwj:image_height="52dip"/>  
  15.   
  16. </LinearLayout> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值