Android-自定义TextView和异步加载图片的实现
Android-自定义TextView和异步加载图片的实现
实例:ConstomTextView
实现步骤:
1. 定义一个继承LinearLayout的类:ConstomTextView
2. 在ConstomTextView类中自定义setText()方法
3.在setText方法中,通过TypedArray来获取自定义属性,来设置组件相应的参数
4.如果要在布局中显示出图片就应该定义ImageView,显示出文本就定义TextView,以此类推
5. 最后要将组件通过addView()方法添加到布局当中。
6. 要实现图片异步加载,需要定义一个线程类,通过Handler来进行数据交互,来达到UI的更新
项目运行效果:
2秒过后。。。
源代码:MainActivity.java
package com.wwj.textView; import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /**********************测试数据**********************/ ArrayList<HashMap<String, String>> datas = new ArrayList<HashMap<String,String>>(); HashMap<String, String> hashMap1 = new HashMap<String, String>(); hashMap1.put("type", "image"); hashMap1.put("value", "http://www.taoqao.com/uploads/allimg/111216/1-111216101257.png"); HashMap<String, String> hashMap2 = new HashMap<String, String>(); hashMap2.put("type", "text"); hashMap2.put("value", newsbody); HashMap<String, String> hashMap3 = new HashMap<String, String>(); hashMap3.put("type", "image"); hashMap3.put("value", "http://www.taoqao.com/uploads/allimg/111216/1-111216101257.png"); datas.add(hashMap1); datas.add(hashMap2); datas.add(hashMap3); /*************************************************************************/ //获取自定义组件的引用 ConstomTextView view = (ConstomTextView) findViewById(R.id.textView); //调用ConstomTextView自定义的setText方法 view.setText(datas); } //新闻信息 private final String newsbody = " <p> 今年浙江卫视凭《中国好声音》一举做大" + ",其巨大的影响力直接波及到了各家卫视“跨年晚会”的战略部署。日前" + ",“跨年晚会”概念的鼻祖湖南卫视率先表示“退出跨年烧钱大战”。" + "但据湖南卫视内部人士透露,即使如此,今年的湖南跨年晚会也将会掂出“跨年季”这个概念" + ",“也就是从12月27日到12月31日,连续五天,我们将相继用《百变大咖秀》、《快乐大本营》" + "、《女人如歌》、《天天向上》的特别节目来连续打造这个”季“的概念,直到12月31日的那场晚会。”</p>"; }
源代码:ConstomTextView.java
package com.wwj.textView; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.text.Html; import android.util.AttributeSet; import android.view.Gravity; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ConstomTextView extends LinearLayout{ //上下文对象 private Context mContext; //声明TypedArray的引用 private TypedArray mTypedArray; //布局参数 private LayoutParams params; public ConstomTextView(Context context) { super(context); } public ConstomTextView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; this.setOrientation(LinearLayout.VERTICAL); //从attrs.xml文件中那个获取自定义属性 mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.constomTextView); } public void setText(ArrayList<HashMap<String, String>> datas) { //遍历ArrayList for(HashMap<String, String> hashMap : datas) { //获取key为"type"的值 String type = hashMap.get("type"); //如果value=imaeg if(type.equals("image")){ //获取自定义属性属性 int imagewidth = mTypedArray.getDimensionPixelOffset(R.styleable.constomTextView_image_width, 100); int imageheight = mTypedArray.getDimensionPixelOffset(R.styleable.constomTextView_image_height, 100); ImageView imageView = new ImageView(mContext); params = new LayoutParams(imagewidth, imageheight); params.gravity = Gravity.CENTER_HORIZONTAL; //居中 imageView.setLayoutParams(params); //显示图片 imageView.setImageResource(R.drawable.ic_constom); //将imageView添加到LinearLayout当中 addView(imageView); //启动异步线程更新异步显示图片信息 new DownloadPicThread(imageView, hashMap.get("value")).start(); } else { float textSize = mTypedArray.getDimension(R.styleable.constomTextView_textSize, 16); int textColor = mTypedArray.getColor(R.styleable.constomTextView_textColor, 0xFF0000FF); TextView textView = new TextView(mContext); textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); textView.setText(Html.fromHtml(hashMap.get("value"))); textView.setTextSize(textSize); //设置字体大小 textView.setTextColor(textColor); //设置字体颜色 addView(textView); } } } private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { @SuppressWarnings("unchecked") HashMap<String, Object> hashMap = (HashMap<String, Object>) msg.obj; ImageView imageView = (ImageView) hashMap.get("imageView"); LayoutParams params = new LayoutParams(msg.arg1, msg.arg2); params.gravity = Gravity.CENTER_HORIZONTAL; //居中 imageView.setLayoutParams(params); Drawable drawable = (Drawable) hashMap.get("drawable"); imageView.setImageDrawable(drawable); //显示图片 }; }; /** * 定义一个线程类,异步加载图片 * @author Administrator * */ private class DownloadPicThread extends Thread { private ImageView imageView; private String mUrl; public DownloadPicThread(ImageView imageView, String mUrl) { super(); this.imageView = imageView; this.mUrl = mUrl; } @Override public void run() { // TODO Auto-generated method stub Drawable drawable = null; int newImgWidth = 0; int newImgHeight = 0; try { drawable = Drawable.createFromStream(new URL(mUrl).openStream(), "image"); //对图片进行缩放 newImgWidth = drawable.getIntrinsicWidth() / 3; newImgHeight = drawable.getIntrinsicHeight() / 3; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } //让线程休眠2秒 SystemClock.sleep(2000); //使用Handler更新UI Message msg = handler.obtainMessage(); HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("imageView", imageView); hashMap.put("drawable", drawable); msg.obj = hashMap; msg.arg1 = newImgWidth; msg.arg2 = newImgHeight; handler.sendMessage(msg); } } }
自定义属性:/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="constomTextView"> <attr name="image_width" format="dimension"/> <attr name="image_height" format="dimension"/> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
布局文件:main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:wwj="http://schemas.android.com/apk/res/com.wwj.textView" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.wwj.textView.ConstomTextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" wwj:image_width="200dip" wwj:image_height="52dip"/> </LinearLayout>