Skip to content

Commit

Permalink
1.添加了textView显示Html(可以显示图片)
Browse files Browse the repository at this point in the history
2.添加加载网络图片,返回原图的bitmap方法
  • Loading branch information
ychxx committed Dec 25, 2018
1 parent 0c7c60d commit 5497f15
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation project(':ycutilslibrary')
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.0.5'
implementation 'com.github.ychxx:YcMvp:1.02'
implementation 'com.github.ychxx:YcMvp:1.2.0'
/* view依赖注入 */
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/yc/ycutils/ApiService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yc.ycutils;

import com.yc.yclibrary.YcInit;
import com.yc.ycutilslibrary.YcUtilsInit;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;

/**
*
*/

public interface ApiService {
// @Headers(YcInit.OTHER_BASE_URL+":"+"https://bg.fnpsy.com/")
@GET("api/content/info/19")
Observable<String> getHtml();
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/yc/ycutils/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Application;

import com.yc.yclibrary.YcInit;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

Expand All @@ -13,6 +15,6 @@ public class App extends Application{
@Override
public void onCreate() {
super.onCreate();

YcInit.init(this,"https://bg.fnpsy.com/");
}
}
250 changes: 250 additions & 0 deletions app/src/main/java/com/yc/ycutils/test/html/GetHtmlJson.java

Large diffs are not rendered by default.

36 changes: 26 additions & 10 deletions app/src/main/java/com/yc/ycutils/test/html/TestHtmlActivity.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.yc.ycutils.test.html;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.style.ImageSpan;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yc.yclibrary.base.YcAppCompatActivity;
import com.yc.yclibrary.exception.ApiException;
import com.yc.yclibrary.net.BaseObserver;
import com.yc.yclibrary.net.NetTransformer;
import com.yc.yclibrary.net.RetrofitUtils;
import com.yc.ycutils.ApiService;
import com.yc.ycutils.R;

import java.util.ArrayList;
import com.yc.ycutilslibrary.html.YcHtmlImageGetter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
*
Expand All @@ -29,10 +28,27 @@ public class TestHtmlActivity extends YcAppCompatActivity {
protected int getLayoutId() {
return R.layout.test_html_activity;
}
String html = "环境描述:描述建筑配套相关的说明,加强用户对环境因素的喜爱,善用修辞和修<div>所属地址:福建省福州市福能总院煤矿心理医院<br></div><div><br></div><div><img src=\"https://image.wowhua.com/b0708ad4-7f56-9435-dca6-bfa79444e7b8.jpeg\"></div><div><br></div><div><img src=\"https://image.wowhua.com/fc3d458f-01b6-eca3-6c1b-05e11ede5d6a.jpeg\"><br></div>结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦结束了啦啦啦";

@Override
protected void initView(Bundle bundle) {
testHtmlTv.setText(Html.fromHtml(html, new MImageGetter(testHtmlTv, getApplicationContext()), null));

RetrofitUtils.Instance
.getApiService(ApiService.class)
.getHtml()
.compose(NetTransformer.compose())
.subscribe(new BaseObserver<String>() {
@Override
public void onSuccess(String s) {
Gson gson = new Gson();
GetHtmlJson getHtmlJson = gson.fromJson(s, GetHtmlJson.class);
testHtmlTv.setText(Html.fromHtml(getHtmlJson.getData().getText(), new YcHtmlImageGetter(testHtmlTv, getActivity()), null));
}

@Override
public void onFail(ApiException e) {

}
});
// final Handler handler = new Handler() {
// public void handleMessage(Message msg) {
// int what = msg.what;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Base64;
Expand All @@ -16,7 +17,9 @@
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.yc.ycutilslibrary.R;
import com.yc.ycutilslibrary.common.YcEmpty;
import com.yc.ycutilslibrary.common.YcLog;
Expand Down Expand Up @@ -46,15 +49,15 @@ public class YcImgUtils {
*/
public static int IMG_FAIL_RELOAD_NUM = 3;

public static void loadNetImg(Context activity, String imgUrl, ImageView imageView) {
loadNetImg(activity, imgUrl, imageView, 0);
public static void loadNetImg(Context context, String imgUrl, ImageView imageView) {
loadNetImg(context, imgUrl, imageView, 0);
}

public static void loadNetImg(final Context activity, final String imgUrl, final ImageView imageView, final int loadNum) {
if (activity == null || YcEmpty.isEmpty(imgUrl) || imageView == null) {
public static void loadNetImg(final Context context, final String imgUrl, final ImageView imageView, final int loadNum) {
if (context == null || YcEmpty.isEmpty(imgUrl) || imageView == null) {
return;
}
GlideApp.with(activity)
GlideApp.with(context)
// .asGif()//指定加载类型
// .asBitmap()//指定加载类型 git图会变成第一帧静态图
// .asDrawable()//指定加载类型
Expand All @@ -74,7 +77,7 @@ public static void loadNetImg(final Context activity, final String imgUrl, final
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (loadNum < IMG_FAIL_RELOAD_NUM)
loadNetImg(activity, imgUrl, imageView, loadNum + 1);
loadNetImg(context, imgUrl, imageView, loadNum + 1);
return false;
}

Expand Down Expand Up @@ -157,4 +160,19 @@ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWi
return inSampleSize;
}

public interface ImgLoadCall {
void call(Bitmap resource);
}

public static void loadNetImg(Context context, String imgUrl, final ImgLoadCall imgLoadCall) {
GlideApp.with(context)
.asBitmap()
.load(imgUrl)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imgLoadCall.call(resource);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yc.ycutils.test.html;
package com.yc.ycutilslibrary.html;

import android.content.Context;
import android.graphics.Bitmap;
Expand All @@ -11,14 +11,16 @@
import com.yc.ycutilslibrary.file.YcImgUtils;

/**
*
* 用于解决TextView展示Html时图片不显示问题
* eg:textView.setText(Html.fromHtml(htmlData, new YcHtmlImageGetter(textView, context), null));
* htmlData:要显示html字符串
*/

public class MImageGetter implements Html.ImageGetter {
public class YcHtmlImageGetter implements Html.ImageGetter {
private Context mContext;
private TextView textView;

public MImageGetter(TextView text, Context context) {
public YcHtmlImageGetter(TextView text, Context context) {
this.mContext = context;
this.textView = text;
}
Expand All @@ -37,5 +39,4 @@ public void call(Bitmap resource) {
});
return drawable;
}

}

0 comments on commit 5497f15

Please sign in to comment.