Skip to content

Commit

Permalink
add load() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Jun 14, 2018
1 parent a6cbd1f commit 3335ffd
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 318 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public GlideImageLoader load(String url, @DrawableRes int placeholder, @NonNull
}

protected GlideImageLoader loadImage(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation) {
if (obj instanceof String && url.startsWith(HTTP)) {
if (obj instanceof String) {
url = (String) obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.bumptech.glide.load.Transformation;
import com.sunfusheng.glideimageview.progress.OnProgressListener;
import com.sunfusheng.glideimageview.transformation.CircleTransformation;
import com.sunfusheng.glideimageview.transformation.RadiusTransformation;

/**
* @author sunfusheng on 2017/11/10.
Expand Down Expand Up @@ -58,7 +59,7 @@ public void load(String url, @DrawableRes int placeholder, int radius) {
}

public void load(String url, @DrawableRes int placeholder, int radius, OnProgressListener onProgressListener) {
load(url, placeholder, new CircleTransformation(), onProgressListener);
load(url, placeholder, new RadiusTransformation(getContext(), radius), onProgressListener);
}

public void load(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation, OnProgressListener onProgressListener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package com.sunfusheng.glideimageview.sample;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.sunfusheng.glideimageview.GlideImageLoader;
import com.sunfusheng.glideimageview.GlideImageView;
import com.sunfusheng.glideimageview.progress.CircleProgressView;
import com.sunfusheng.glideimageview.sample.about.AboutActivity;
import com.sunfusheng.glideimageview.sample.image.SingleImageActivity;

import java.util.Random;

import static com.sunfusheng.glideimageview.sample.image.SingleImageActivity.KEY_IMAGE_URL;
import static com.sunfusheng.glideimageview.sample.image.SingleImageActivity.KEY_IMAGE_URL_THUMBNAIL;

/**
* Created by sunfusheng on 2017/6/3.
Expand All @@ -43,13 +27,8 @@ public class MainActivity extends BaseActivity {
GlideImageView image24;

GlideImageView image31;
GlideImageView image32;
GlideImageView image33;
GlideImageView image34;

GlideImageView image41;
CircleProgressView progressView1;
GlideImageView image42;
GlideImageView image32;
CircleProgressView progressView2;

TextView draggableView1;
Expand All @@ -58,8 +37,6 @@ public class MainActivity extends BaseActivity {
String url1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497688355699&di=ea69a930b82ce88561c635089995e124&imgtype=0&src=http%3A%2F%2Fcms-bucket.nosdn.127.net%2Ff84e566bcf654b3698363409fbd676ef20161119091503.jpg";
String url2 = "http://img1.imgtn.bdimg.com/it/u=4027212837,1228313366&fm=23&gp=0.jpg";

public static boolean isLoadAgain = false; // Just for fun when loading images!

public static final String cat = "https://raw.githubusercontent.com/sfsheng0322/GlideImageView/master/screenshot/cat.jpg";
public static final String cat_thumbnail = "https://raw.githubusercontent.com/sfsheng0322/GlideImageView/master/screenshot/cat_thumbnail.jpg";

Expand All @@ -86,131 +63,43 @@ protected void onCreate(Bundle savedInstanceState) {
image24 = findViewById(R.id.image24);

image31 = findViewById(R.id.image31);
image32 = findViewById(R.id.image32);
image33 = findViewById(R.id.image33);
image34 = findViewById(R.id.image34);

image41 = findViewById(R.id.image41);
progressView1 = findViewById(R.id.progressView1);
image42 = findViewById(R.id.image42);
image32 = findViewById(R.id.image32);
progressView2 = findViewById(R.id.progressView2);

draggableView1 = findViewById(R.id.draggableView1);
draggableView2 = findViewById(R.id.draggableView2);
draggableView1.setOnClickListener(v -> startActivity(new Intent(mContext, RecyclerViewActivity.class)));
draggableView2.setOnClickListener(v -> startActivity(new Intent(mContext, NineImageViewActivity.class)));

isLoadAgain = new Random().nextInt(3) == 1;

line1();
line2();
line3();
line41();
line42();
}

private void line1() {
image11.loadImage(url1, R.color.placeholder).listener((imageUrl, bytesRead, totalBytes, isDone, exception) ->
Log.d("--->image11", "bytesRead: " + bytesRead + " totalBytes: " + totalBytes + " isDone: " + isDone));

image12.setCircle(true);
image12.setBorderWidth(5);
image12.setBorderColor(getResources().getColor(R.color.transparent20));
image12.loadImage(url1, R.color.placeholder);

image13.setCornerRadius(10);
image13.setBorderWidth(2);
image13.setBorderColor(getResources().getColor(R.color.blue));
image13.setPressedModeEnabled(true);
image13.setPressedBorderWidth(2);
image13.setPressedBorderColor(getResources().getColor(R.color.blue));
image13.setPressedMaskColor(getResources().getColor(R.color.transparent20));
image13.loadImage(url1, R.color.placeholder);
image13.setOnClickListener(v -> {
});

image14.setCircle(true);
image14.setBorderWidth(2);
image14.setBorderColor(getResources().getColor(R.color.orange));
image14.setPressedBorderWidth(2);
image14.setPressedBorderColor(getResources().getColor(R.color.orange));
image14.setPressedMaskColor(Color.parseColor("#4df57c00"));
image14.loadImage(url1, R.color.placeholder);
image14.setOnClickListener(v -> {
image11.load(url1);
image12.loadCircle(url1);
image13.load(url2, R.color.placeholder);
image14.load(url2, R.color.placeholder, 10, (percentage, bytesRead, totalBytes) -> {
Log.d("--->", "percentage: " + percentage + " totalBytes: " + totalBytes + " bytesRead: " + bytesRead);
});
}

private void line2() {
image21.loadImage(url2, R.color.placeholder);
image22.loadImage(url2, R.color.placeholder);
image23.loadImage(url2, R.color.placeholder);
image24.loadImage(url2, R.color.placeholder);
}

private void line3() {
image31.loadLocalImage(R.drawable.gif_robot_walk, R.mipmap.ic_launcher);

GlideImageLoader.create(image32).load(gif1, new RequestOptions());
image32.loadCircleImage(gif1, R.mipmap.ic_launcher).listener((percent, isDone, exception) ->
Log.d("--->image32", "percent: " + percent + " isDone: " + isDone));

image33.loadImage(gif2, R.mipmap.ic_launcher);
image34.loadImage(gif3, R.mipmap.ic_launcher);
}

private void line41() {
image41.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SingleImageActivity.class);
intent.putExtra(KEY_IMAGE_URL, cat);
intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, cat_thumbnail);
ActivityOptionsCompat compat = ActivityOptionsCompat
.makeSceneTransitionAnimation(MainActivity.this, image41, getString(R.string.transitional_image));
ActivityCompat.startActivity(MainActivity.this, intent, compat.toBundle());
});

RequestOptions requestOptions = image41.requestOptions(R.color.placeholder).centerCrop();
if (isLoadAgain) {
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
}
private void line3() {

// 第一种方式加载
image41.load(cat_thumbnail, requestOptions).listener((percent, isDone, exception) -> {
if (exception != null && !TextUtils.isEmpty(exception.getMessage())) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
progressView1.setProgress(percent);
progressView1.setVisibility(isDone ? View.GONE : View.VISIBLE);
});
}

private void line42() {
image42.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SingleImageActivity.class);
intent.putExtra(KEY_IMAGE_URL, girl);
intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, girl_thumbnail);
ActivityOptionsCompat compat = ActivityOptionsCompat
.makeSceneTransitionAnimation(MainActivity.this, image42, getString(R.string.transitional_image));
ActivityCompat.startActivity(MainActivity.this, intent, compat.toBundle());
});

RequestOptions requestOptions = image42.requestOptions(R.color.placeholder).centerCrop();
if (isLoadAgain) {
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
}

// 第二种方式加载:可以解锁更多功能
GlideImageLoader imageLoader = image42.getImageLoader();
imageLoader.setOnGlideImageViewListener(girl_thumbnail, (percent, isDone, exception) -> {
if (exception != null && !TextUtils.isEmpty(exception.getMessage())) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
progressView2.setProgress(percent);
progressView2.setVisibility(isDone ? View.GONE : View.VISIBLE);
});
imageLoader.requestBuilder(girl_thumbnail, requestOptions)
.transition(DrawableTransitionOptions.withCrossFade())
.into(image42);
}
// Intent intent = new Intent(MainActivity.this, SingleImageActivity.class);
// intent.putExtra(KEY_IMAGE_URL, cat);
// intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, cat_thumbnail);
// ActivityOptionsCompat compat = ActivityOptionsCompat
// .makeSceneTransitionAnimation(MainActivity.this, image41, getString(R.string.transitional_image));
// ActivityCompat.startActivity(MainActivity.this, intent, compat.toBundle());

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand All @@ -228,5 +117,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
public void onBindViewHolder(ViewHolder viewHolder, int position) {
NewsModel entity = list.get(position);
viewHolder.tvTitle.setText(entity.getTitle());
viewHolder.imageView.loadImage(entity.getImage_url(), R.color.placeholder);
viewHolder.imageView.load(entity.getImage_url(), R.color.placeholder);

viewHolder.llRootView.setOnClickListener(v -> Toast.makeText(mContext, entity.getTitle(), Toast.LENGTH_SHORT).show());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sunfusheng.glideimageview.sample.image;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
Expand All @@ -11,21 +10,12 @@
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.sunfusheng.glideimageview.GlideImageLoader;
import com.sunfusheng.glideimageview.progress.CircleProgressView;
import com.sunfusheng.glideimageview.sample.R;
import com.sunfusheng.glideimageview.sample.widget.NineImageView.ImageAttr;
import com.sunfusheng.glideimageview.util.DisplayUtil;

import java.util.List;

Expand Down Expand Up @@ -75,29 +65,29 @@ public Object instantiateItem(ViewGroup container, int position) {
ImageAttr attr = images.get(position);
String url = TextUtils.isEmpty(attr.thumbnailUrl) ? attr.url : attr.thumbnailUrl;

GlideImageLoader imageLoader = GlideImageLoader.create(photoView);
imageLoader.setOnGlideImageViewListener(url, (percent, isDone, exception) -> {
progressView.setProgress(percent);
progressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
});
RequestOptions requestOptions = imageLoader.requestOptions(R.color.placeholder)
.centerCrop()
.skipMemoryCache(false)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
RequestBuilder<Drawable> requestBuilder = imageLoader.requestBuilder(url, requestOptions)
.transition(DrawableTransitionOptions.withCrossFade());
requestBuilder.into(new SimpleTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
if (resource.getIntrinsicHeight() > DisplayUtil.getScreenHeight(mContext)) {
photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
requestBuilder.into(photoView);
}
});

container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// GlideImageLoader imageLoader = GlideImageLoader.create(photoView);
// imageLoader.setOnGlideImageViewListener(url, (percent, isDone, exception) -> {
// progressView.setProgress(percent);
// progressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
// });
// RequestOptions requestOptions = imageLoader.requestOptions(R.color.placeholder)
// .centerCrop()
// .skipMemoryCache(false)
// .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
// .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
// RequestBuilder<Drawable> requestBuilder = imageLoader.requestBuilder(url, requestOptions)
// .transition(DrawableTransitionOptions.withCrossFade());
// requestBuilder.into(new SimpleTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
// @Override
// public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
// if (resource.getIntrinsicHeight() > DisplayUtil.getScreenHeight(mContext)) {
// photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// }
// requestBuilder.into(photoView);
// }
// });

// container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return view;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.sunfusheng.glideimageview.GlideImageLoader;
import com.sunfusheng.glideimageview.GlideImageView;
import com.sunfusheng.glideimageview.progress.CircleProgressView;
import com.sunfusheng.glideimageview.sample.R;

import java.util.Random;

import static com.sunfusheng.glideimageview.sample.MainActivity.isLoadAgain;

/**
* Created by sunfusheng on 2017/6/15.
*/
Expand Down Expand Up @@ -59,7 +49,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}

private void initProgressView() {
isLoadAgain = new Random().nextInt(3) == 1;
int randomNum = new Random().nextInt(3);
switch (randomNum) {
case 1:
Expand All @@ -80,31 +69,6 @@ private void initProgressView() {
}

private void loadImage() {
glideImageView.setOnClickListener(v -> ActivityCompat.finishAfterTransition(SingleImageActivity.this));

RequestOptions requestOptions = glideImageView.requestOptions(R.color.black)
.centerCrop();
RequestOptions requestOptionsWithoutCache = glideImageView.requestOptions(R.color.black)
.centerCrop()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE);

GlideImageLoader imageLoader = glideImageView.getImageLoader();

imageLoader.setOnGlideImageViewListener(image_url, (percent, isDone, exception) -> {
if (exception != null && !TextUtils.isEmpty(exception.getMessage())) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
progressView.setProgress(percent);
progressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
maskView.setVisibility(isDone ? View.GONE : View.VISIBLE);
});

imageLoader.requestBuilder(image_url, requestOptionsWithoutCache)
.thumbnail(Glide.with(SingleImageActivity.this)
.load(image_url_thumbnail)
.apply(requestOptions))
.transition(DrawableTransitionOptions.withCrossFade())
.into(glideImageView);
}
}
Loading

0 comments on commit 3335ffd

Please sign in to comment.