EditText的富媒体开发(Edittext显示网络图片) 二

  重写Imagegetter

ublic class URLImageParser implements Html.ImageGetter {
    Context c;
    EditText container;
    private LoadingDialog mLoaddingDialog;

    /***
     * 构建URLImageParser将运行AsyncTask,刷新容器
     *
     * @param t
     * @param c
     */
    public URLImageParser(EditText t, Context c) {
        this.c = c;
        this.container = t;
        mLoaddingDialog = new LoadingDialog((Activity) c);
        mLoaddingDialog.show();
    }

    public Drawable getDrawable(String source) {
        URLDrawable urlDrawable = new URLDrawable();
        // 获得实际的源
        ImageGetterAsyncTask asyncTask =
                new ImageGetterAsyncTask(urlDrawable);

        asyncTask.execute(source);

        //返回引用URLDrawable我将改变从src与实际图像标记
        return urlDrawable;
    }

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
        URLDrawable urlDrawable;

        public ImageGetterAsyncTask(URLDrawable d) {
            this.urlDrawable = d;
        }

        @Override
        protected Drawable doInBackground(String... params) {
            String source = params[0];
            return fetchDrawable(source);
        }

        @Override
        protected void onPostExecute(Drawable result) {
            // 设置正确的绑定依据HTTP调用的结果
            LogUtils.debug("图片下载完成");
            mLoaddingDialog.dismiss();
            LogUtils.debug("height=" + result.getIntrinsicHeight());
            LogUtils.debug("width=" + result.getIntrinsicWidth());
            urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight());
            // 改变当前可提取的參考结果从HTTP调用
            urlDrawable.drawable = result;

            // 绘制图像容器
            URLImageParser.this.container.invalidate();

            // For ICS
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()));

            // Pre ICS
            URLImageParser.this.container.setEllipsize(null);
        }

        /***
         * 得到Drawable的URL
         *
         * @param urlString
         * @return
         */
        public Drawable fetchDrawable(String urlString) {
            try {
                InputStream is = fetch(urlString);
                Drawable drawable = Drawable.createFromStream(is, "src");
                drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                        + drawable.getIntrinsicHeight());
                return drawable;
            } catch (Exception e) {
                return null;
            }
        }

        private InputStream fetch(String urlString) throws MalformedURLException, IOException {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(urlString);
            HttpResponse response = httpClient.execute(request);
            return response.getEntity().getContent();
        }
    }


public class URLDrawable extends BitmapDrawable {
    // the drawable that you need to set, you could set the initial drawing
    // with the loading image if you need to
    protected Drawable drawable;

    @Override
    public void draw(Canvas canvas) {
        // override the draw to facilitate refresh function later
        if (drawable != null) {
            drawable.draw(canvas);
        }
    }
}

从EditText里面getText的时候,注意这一点就可以了


String html = Html.toHtml(et_edit_tigan.getText());



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值