|
| 1 | +package com.guiying.news.detail; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.graphics.drawable.BitmapDrawable; |
| 8 | +import android.graphics.drawable.Drawable; |
| 9 | +import android.support.design.widget.CollapsingToolbarLayout; |
| 10 | +import android.text.Html; |
| 11 | +import android.text.method.LinkMovementMethod; |
| 12 | +import android.util.AttributeSet; |
| 13 | +import android.widget.FrameLayout; |
| 14 | +import android.widget.ImageView; |
| 15 | +import android.widget.TextView; |
| 16 | + |
| 17 | +import com.bumptech.glide.Glide; |
| 18 | +import com.bumptech.glide.request.animation.GlideAnimation; |
| 19 | +import com.bumptech.glide.request.target.SimpleTarget; |
| 20 | +import com.guiying.news.R; |
| 21 | +import com.guiying.news.data.bean.MessageDetail; |
| 22 | + |
| 23 | +/** |
| 24 | + * <p>类说明</p> |
| 25 | + * |
| 26 | + * @author 张华洋 2017/7/1 13:18 |
| 27 | + * @version V1.2.0 |
| 28 | + * @name NewsDetailView |
| 29 | + */ |
| 30 | + |
| 31 | +public class NewsDetailView extends FrameLayout implements NewsDetailContract.View { |
| 32 | + |
| 33 | + private boolean isActive = false; |
| 34 | + private NewsDetailContract.Presenter mPresenter; |
| 35 | + private ImageView mToolbarImage; |
| 36 | + private TextView mToolbarText; |
| 37 | + private CollapsingToolbarLayout mCollapsingLayout; |
| 38 | + private TextView mDetailText; |
| 39 | + |
| 40 | + |
| 41 | + public NewsDetailView(Context context) { |
| 42 | + super(context); |
| 43 | + initView(); |
| 44 | + } |
| 45 | + |
| 46 | + public NewsDetailView(Context context, AttributeSet attrs) { |
| 47 | + super(context, attrs); |
| 48 | + initView(); |
| 49 | + } |
| 50 | + |
| 51 | + private void initView() { |
| 52 | + inflate(getContext(), R.layout.view_news_detail, this); |
| 53 | + mToolbarImage = (ImageView) findViewById(R.id.toolbar_image); |
| 54 | + mToolbarText = (TextView) findViewById(R.id.toolbar_text); |
| 55 | + mCollapsingLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_layout); |
| 56 | + mDetailText = (TextView) findViewById(R.id.news_detail_text); |
| 57 | + |
| 58 | + isActive = true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void onAttachedToWindow() { |
| 63 | + super.onAttachedToWindow(); |
| 64 | + isActive = true; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void onDetachedFromWindow() { |
| 69 | + super.onDetachedFromWindow(); |
| 70 | + isActive = false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean isActive() { |
| 75 | + return isActive; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @Override |
| 80 | + public void setPresenter(NewsDetailContract.Presenter presenter) { |
| 81 | + mPresenter = presenter; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void showNewsDetail(MessageDetail detail) { |
| 86 | + mCollapsingLayout.setTitle(detail.getTitle()); |
| 87 | + //设置还没收缩时状态下字体颜色 |
| 88 | + mCollapsingLayout.setExpandedTitleColor(Color.WHITE); |
| 89 | + //设置收缩后Toolbar上字体的颜色 |
| 90 | + mCollapsingLayout.setCollapsedTitleTextColor(Color.WHITE); |
| 91 | + mToolbarText.setText(detail.getImage_source()); |
| 92 | + Glide.with(getContext()) |
| 93 | + .load(detail.getImage()) |
| 94 | + .thumbnail(0.2f) |
| 95 | + .into(mToolbarImage); |
| 96 | + mDetailText.setMovementMethod(LinkMovementMethod.getInstance()); |
| 97 | + mDetailText.setText(Html.fromHtml(detail.getBody(), new Html.ImageGetter() { |
| 98 | + @Override |
| 99 | + public Drawable getDrawable(String source) { |
| 100 | + final URLDrawable urlDrawable = new URLDrawable(); |
| 101 | + Glide.with(getContext()).load(source).asBitmap().into(new SimpleTarget<Bitmap>() { |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { |
| 105 | + urlDrawable.bitmap = resource; |
| 106 | + urlDrawable.setBounds(0, 0, resource.getWidth(), resource.getHeight()); |
| 107 | + mDetailText.invalidate(); |
| 108 | + // 解决图文重叠 |
| 109 | + mDetailText.setText(mDetailText.getText()); |
| 110 | + } |
| 111 | + }); |
| 112 | + return urlDrawable; |
| 113 | + } |
| 114 | + }, null)); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + private class URLDrawable extends BitmapDrawable { |
| 119 | + |
| 120 | + private Bitmap bitmap; |
| 121 | + |
| 122 | + @Override |
| 123 | + public void draw(Canvas canvas) { |
| 124 | + if (bitmap != null) { |
| 125 | + canvas.drawBitmap(bitmap, 0, 0, getPaint()); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments