package com.uiui.zyos.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.widget.ImageView; import android.widget.TextView; import androidx.databinding.BindingAdapter; import com.bumptech.glide.Glide; import com.uiui.zyos.R; import com.uiui.zyos.utils.GlideLoadUtils; import com.uiui.zyos.utils.TimeUtils; public class ImageViewAdapter { @BindingAdapter("android:src") public static void setSrc(ImageView view, Bitmap bitmap) { view.setImageBitmap(bitmap); } @BindingAdapter("android:src") public static void setSrc(ImageView view, int resId) { view.setImageResource(resId); } @BindingAdapter("imageUrl") public static void setSrc(ImageView imageView, String url) { Glide.with(imageView.getContext()) .load(url) .error(R.mipmap.ic_launcher) .centerCrop() .into(imageView); } /** * 自定义设置图片属性 - 在匹配时自定义命名空间会被忽略 */ @BindingAdapter({"imageUrl", "error"}) public static void loadImage(ImageView imageView, String url, Drawable error) { Glide.with(imageView.getContext()) .load(url) .error(error) .into(imageView); } @BindingAdapter({"setTime"}) public static void setTime(TextView textView, long timestamp) { textView.setText(TimeUtils.transferSecondgToDate(timestamp)); } @BindingAdapter({"setTemp"}) public static void setTemp(TextView textView, String temp) { if (TextUtils.isEmpty(temp)) { textView.setText("N/A ℃"); } else { textView.setText(temp + " ℃"); } } // @BindingAdapter({"setTempIcon"}) // public static void setTempIcon(ImageView imageView, String code) { // String imageName = "he" + code; // Context context = imageView.getContext(); // int resId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName()); // GlideLoadUtils.getInstance().glideLoad(context, resId, imageView, R.drawable.he999); // } }