|
|
@@ -1,91 +0,0 @@
|
|
|
-package com.kfzs.duanduan.utils;
|
|
|
-
|
|
|
-import android.content.Context;
|
|
|
-import android.graphics.drawable.Drawable;
|
|
|
-import android.graphics.drawable.StateListDrawable;
|
|
|
-import android.os.AsyncTask;
|
|
|
-import android.support.annotation.DrawableRes;
|
|
|
-import android.view.View;
|
|
|
-import android.widget.ImageView;
|
|
|
-
|
|
|
-import com.bumptech.glide.Glide;
|
|
|
-import com.bumptech.glide.load.resource.drawable.GlideDrawable;
|
|
|
-import com.bumptech.glide.request.animation.GlideAnimation;
|
|
|
-import com.bumptech.glide.request.target.SimpleTarget;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.net.URL;
|
|
|
-
|
|
|
-/**
|
|
|
- * @ Created by Dlg
|
|
|
- * @ <p>TiTle: SelectorUtils</p>
|
|
|
- * @ <p>Description:动态设置 selector 的工具类</p>
|
|
|
- * @ <p>Description:可以从本地和网络添加</p>
|
|
|
- * @ date: 2017/12/14 14:57
|
|
|
- * @ QQ: 315096953
|
|
|
- */
|
|
|
-
|
|
|
-public class SelectorUtils {
|
|
|
-
|
|
|
- /**
|
|
|
- * 从 drawable 获取图片 id 给 Imageview 添加 selector
|
|
|
- *
|
|
|
- * @param context 调用方法的 Activity
|
|
|
- * @param idNormal 默认图片的 id
|
|
|
- * @param idPress 点击图片的 id
|
|
|
- * @param view 点击的 view,如果为imageView或imageButton会设置src
|
|
|
- * @param isBackground 是否为后台点击
|
|
|
- */
|
|
|
- public static void addSelectorFromDrawable(Context context, @DrawableRes int idNormal,
|
|
|
- @DrawableRes int idPress, View view, boolean isBackground) {
|
|
|
- StateListDrawable drawable = new StateListDrawable();
|
|
|
- Drawable normal = context.getResources().getDrawable(idNormal);
|
|
|
- Drawable press = context.getResources().getDrawable(idPress);
|
|
|
- drawable.addState(new int[]{android.R.attr.state_pressed}, press);
|
|
|
- drawable.addState(new int[]{-android.R.attr.state_pressed}, normal);
|
|
|
- if (isBackground) {
|
|
|
- view.setBackgroundDrawable(drawable);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (view instanceof ImageView) {
|
|
|
- ((ImageView) view).setImageDrawable(drawable);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 从网络获取图片 给 Button 设置 selector
|
|
|
- *
|
|
|
- * @param context 调用方法的类
|
|
|
- * @param normalUrl 获取默认图片的链接
|
|
|
- * @param pressUrl 获取点击图片的链接
|
|
|
- * @param imageView 点击的 imageView
|
|
|
- */
|
|
|
- public static void addSeletorFromNet(Context context, final String normalUrl,
|
|
|
- final String pressUrl, final ImageView imageView) {
|
|
|
- imageView.setImageDrawable(new StateListDrawable());
|
|
|
- Glide.with(context)
|
|
|
- .load(normalUrl)
|
|
|
- .into(new SimpleTarget<GlideDrawable>() {
|
|
|
- @Override
|
|
|
- public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
|
|
|
- StateListDrawable drawable = imageView.getDrawable() instanceof StateListDrawable ?
|
|
|
- (StateListDrawable) imageView.getDrawable() : new StateListDrawable();
|
|
|
- drawable.addState(new int[]{-android.R.attr.state_selected}, resource);
|
|
|
- imageView.setImageDrawable(drawable);
|
|
|
- }
|
|
|
- });
|
|
|
- Glide.with(context)
|
|
|
- .load(pressUrl)
|
|
|
- .into(new SimpleTarget<GlideDrawable>() {
|
|
|
- @Override
|
|
|
- public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
|
|
|
- StateListDrawable drawable = imageView.getDrawable() instanceof StateListDrawable ?
|
|
|
- (StateListDrawable) imageView.getDrawable() : new StateListDrawable();
|
|
|
- drawable.addState(new int[]{android.R.attr.state_pressed}, resource);
|
|
|
- drawable.addState(new int[]{android.R.attr.state_selected}, resource);
|
|
|
- imageView.setImageDrawable(drawable);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-}
|