From c0753db5735c88b80765d324151c590173dc0276 Mon Sep 17 00:00:00 2001 From: sendtion <524100248@qq.com> Date: Sat, 5 May 2018 20:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B01.3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E5=8D=87=E7=BA=A7Glide=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E6=8F=92=E5=85=A5=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 22 ------- .idea/copyright/profiles_settings.xml | 3 - .idea/misc.xml | 9 +-- app/build.gradle | 24 +++++--- app/src/main/AndroidManifest.xml | 25 ++++++-- .../xrichtextdemo/ui/NewActivity.java | 59 ++++++++++++------ .../xrichtextdemo/util/MyGlideEngine.java | 61 +++++++++++++++++++ .../xrichtextdemo/util/SDCardUtil.java | 59 +++++++++++++++++- app/src/main/res/values/dimens.xml | 3 + app/src/main/res/xml/file_paths.xml | 4 ++ build.gradle | 3 +- gradle/wrapper/gradle-wrapper.properties | 4 +- xrichtext/build.gradle | 14 +++-- xrichtext/proguard-rules.pro | 12 ++-- .../com/sendtion/xrichtext/MyGlideModule.java | 11 ++++ .../sendtion/xrichtext/RichTextEditor.java | 26 +++++--- .../com/sendtion/xrichtext/RichTextView.java | 46 ++++++++------ 17 files changed, 277 insertions(+), 108 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 app/src/main/java/com/sendtion/xrichtextdemo/util/MyGlideEngine.java create mode 100644 app/src/main/res/xml/file_paths.xml create mode 100644 xrichtext/src/main/java/com/sendtion/xrichtext/MyGlideModule.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4a04528..ac59544 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,11 +5,12 @@ @@ -24,7 +25,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 66a7aa3..0f28f70 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,21 +1,27 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 - buildToolsVersion "26.0.2" + compileSdkVersion 27 + buildToolsVersion "27.0.3" defaultConfig { applicationId "com.sendtion.xrichtext" minSdkVersion 14 targetSdkVersion 22 - versionCode 2 - versionName "1.1" + versionCode 5 + versionName "1.3" } + buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + lintOptions { + disable 'GoogleAppIndexingWarning' + baseline file("lint-baseline.xml") + } } dependencies { @@ -23,14 +29,18 @@ dependencies { testImplementation 'junit:junit:4.12' implementation project(':xrichtext') + //implementation 'com.github.sendtion:XRichText:1.2' //implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.android.support:design:26.1.0' - implementation 'com.android.support:cardview-v7:26.1.0' + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:cardview-v7:27.1.1' //图片选择器 https://github.com/donglua/PhotoPicker - implementation 'me.iwf.photopicker:PhotoPicker:0.8.4@aar' + //implementation 'me.iwf.photopicker:PhotoPicker:0.8.4@aar' + //知乎图片选择 https://github.com/zhihu/Matisse + implementation 'com.zhihu.android:matisse:0.4.3' implementation 'io.reactivex:rxjava:1.3.0' implementation 'io.reactivex:rxandroid:1.2.1' + } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4a69487..358f95d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.sendtion.xrichtextdemo"> + - - + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java b/app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java index 99b7354..e9a094f 100644 --- a/app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java +++ b/app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java @@ -2,7 +2,9 @@ import android.app.ProgressDialog; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.graphics.Bitmap; +import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; @@ -22,14 +24,16 @@ import com.sendtion.xrichtextdemo.db.NoteDao; import com.sendtion.xrichtextdemo.util.CommonUtil; import com.sendtion.xrichtextdemo.util.ImageUtils; +import com.sendtion.xrichtextdemo.util.MyGlideEngine; import com.sendtion.xrichtextdemo.util.SDCardUtil; import com.sendtion.xrichtextdemo.util.StringUtils; +import com.zhihu.matisse.Matisse; +import com.zhihu.matisse.MimeType; +import com.zhihu.matisse.internal.entity.CaptureStrategy; -import java.util.ArrayList; import java.util.Date; import java.util.List; -import me.iwf.photopicker.PhotoPicker; import rx.Observable; import rx.Observer; import rx.Subscriber; @@ -43,6 +47,8 @@ public class NewActivity extends BaseActivity implements RichTextEditor.OnDeleteImageListener { private static final String TAG = "NewActivity"; + private static final int REQUEST_CODE_CHOOSE = 23;//定义请求码常量 + private EditText et_new_title; private RichTextEditor et_new_content; private TextView tv_new_time; @@ -318,13 +324,20 @@ private void callGallery(){ // intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");// 相片类型 // startActivityForResult(intent, 1); - //调用第三方图库选择 - PhotoPicker.builder() - .setPhotoCount(5)//可选择图片数量 - .setShowCamera(true)//是否显示拍照按钮 - .setShowGif(true)//是否显示动态图 - .setPreviewEnabled(true)//是否可以预览 - .start(this, PhotoPicker.REQUEST_CODE); + Matisse.from(this) + .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.GIF))//照片视频全部显示MimeType.allOf() + .countable(true)//true:选中后显示数字;false:选中后显示对号 + .maxSelectable(3)//最大选择数量为9 + //.addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K)) + .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))//图片显示表格的大小 + .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)//图像选择和预览活动所需的方向 + .thumbnailScale(0.85f)//缩放比例 + .theme(R.style.Matisse_Zhihu)//主题 暗色主题 R.style.Matisse_Dracula + .imageEngine(new MyGlideEngine())//图片加载方式,Glide4需要自定义实现 + .capture(true) //是否提供拍照功能,兼容7.0系统需要下面的配置 + //参数1 true表示拍照存储在共有目录,false表示存储在私有目录;参数2与 AndroidManifest中authorities值相同,用于适配7.0系统 必须设置 + .captureStrategy(new CaptureStrategy(true,"com.sendtion.xrichtextdemo.fileprovider"))//存储到哪里 + .forResult(REQUEST_CODE_CHOOSE);//请求码 } @Override @@ -334,7 +347,7 @@ protected void onActivityResult(int requestCode, int resultCode, final Intent da if (data != null) { if (requestCode == 1){ //处理调用系统图库 - } else if (requestCode == PhotoPicker.REQUEST_CODE){ + } else if (requestCode == REQUEST_CODE_CHOOSE){ //异步方式插入图片 insertImagesSync(data); } @@ -354,18 +367,21 @@ private void insertImagesSync(final Intent data){ public void call(Subscriber subscriber) { try{ et_new_content.measure(0, 0); - int width = CommonUtil.getScreenWidth(NewActivity.this); - int height = CommonUtil.getScreenHeight(NewActivity.this); - ArrayList photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS); + List mSelected = Matisse.obtainResult(data); //可以同时插入多张图片 - for (String imagePath : photos) { - //Log.i("NewActivity", "###path=" + imagePath); - Bitmap bitmap = ImageUtils.getSmallBitmap(imagePath, width, height);//压缩图片 + for (Uri imageUri : mSelected) { + String imagePath = SDCardUtil.getFilePathFromUri(NewActivity.this, imageUri); + //Log.e(TAG, "###path=" + imagePath); + Bitmap bitmap = ImageUtils.getSmallBitmap(imagePath, screenWidth, screenHeight);//压缩图片 //bitmap = BitmapFactory.decodeFile(imagePath); imagePath = SDCardUtil.saveToSdCard(bitmap); - //Log.i("NewActivity", "###imagePath="+imagePath); + //Log.e(TAG, "###imagePath="+imagePath); subscriber.onNext(imagePath); } + + //测试插入网络图片 http://p695w3yko.bkt.clouddn.com/18-5-5/44849367.jpg + subscriber.onNext("http://p695w3yko.bkt.clouddn.com/18-5-5/30271511.jpg"); + subscriber.onCompleted(); }catch (Exception e){ e.printStackTrace(); @@ -379,14 +395,17 @@ public void call(Subscriber subscriber) { .subscribe(new Observer() { @Override public void onCompleted() { - insertDialog.dismiss(); - et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), " "); + if (insertDialog != null && insertDialog.isShowing()) { + insertDialog.dismiss(); + } showToast("图片插入成功"); } @Override public void onError(Throwable e) { - insertDialog.dismiss(); + if (insertDialog != null && insertDialog.isShowing()) { + insertDialog.dismiss(); + } showToast("图片插入失败:"+e.getMessage()); } diff --git a/app/src/main/java/com/sendtion/xrichtextdemo/util/MyGlideEngine.java b/app/src/main/java/com/sendtion/xrichtextdemo/util/MyGlideEngine.java new file mode 100644 index 0000000..6eaad86 --- /dev/null +++ b/app/src/main/java/com/sendtion/xrichtextdemo/util/MyGlideEngine.java @@ -0,0 +1,61 @@ +package com.sendtion.xrichtextdemo.util; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.widget.ImageView; + +import com.bumptech.glide.Priority; +import com.sendtion.xrichtext.GlideApp; +import com.zhihu.matisse.engine.ImageEngine; + +/** + * 自定义Glide加载引擎,用于知乎图片选择器 + */ +public class MyGlideEngine implements ImageEngine { + @Override + public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) { + GlideApp.with(context) + .asBitmap() // some .jpeg files are actually gif + .load(uri) + .placeholder(placeholder) + .override(resize, resize) + .centerCrop() + .into(imageView); + } + + @Override + public void loadAnimatedGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) { + GlideApp.with(context) + .asBitmap() + .load(uri) + .placeholder(placeholder) + .override(resize, resize) + .centerCrop() + .into(imageView); + } + + @Override + public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) { + GlideApp.with(context) + .load(uri) + .override(resizeX, resizeY) + .priority(Priority.HIGH) + .into(imageView); + } + + @Override + public void loadAnimatedGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) { + GlideApp.with(context) + .asGif() + .load(uri) + .override(resizeX, resizeY) + .priority(Priority.HIGH) + .into(imageView); + } + + @Override + public boolean supportAnimatedGif() { + return true; + } +} diff --git a/app/src/main/java/com/sendtion/xrichtextdemo/util/SDCardUtil.java b/app/src/main/java/com/sendtion/xrichtextdemo/util/SDCardUtil.java index 8012a04..0092794 100644 --- a/app/src/main/java/com/sendtion/xrichtextdemo/util/SDCardUtil.java +++ b/app/src/main/java/com/sendtion/xrichtextdemo/util/SDCardUtil.java @@ -6,9 +6,12 @@ import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; +import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import java.io.File; +import java.io.FileDescriptor; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -19,7 +22,6 @@ public class SDCardUtil { /** * 检查是否存在SDCard - * @return */ public static boolean hasSdcard(){ String state = Environment.getExternalStorageState(); @@ -32,13 +34,12 @@ public static boolean hasSdcard(){ /** * 获得文章图片保存路径 - * @return */ public static String getPictureDir(){ String imageCacheUrl = SDCardRoot + APP_NAME + File.separator; File file = new File(imageCacheUrl); if(!file.exists()) - file.mkdir(); //如果不存在则创建 + file.mkdirs(); //如果不存在则创建 return imageCacheUrl; } @@ -93,6 +94,58 @@ public static String getFilePathByUri(Context context, final Uri uri) { return data; } + /** + * 根据Uri获取真实的文件路径 + * + * @param context + * @param uri + * @return + */ + public static String getFilePathFromUri(Context context, Uri uri) { + if (uri == null) return null; + + ContentResolver resolver = context.getContentResolver(); + FileInputStream input = null; + FileOutputStream output = null; + try { + ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r"); + if (pfd == null) { + return null; + } + FileDescriptor fd = pfd.getFileDescriptor(); + input = new FileInputStream(fd); + + + File outputDir = context.getCacheDir(); + File outputFile = File.createTempFile("image", "tmp", outputDir); + String tempFilename = outputFile.getAbsolutePath(); + output = new FileOutputStream(tempFilename); + + int read; + byte[] bytes = new byte[4096]; + while ((read = input.read(bytes)) != -1) { + output.write(bytes, 0, read); + } + + return new File(tempFilename).getAbsolutePath(); + } catch (Exception ignored) { + + ignored.getStackTrace(); + } finally { + try { + if (input != null){ + input.close(); + } + if (output != null){ + output.close(); + } + } catch (Throwable t) { + // Do nothing + } + } + return null; + } + /** 删除文件 **/ public static boolean deleteFile(String filePath) { File file = new File(filePath); diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 00032ac..b4e7951 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -13,4 +13,7 @@ 18dp 20dp + + 120dp + diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..5412219 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 048bc34..2f7a685 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter()//jcenter仓库 } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0' + classpath 'com.android.tools.build:gradle:3.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -22,6 +22,7 @@ allprojects { repositories { google() jcenter() + maven { url 'https://jitpack.io' } } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fc9b829..831019d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Dec 28 10:00:20 PST 2015 +#Fri May 04 21:37:24 CST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/xrichtext/build.gradle b/xrichtext/build.gradle index 7fa2151..ed2d8a2 100644 --- a/xrichtext/build.gradle +++ b/xrichtext/build.gradle @@ -1,14 +1,14 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 26 - buildToolsVersion "26.0.2" + compileSdkVersion 27 + buildToolsVersion "27.0.3" defaultConfig { minSdkVersion 14 targetSdkVersion 22 - versionCode 2 - versionName "1.1" + versionCode 5 + versionName "1.3" } buildTypes { @@ -23,8 +23,10 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' - implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'com.android.support:appcompat-v7:27.1.1' //图片加载框架 https://github.com/bumptech/glide - implementation 'com.github.bumptech.glide:glide:3.7.0' + //implementation 'com.github.bumptech.glide:glide:3.7.0' + api 'com.github.bumptech.glide:glide:4.7.1' + annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' } diff --git a/xrichtext/proguard-rules.pro b/xrichtext/proguard-rules.pro index 6e89e3b..5044afd 100644 --- a/xrichtext/proguard-rules.pro +++ b/xrichtext/proguard-rules.pro @@ -18,7 +18,11 @@ # Glide -keep public class * implements com.bumptech.glide.module.GlideModule --keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { - **[] $VALUES; - public *; -} \ No newline at end of file +-keep public class * extends com.bumptech.glide.module.AppGlideModule +-keep public enum com.bumptech.glide.load.ImageHeaderParser$** { + **[] $VALUES; + public *; +} + +# for DexGuard only +-keepresourcexmlelements manifest/application/meta-data@value=GlideModule \ No newline at end of file diff --git a/xrichtext/src/main/java/com/sendtion/xrichtext/MyGlideModule.java b/xrichtext/src/main/java/com/sendtion/xrichtext/MyGlideModule.java new file mode 100644 index 0000000..531d3e9 --- /dev/null +++ b/xrichtext/src/main/java/com/sendtion/xrichtext/MyGlideModule.java @@ -0,0 +1,11 @@ +package com.sendtion.xrichtext; + +import com.bumptech.glide.annotation.GlideModule; +import com.bumptech.glide.module.AppGlideModule; + +/** + * 用户生成GlideApp,沿用Glide3版本使用方式 + */ +@GlideModule +public final class MyGlideModule extends AppGlideModule { +} diff --git a/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextEditor.java b/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextEditor.java index c4c6a11..84d17f3 100644 --- a/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextEditor.java +++ b/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextEditor.java @@ -6,7 +6,10 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; import android.os.Environment; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; @@ -20,9 +23,11 @@ import android.widget.ScrollView; import com.bumptech.glide.Glide; -import com.bumptech.glide.request.animation.GlideAnimation; +//import com.bumptech.glide.request.animation.GlideAnimation; +import com.bumptech.glide.request.FutureTarget; import com.bumptech.glide.request.target.SimpleTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; import java.io.File; import java.io.FileNotFoundException; @@ -30,6 +35,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutionException; /** * Created by sendtion on 2016/6/24. @@ -368,7 +374,7 @@ public void addImageViewAtIndex(final int index, Bitmap bmp, String imagePath) { imagePaths.add(imagePath); RelativeLayout imageLayout = createImageLayout(); DataImageView imageView = (DataImageView) imageLayout.findViewById(R.id.edit_imageView); - Glide.with(getContext()).load(imagePath).crossFade().centerCrop().into(imageView); + GlideApp.with(getContext()).load(imagePath).centerCrop().into(imageView); imageView.setAbsolutePath(imagePath); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//裁剪剧中 @@ -399,12 +405,12 @@ public void addImageViewAtIndex(final int index, final String imagePath) { imageView.setAbsolutePath(imagePath); //如果是网络图片 - if (imagePath.startsWith("http")){ + if (imagePath.startsWith("http://") || imagePath.startsWith("https://")){ - Glide.with(getContext()).load(imagePath).asBitmap().dontAnimate() - .into(new SimpleTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { + GlideApp.with(getContext()).asBitmap().load(imagePath).dontAnimate() + .into(new SimpleTarget() { @Override - public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { // 调整imageView的高度,根据宽度等比获得高度 //int imageHeight = allLayout.getWidth() * resource.getHeight() / resource.getWidth(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( @@ -412,22 +418,22 @@ public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { lp.bottomMargin = 10; imageView.setLayoutParams(lp); - Glide.with(getContext()).load(imagePath).crossFade().centerCrop() + GlideApp.with(getContext()).load(imagePath).centerCrop() .placeholder(R.drawable.img_load_fail).error(R.drawable.img_load_fail) .override(Target.SIZE_ORIGINAL, 500).into(imageView); } - }); + }); } else { //如果是本地图片 - //Bitmap bmp = BitmapFactory.decodeFile(imagePath); // 调整imageView的高度,根据宽度等比获得高度 + //Bitmap bmp = BitmapFactory.decodeFile(imagePath); //int imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, 500);//固定图片高度,记得设置裁剪剧中 lp.bottomMargin = 10; imageView.setLayoutParams(lp); - Glide.with(getContext()).load(imagePath).crossFade().centerCrop() + GlideApp.with(getContext()).load(imagePath).centerCrop() .placeholder(R.drawable.img_load_fail).error(R.drawable.img_load_fail).into(imageView); } diff --git a/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextView.java b/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextView.java index a1c9db9..b423c23 100644 --- a/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextView.java +++ b/xrichtext/src/main/java/com/sendtion/xrichtext/RichTextView.java @@ -5,6 +5,9 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -15,11 +18,14 @@ import android.widget.TextView; import com.bumptech.glide.Glide; -import com.bumptech.glide.request.animation.GlideAnimation; +//import com.bumptech.glide.request.animation.GlideAnimation; +import com.bumptech.glide.request.FutureTarget; import com.bumptech.glide.request.target.SimpleTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; import java.util.ArrayList; +import java.util.concurrent.ExecutionException; /** * Created by sendtion on 2016/6/24. @@ -165,35 +171,35 @@ public void addImageViewAtIndex(final int index, final String imagePath) { imageView.setAbsolutePath(imagePath); //如果是网络图片 - if (imagePath.startsWith("http")){ - - Glide.with(getContext()).load(imagePath).asBitmap().dontAnimate() - .into(new SimpleTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { - @Override - public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { - // 调整imageView的高度,根据宽度等比获得高度 - int imageHeight = allLayout.getWidth() * resource.getHeight() / resource.getWidth(); - RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( - LayoutParams.MATCH_PARENT, imageHeight);//固定图片高度,记得设置裁剪剧中 - lp.bottomMargin = 10; - imageView.setLayoutParams(lp); - - Glide.with(getContext()).load(imagePath).crossFade().centerCrop() - .placeholder(R.drawable.img_load_fail).error(R.drawable.img_load_fail) - .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView); - } + if (imagePath.startsWith("http://") || imagePath.startsWith("https://")){ + + GlideApp.with(getContext()).asBitmap().load(imagePath).dontAnimate() + .into(new SimpleTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + //调整imageView的高度,根据宽度等比获得高度 + int imageHeight = allLayout.getWidth() * resource.getHeight() / resource.getWidth(); + RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( + LayoutParams.MATCH_PARENT, imageHeight);//固定图片高度,记得设置裁剪剧中 + lp.bottomMargin = 10; + imageView.setLayoutParams(lp); + + GlideApp.with(getContext()).load(imagePath).centerCrop() + .placeholder(R.drawable.img_load_fail).error(R.drawable.img_load_fail) + .override(Target.SIZE_ORIGINAL, imageHeight).into(imageView); + } }); } else { //如果是本地图片 - Bitmap bmp = BitmapFactory.decodeFile(imagePath); // 调整imageView的高度,根据宽度等比获得高度 + Bitmap bmp = BitmapFactory.decodeFile(imagePath); int imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, imageHeight);//固定图片高度,记得设置裁剪剧中 lp.bottomMargin = 10; imageView.setLayoutParams(lp); - Glide.with(getContext()).load(imagePath).crossFade().centerCrop() + GlideApp.with(getContext()).load(imagePath).centerCrop() .placeholder(R.drawable.img_load_fail).error(R.drawable.img_load_fail).into(imageView); }