import android.content.res.Resources;
import android.databinding.BindingAdapter;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.util.TypedValue;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.File;
/**
* Created by wanghailong on 2016/9/27/0027.
*/
public class FrescoUtil {
public static RoundingParams circleParams = null;
public static RoundingParams roundingParams = null;
public static SimpleDraweeView imageView = null;
public static String getHttpUrl(String imageUrl) {
return imageUrl;
}
/**
* Fresco 返回圆形图片的配置参数
* 使用方法 SimpleDraweeView.getHierarchy().setRoundingParams(FrescoUtil.getRoundParams)
*
* @return
*/
public static RoundingParams getCircleParams() {
if (null == circleParams) {
synchronized (FrescoUtil.class) {
if (null == circleParams) {
circleParams = new RoundingParams();
circleParams.setRoundAsCircle(true);
}
}
}
return circleParams;
}
public static RoundingParams getRoundingParams(int round) {
roundingParams = new RoundingParams();
roundingParams.setCornersRadius(round);
return roundingParams;
}
/**
* Fresco加载圆形图片
*
* @param imageView
* @param imageUrl // * @param defaultImageID
*/
public static void loadCircleImage(SimpleDraweeView imageView, String imageUrl, int defaultDrawableId) {
imageUrl = getHttpUrl(imageUrl);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getCircleParams());
imageView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
PointF point = new PointF();
point.set(0.5f, 0.0f);
imageView.getHierarchy().setActualImageFocusPoint(point);
imageView.setImageURI(Uri.parse(imageUrl));
} else {
loadResourceCircleImage(imageView, defaultDrawableId, 0);
}
}
/**
* Fresco加载圆形图片
*
* @param imageView
* @param imageUrl
* @param defaultImage
*/
@BindingAdapter({"circleUrl", "defaultDrawable"})
public static void loadCircleImage(SimpleDraweeView imageView, String imageUrl, Drawable defaultImage) {
imageUrl = getHttpUrl(imageUrl);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getCircleParams());
imageView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
PointF point = new PointF();
point.set(0.5f, 0.0f);
imageView.getHierarchy().setActualImageFocusPoint(point);
imageView.setImageURI(Uri.parse(imageUrl));
} else {
// loadResourceCircleImage(imageView, defaultImageID, 0);
imageView.setImageDrawable(defaultImage);
}
}
/**
* 加载网络图片圆角
*
* @param imageView
* @param imageUrl
* @param round
* @param defaultImage
*/
@BindingAdapter({"roundUrl", "round", "defaultDrawable"})
public static void loadRoundImage(SimpleDraweeView imageView, String imageUrl, int round, Drawable defaultImage) {
imageUrl = getHttpUrl(imageUrl);
int pix = dip2px(round);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix));
imageView.setImageURI(Uri.parse(imageUrl));
} else {
imageView.setImageDrawable(defaultImage);
}
}
/**
* 加载网络图片圆角
*
* @param imageView
* @param imageUrl
* @param round
* @param imageId
*/
public static void loadRoundImage(SimpleDraweeView imageView, String imageUrl, int round, int imageId) {
imageUrl = getHttpUrl(imageUrl);
int pix = dip2px(round);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix));
imageView.setImageURI(Uri.parse(imageUrl));
} else {
imageView.setBackgroundResource(imageId);
}
}
/**
* 加载网络图片,图片的顶部为圆角
*
* @param imageView
* @param imageUrl
* @param round
* @param defaultImageID
*/
public static void loadTopRoundImage(SimpleDraweeView imageView, String imageUrl, int round, int defaultImageID) {
imageUrl = getHttpUrl(imageUrl);
int pix = dip2px(round);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix).setCornersRadii(pix, pix, 0, 0));
imageView.setImageURI(Uri.parse(imageUrl));
} else {
imageView.setBackgroundResource(defaultImageID);
}
}
/**
* 加载网络图片,图片的顶部为圆角
*
* @param imageView
* @param imageUrl
* @param round
* @param defaultDrawable
*/
@BindingAdapter({"topRoundUrl", "round", "defaultDrawable"})
public static void loadTopRoundImage(SimpleDraweeView imageView, String imageUrl, int round, Drawable defaultDrawable) {
imageUrl = getHttpUrl(imageUrl);
int pix = dip2px(round);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix).setCornersRadii(pix, pix, 0, 0));
imageView.setImageURI(Uri.parse(imageUrl));
} else {
imageView.setImageDrawable(defaultDrawable);
}
}
/**
* 加载矩形图片
*
* @param imageView
* @param imageUrl
* @param defaultImageID
*/
public static void loadImage(SimpleDraweeView imageView, String imageUrl, int defaultImageID) {
imageUrl = getHttpUrl(imageUrl);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.setImageURI(Uri.parse(imageUrl));
} else {
if (0 == defaultImageID) {
} else {
imageView.setImageResource(defaultImageID);
}
}
}
@BindingAdapter({"url", "defaultDrawable"})
public static void loadImage(SimpleDraweeView imageView, String imageUrl, Drawable defaultDrawable) {
imageUrl = getHttpUrl(imageUrl);
if (!TextUtils.isEmpty(imageUrl)) {
imageView.setImageURI(Uri.parse(imageUrl));
} else {
if (null == defaultDrawable) {
} else {
imageView.setImageDrawable(defaultDrawable);
}
}
}
/**
* fresco 加载本地图片
* 已测试
*/
/**
* 加载资源图片,顶部为圆角
*
* @param imageView
* @param round
*/
@BindingAdapter(value = {"topRoundDrawable", "round", "defaultDrawable"}, requireAll = false)
public static void loadTopRoundimage(SimpleDraweeView imageView, Drawable topRoundDrawable, int round, Drawable defaultDrawable) {
int pix = dip2px(round);
if (null != topRoundDrawable) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix).setCornersRadii(pix, pix, 0, 0));
// imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(topRoundDrawable)).build());
imageView.setImageDrawable(topRoundDrawable);
} else if (null != defaultDrawable) {
imageView.setImageDrawable(defaultDrawable);
}
}
/**
* 加载资源文件
*
* @param imageView
* @param resourceDrawable
* @param defaultImageResId
*/
public static void loadResourceDrawable(SimpleDraweeView imageView, int resourceDrawable, int defaultImageResId) {
if (0 != resourceDrawable) {
imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(resourceDrawable)).build());
} else if (0 != defaultImageResId) {
imageView.setBackgroundResource(defaultImageResId);
}
}
@BindingAdapter({"resourceDrawable", "defaultDrawable"})
public static void loadResourceDrawable(SimpleDraweeView imageView, Drawable resourceDrawable, Drawable defaultDrawable) {
if (null != resourceDrawable) {
// imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(resourceDrawable)).build());
imageView.setImageDrawable(resourceDrawable);
} else if (null != defaultDrawable) {
imageView.setImageDrawable(defaultDrawable);
}
}
/**
* fresco加载本地图片处理为圆形
*
* @param imageView
* @param targetImageResId
* @param defaultImageResId
*/
public static void loadResourceCircleImage(SimpleDraweeView imageView, int targetImageResId, int defaultImageResId) {
if (0 != targetImageResId) {
imageView.getHierarchy().setRoundingParams(getCircleParams());
imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(targetImageResId)).build());
} else {
if (defaultImageResId != 0) {
loadResourceCircleImage(imageView, defaultImageResId, 0);
}
}
}
@BindingAdapter({"circlRresourceDrawable", "defaultDrawable"})
public static void loadCirclRresourceDrawable(SimpleDraweeView imageView, Drawable circlRresourceDrawable, Drawable defaultDrawable) {
if (null != circlRresourceDrawable) {
imageView.getHierarchy().setRoundingParams(getCircleParams());
// imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(circlRresourceDrawable)).build());
imageView.setImageDrawable(circlRresourceDrawable);
} else {
if (defaultDrawable != null) {
imageView.setImageDrawable(defaultDrawable);
}
}
}
/**
* fresco加载本地图片需要输入图片的角度 int
*
* @param imageView
* @param targetImageResId
* @param defaultImageResId
* @param round
*/
public static void loadLocalRoundImage(SimpleDraweeView imageView, int targetImageResId, int defaultImageResId, int round) {
if (0 != targetImageResId) {
int pix = dip2px(round);
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix));
imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(targetImageResId)).build());
} else if (0 != defaultImageResId) {
imageView.setBackgroundResource(defaultImageResId);
}
}
@BindingAdapter({"roundResourceDrawable", "round", "defaultDrawable"})
public static void loadRoundResourceDrawable(SimpleDraweeView imageView, Drawable roundResourceDrawable, int round, Drawable defaultDrawable) {
if (null != roundResourceDrawable) {
int pix = dip2px(round);
imageView.getHierarchy().setRoundingParams(getRoundingParams(pix));
//imageView.setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(roundResourceDrawable)).build());
imageView.setImageDrawable(roundResourceDrawable);
} else if (null != defaultDrawable) {
imageView.setImageDrawable(defaultDrawable);
}
}
/**
* fresco加载手机设备上的图片
*
* @param imageView
* @param filePath
* @param defaultImageResId
*/
public static void loadFileImage(SimpleDraweeView imageView, String filePath, int defaultImageResId) {
if (!TextUtils.isEmpty(filePath)) {
if (new File(filePath).exists()) {
Uri uri = Uri.parse("file://" + filePath);
imageView.setImageURI(uri);
} else {
imageView.setBackgroundResource(defaultImageResId);
}
} else if (0 != defaultImageResId) {
imageView.setBackgroundResource(defaultImageResId);
}
}
/**
* fresco加载设备上的文件,显示为圆形
*
* @param imageView
* @param filePath
* @param defaultImageResId
*/
public static void LoadFileCircleImage(SimpleDraweeView imageView, String filePath, int defaultImageResId) {
if (!TextUtils.isEmpty(filePath)) {
if (new File(filePath).exists()) {
imageView.getHierarchy().setRoundingParams(getCircleParams());
Uri uri = Uri.parse("file://" + filePath);
imageView.setImageURI(uri);
} else {
imageView.setBackgroundResource(defaultImageResId);
}
} else if (0 != defaultImageResId) {
imageView.setBackgroundResource(defaultImageResId);
}
}
/**
* fresco加载设备图片显示为圆形,传入角度
*
* @param imageView
* @param filePath
* @param defaultImageResId
* @param round
*/
public static void LoadFileRoundImage(SimpleDraweeView imageView, String filePath, int defaultImageResId, int round) {
if (!TextUtils.isEmpty(filePath)) {
if (new File(filePath).exists()) {
imageView.getHierarchy().setRoundingParams(getRoundingParams(round));
Uri uri = Uri.parse("file://" + filePath);
imageView.setImageURI(uri);
} else {
imageView.setBackgroundResource(defaultImageResId);
}
} else if (0 != defaultImageResId) {
imageView.setBackgroundResource(defaultImageResId);
}
}
public static int dip2px(int dip) {
Resources r = Resources.getSystem();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
}
}
在xml文件中的用法
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="debug.been.ImageBeen" />
<import type="java.util.List" />
<variable
name="image"
type="debug.been.ImageBeen" />
<variable
name="list"
type="java.util.List<debug.been.ImageBeen>" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:gravity="center"
android:orientation="vertical">
<base.imageloader.view.MView
android:id="@+id/image5"
android:layout_width="50dp"
android:layout_height="50dp"
app:circleUrl="http://noavatar.youkuaiyun.com/C/9/2/1_wangbofei.jpg"
app:defaultDrawable="@drawable/open_qilu" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@drawable/open_qilu"
app:round="10"
app:roundUrl="http://noavatar.youkuaiyun.com/C/9/2/1_wangbofei.jpg" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@drawable/open_qilu"
app:round="10"
app:topRoundUrl="http://noavatar.youkuaiyun.com/C/9/2/1_wangbofei.jpg" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@drawable/open_qilu"
app:url="http://noavatar.youkuaiyun.com/C/9/2/1_wangbofei.jpg" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@{@drawable/open_qilu}"
app:url="@{list[0].url}" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:circleUrl="@{list[0].url}"
app:defaultDrawable="@{@drawable/open_qilu}" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:circleUrl="@{list[1].url}"
app:defaultDrawable="@{@drawable/open_qilu}" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@{@drawable/ic_launcher}"
app:resourceDrawable="@{@drawable/open_qilu}" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:circlRresourceDrawable="@drawable/open_qilu"
app:defaultDrawable="@drawable/ic_launcher" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@drawable/ic_launcher"
app:round="10"
app:topRoundDrawable="@drawable/open_qilu" />
<base.imageloader.view.MView
android:layout_width="50dp"
android:layout_height="50dp"
app:defaultDrawable="@drawable/open_qilu"
app:round="10"
app:roundResourceDrawable="@drawable/open_qilu" />
<base.imageloader.view.MView
android:id="@+id/image6"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
</ScrollView>
</layout>
MView自定义控件
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
/**
* Created by Administrator on 2017/3/23/0023.
*/
public class MView extends SimpleDraweeView {
private static RoundingParams circleParams = null;
private static RoundingParams roundingParams = null;
private String url = "";
private String topRoundUrl = "";
private String roundUrl = "";
private String circleUrl = "";
private int round = 0;
private int defaultDrawable = 0;
private int topRoundDrawable = 0;
private int resourceDrawable = 0;
private int circlRresourceDrawable = 0;
private int roundResourceDrawable = 0;
public MView(Context context, GenericDraweeHierarchy hierarchy) {
super(context, hierarchy);
init(context, null);
}
public MView(Context context) {
super(context);
init(context, null);
}
public MView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public MView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
/**
* pvwx
*
* @param context
* @param attrs
*/
private void init(Context context, @Nullable AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MView);
circleUrl = a.getString(R.styleable.MView_circleUrl);
roundUrl = a.getString(R.styleable.MView_roundUrl);
url = a.getString(R.styleable.MView_url);
topRoundUrl = a.getString(R.styleable.MView_topRoundUrl);
round = (int) a.getInt(R.styleable.MView_round, round);
defaultDrawable = (int) a.getResourceId(R.styleable.MView_defaultDrawable, defaultDrawable);
topRoundDrawable = (int) a.getResourceId(R.styleable.MView_topRoundDrawable, topRoundDrawable);
roundResourceDrawable = (int) a.getResourceId(R.styleable.MView_roundResourceDrawable, roundResourceDrawable);
circlRresourceDrawable = (int) a.getResourceId(R.styleable.MView_circlRresourceDrawable, circlRresourceDrawable);
resourceDrawable = (int) a.getResourceId(R.styleable.MView_roundResourceDrawable, resourceDrawable);
a.recycle();
if (!TextUtils.isEmpty(url)) { setImage(url, defaultDrawable); } else if (!TextUtils.isEmpty(circleUrl)) { setCircleImage(circleUrl, defaultDrawable); } else if (!TextUtils.isEmpty(roundUrl)) { setRoundImage(roundUrl, round, defaultDrawable); } else if (!TextUtils.isEmpty(topRoundUrl)) { seteTopRoundImage(topRoundUrl, round, defaultDrawable); } else if (0 != topRoundDrawable) { //TODO 调用上圆角方法 setTopRoundDrawable(topRoundDrawable, round, defaultDrawable); } else if (0 != roundResourceDrawable) { //TODO 调用圆角方法 setRoundResourceDrawable(roundResourceDrawable, defaultDrawable, round); } else if (0 != circlRresourceDrawable) { setCirclRresourceDrawable(circlRresourceDrawable, defaultDrawable); //TODO 调用圆形方法 } else if (0 != resourceDrawable) { //TODO 调用加载本地图片方法 setResourceDrawable(resourceDrawable, defaultDrawable); } } /** * 加载圆形图片对应circleImage * * @param * @param imageUrl * @param defaultDrawableId */ public void setCircleImage(String imageUrl, int defaultDrawableId) { if (!TextUtils.isEmpty(imageUrl)) { getHierarchy().setRoundingParams(getCircleParams()); getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP); PointF point = new PointF(); point.set(0.5f, 0.0f); getHierarchy().setActualImageFocusPoint(point); setImageURI(Uri.parse(imageUrl)); } else { setImageResource(defaultDrawableId); } } public void setRoundImage(String imageUrl, int round, int defaultImage) { int pix = dip2px(round); if (!TextUtils.isEmpty(imageUrl)) { getHierarchy().setRoundingParams(getRoundingParams(pix)); setImageURI(Uri.parse(imageUrl)); } else { setImageResource(defaultImage); } } public void seteTopRoundImage(String imageUrl, int round, int defaultImageID) { int pix = dip2px(round); if (!TextUtils.isEmpty(imageUrl)) { getHierarchy().setRoundingParams(getRoundingParams(pix).setCornersRadii(pix, pix, 0, 0)); setImageURI(Uri.parse(imageUrl)); } else { setImageResource(defaultImageID); } } public void setImage(String imageUrl, int defaultImageID) { if (!TextUtils.isEmpty(imageUrl)) { setImageURI(Uri.parse(imageUrl)); } else { if (0 == defaultImageID) { } else { setImageResource(defaultImageID); } } } public void setRoundResourceDrawable(int roundResourceDrawable, int defaultImageResId, int round) { if (0 != roundResourceDrawable) { int pix = dip2px(round); getHierarchy().setRoundingParams(getRoundingParams(pix)); setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(roundResourceDrawable)).build()); } else if (0 != defaultImageResId) { setImageResource(defaultImageResId); } } /** * 加载资源文件 * * @param targetImageResId * @param defaultImageResId */ public void setResourceDrawable(int targetImageResId, int defaultImageResId) { if (0 != targetImageResId) { setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(targetImageResId)).build()); } else if (0 != defaultImageResId) { setImageResource(defaultImageResId); } } /** * fresco加载本地图片处理为圆形 * * @param circlRresourceDrawable * @param defaultImageResId */ public void setCirclRresourceDrawable(int circlRresourceDrawable, int defaultImageResId) { if (0 != circlRresourceDrawable) { getHierarchy().setRoundingParams(getCircleParams()); setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(circlRresourceDrawable)).build()); } else { if (defaultImageResId != 0) { setCirclRresourceDrawable(defaultImageResId, 0); } } } public void setTopRoundDrawable(int topRoundDrawable, int round, int defaultImageResId) { int pix = dip2px(round); if (0 != topRoundDrawable) { getHierarchy().setRoundingParams(getRoundingParams(pix).setCornersRadii(pix, pix, 0, 0)); setImageURI(new Uri.Builder().scheme("res").path(String.valueOf(topRoundDrawable)).build()); } else if (0 != defaultImageResId) { setImageResource(defaultImageResId); } } /** * Displays an image given by the uri. * * @param uri uri of the image * @undeprecate */// @Override// public void setImageURI(Uri uri) {// setImageURI(uri, null);// } /** * 圆形的配置 * * @return */ public static RoundingParams getCircleParams() { if (null == circleParams) { synchronized (MView.class) { if (null == circleParams) { circleParams = new RoundingParams(); circleParams.setRoundAsCircle(true); } } } return circleParams; } /** * 圆角的配置 * * @param round * @return */ public static RoundingParams getRoundingParams(int round) { roundingParams = new RoundingParams(); roundingParams.setCornersRadius(round); return roundingParams; } public static int dip2px(int dip) { Resources r = Resources.getSystem(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics()); }}
MView attr文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- MView-->
<declare-styleable name="MView">
<attr name="url" format="string" />
<attr name="round" format="integer" />
<attr name="defaultDrawable" format="reference" />
<attr name="resourceDrawable" format="reference" />
<attr name="circlRresourceDrawable" format="reference" />
<attr name="roundResourceDrawable" format="reference" />
<attr name="topRoundDrawable" format="reference" />
<attr name="topRoundUrl" format="string" />
<attr name="roundUrl" format="string" />
<attr name="circleUrl" format="string" />
</declare-styleable>
</resources>