1.直接在ImageView上传入图片地址是不行的
2.用自已定义属性的方法再用glide的方法来传
3.正确的使用
1.直接在ImageView上传入图片地址是不行的
<!--android:src="@{user.avatar}" 不能直接上图片地址-->
<ImageView
android:id="@+id/avatar"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:src="@{user.avatar}"
tools:src="@mipmap/ic_launcher_round" />
2.用自已定义属性的方法再用glide的方法来传
PPImageView.java
public class PPImageView extends AppCompatImageView {
public PPImageView(Context context) {
super(context);
}
public PPImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PPImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// ViewHelper.setViewOutline(this, attrs, defStyleAttr, 0);
}
public void setImageUrl(String imageUrl) {
setImageUrl(this, imageUrl, false);
}
@BindingAdapter(value = {"image_url", "isCircle"})
public static void setImageUrl(PPImageView view, String imageUrl, boolean isCircle) {
view.setImageUrl(view, imageUrl, isCircle, 0);
}
@BindingAdapter(value = {"image_url", "isCircle", "radius"}, requireAll = false)
public static void setImageUrl(PPImageView view, String imageUrl, boolean isCircle, int radius) {
RequestBuilder<Drawable> builder = Glide.with(view).load(imageUrl);
if (isCircle) {
builder.transform(new CircleCrop());
} else if (radius > 0) {
builder.transform(new RoundedCornersTransformation(PixUtils.dp2px(radius), 0));
}
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (layoutParams != null && layoutParams.width > 0 && layoutParams.height > 0) {
builder.override(layoutParams.width, layoutParams.height);
}
builder.into(view);
}
public void bindData(int widthPx, int heightPx, int marginLeft, String imageUrl) {
bindData(widthPx, heightPx, marginLeft, PixUtils.getScreenWidth(), PixUtils.getScreenWidth(), imageUrl);
}
public void bindData(int widthPx, int heightPx, final int marginLeft, final int maxWidth, final int maxHeight, String imageUrl) {
if (TextUtils.isEmpty(imageUrl)) {
setVisibility(GONE);
return;
} else {
setVisibility(VISIBLE);
}
if (widthPx <= 0 || heightPx <= 0) {
Glide.with(this).load(imageUrl).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
int height = resource.getIntrinsicHeight();
int width = resource.getIntrinsicWidth();
setSize(width, height, marginLeft, maxWidth, maxHeight);
setImageDrawable(resource);
}
});
return;
}
setSize(widthPx, heightPx, marginLeft, maxWidth, maxHeight);
setImageUrl(this, imageUrl, false);
}
private void setSize(int width, int height, int marginLeft, int maxWidth, int maxHeight) {
int finalWidth, finalHeight;
if (width > height) {
finalWidth = maxWidth;
finalHeight = (int) (height / (width * 1.0f / finalWidth));
} else {
finalHeight = maxHeight;
finalWidth = (int) (width / (height * 1.0f / finalHeight));
}
ViewGroup.LayoutParams params = getLayoutParams();
params.width = finalWidth;
params.height = finalHeight;
if (params instanceof FrameLayout.LayoutParams) {
((FrameLayout.LayoutParams) params).leftMargin = height > width ? PixUtils.dp2px(marginLeft) : 0;
} else if (params instanceof LinearLayout.LayoutParams) {
((LinearLayout.LayoutParams) params).leftMargin = height > width ? PixUtils.dp2px(marginLeft) : 0;
}
setLayoutParams(params);
}
@BindingAdapter(value = {"blur_url", "radius"})
public static void setBlurImageUrl(final ImageView imageView, String blurUrl, int radius) {
Glide.with(imageView).load(blurUrl).override(radius)
.transform(new BlurTransformation())
.dontAnimate()
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
imageView.setBackground(resource);
}
});
}
}
PixUtils.java
public class PixUtils {
public static int dp2px(int dpValue) {
DisplayMetrics metrics = AppGlobals.getApplication().getResources().getDisplayMetrics();
return (int) (metrics.density * dpValue + 0.5f);
}
public static int getScreenWidth() {
DisplayMetrics metrics = AppGlobals.getApplication().getResources().getDisplayMetrics();
return metrics.widthPixels;
}
public static int getScreenHeight() {
DisplayMetrics metrics = AppGlobals.getApplication().getResources().getDisplayMetrics();
return metrics.heightPixels;
}
}
3.正确的使用
user.avatar为图片的网络地址
<!--android:src="@{user.avatar}" 不能直接上图片地址-->
<com.mooc.ppjoke.view.PPImageView
android:id="@+id/avatar"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
app:image_url="@{user.avatar}"
app:isCircle="@{true}"
tools:src="@mipmap/ic_launcher_round" />
网友评论