Created
October 10, 2017 21:10
-
-
Save briansalvattore/7e94280bff7dd618da534559a7e7f567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.content.ContextCompat; | |
import android.util.Log; | |
import android.view.View; | |
import java.util.List; | |
import codetail.graphics.drawables.DrawableHotspotTouch; | |
import codetail.graphics.drawables.LollipopDrawable; | |
import codetail.graphics.drawables.LollipopDrawablesCompat; | |
/** | |
* This is a helper for https://github.com/ozodrukh/RippleDrawable | |
* | |
* The first id is a ripple.xml and the second is a solid color, if ripple is not supported | |
*/ | |
/** | |
* @author Brian Salvattore | |
*/ | |
@SuppressWarnings("WeakerAccess") | |
public class DrawableUtil { | |
private static final String TAG = DrawableUtil.class.getSimpleName(); | |
@SuppressLint("StaticFieldLeak") | |
private static Context context; | |
public static void init(Context context) { | |
DrawableUtil.context = context; | |
} | |
public static void addRipple(int id, View... views){ | |
addRipple(new int[]{id}, views); | |
} | |
public static void addRipple(int[] ids, View... views) { | |
for(View view : views){ | |
setBackgroundDrawable(ids, view); | |
} | |
} | |
public static void addRipple(int id, List<? extends View> views){ | |
addRipple(new int[]{id}, views); | |
} | |
public static void addRipple(int[] ids, List<? extends View> views) { | |
for(View view : views){ | |
setBackgroundDrawable(ids, view); | |
} | |
} | |
@SuppressWarnings("deprecation") | |
private static void setBackgroundDrawable(int[] ids, View view) { | |
try { | |
view.setBackgroundDrawable(getDrawable(ids[0])); | |
view.setOnTouchListener(new DrawableHotspotTouch((LollipopDrawable) view.getBackground())); | |
} | |
catch (Exception e) { | |
Log.wtf(TAG, "setBackgroundDrawable: ", e); | |
if (ids.length == 1) return; | |
view.setBackgroundDrawable(ContextCompat.getDrawable(context, ids[1])); | |
} | |
view.setClickable(true); | |
} | |
private static Drawable getDrawable(int id) throws Exception { | |
return LollipopDrawablesCompat.getDrawable(context.getResources(), id, context.getTheme()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DrawableUtil.init(this);
DrawableUtil.addRipple(R.drawable.ripple_primary, the_button);