Created
April 12, 2015 09:33
-
-
Save kyze8439690/e69453411408c00c76d2 to your computer and use it in GitHub Desktop.
ImageView that will draw a selector on top of image, support normal drawable and the newest RippleDrawable.
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.TargetApi; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import me.yugy.app.timeline.R; | |
public class SelectorImageView extends ImageView { | |
private Drawable mSelectorDrawable; | |
public SelectorImageView(Context context) { | |
this(context, null); | |
} | |
public SelectorImageView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public SelectorImageView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
mSelectorDrawable = context.getResources().getDrawable(R.drawable.item_selector, context.getTheme()); | |
} else { | |
//noinspection deprecation | |
mSelectorDrawable = context.getResources().getDrawable(R.drawable.item_selector); | |
} | |
if (mSelectorDrawable != null) { | |
mSelectorDrawable.setCallback(this); | |
} | |
} | |
@Override | |
protected void drawableStateChanged() { | |
super.drawableStateChanged(); | |
mSelectorDrawable.setState(getDrawableState()); | |
invalidate(); | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
@Override | |
public void jumpDrawablesToCurrentState() { | |
super.jumpDrawablesToCurrentState(); | |
mSelectorDrawable.jumpToCurrentState(); | |
} | |
@Override | |
protected boolean verifyDrawable(Drawable dr) { | |
return dr == mSelectorDrawable || super.verifyDrawable(dr); | |
} | |
@Override | |
public void invalidateDrawable(@NonNull Drawable dr) { | |
if (dr == mSelectorDrawable) { | |
invalidate(); | |
} else { | |
super.invalidateDrawable(dr); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public void drawableHotspotChanged(float x, float y) { | |
super.drawableHotspotChanged(x, y); | |
if (mSelectorDrawable != null) { | |
mSelectorDrawable.setHotspot(x, y); | |
} | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
mSelectorDrawable.setBounds(0, 0, w, h); | |
} | |
@Override | |
protected void onDraw(@NonNull Canvas canvas) { | |
super.onDraw(canvas); | |
mSelectorDrawable.draw(canvas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: change the
R.drawable.item_selector
the your own selector.