Created
February 2, 2017 19:07
-
-
Save mduisenov/535af3cf40588b0cee8b4ad9ae9185fe 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
package com.vk.dialogsample.widget; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.RelativeLayout; | |
import com.vk.dialogsample.R; | |
public class ForegroundRelativeLayout extends RelativeLayout { | |
private Drawable mForegroundSelector; | |
private boolean mUseBackgroundPadding = false; | |
public ForegroundRelativeLayout(Context paramContext) { | |
this(paramContext, null); | |
} | |
public ForegroundRelativeLayout(Context paramContext, AttributeSet paramAttributeSet) { | |
this(paramContext, paramAttributeSet, 0); | |
} | |
public ForegroundRelativeLayout(Context paramContext, AttributeSet paramAttributeSet, int paramInt) { | |
super(paramContext, paramAttributeSet, paramInt); | |
init(paramContext, paramAttributeSet, paramInt); | |
} | |
@TargetApi(21) | |
public ForegroundRelativeLayout(Context paramContext, AttributeSet paramAttributeSet, int paramInt1, int paramInt2) { | |
super(paramContext, paramAttributeSet, paramInt1, paramInt2); | |
init(paramContext, paramAttributeSet, paramInt2); | |
} | |
private void init(Context context, AttributeSet set, int i) { | |
if(set != null) { | |
TypedArray a = context.obtainStyledAttributes(set, R.styleable.FrameLayout, i, 0); | |
if(a != null) { | |
Drawable d = a.getDrawable(0); | |
mUseBackgroundPadding = a.getBoolean(R.styleable.FrameLayout_foregroundInsidePadding, false); | |
if(d != null) { | |
setForeground(d); | |
} | |
a.recycle(); | |
} | |
} | |
} | |
protected void dispatchDraw(Canvas paramCanvas) { | |
super.dispatchDraw(paramCanvas); | |
if (this.mForegroundSelector != null) { | |
this.mForegroundSelector.draw(paramCanvas); | |
} | |
} | |
@TargetApi(21) | |
public void drawableHotspotChanged(float paramFloat1, float paramFloat2) { | |
super.drawableHotspotChanged(paramFloat1, paramFloat2); | |
if (this.mForegroundSelector != null) { | |
this.mForegroundSelector.setHotspot(paramFloat1, paramFloat2); | |
} | |
} | |
protected void drawableStateChanged() { | |
super.drawableStateChanged(); | |
if ((this.mForegroundSelector != null) && (this.mForegroundSelector.isStateful())) { | |
this.mForegroundSelector.setState(getDrawableState()); | |
} | |
} | |
public void jumpDrawablesToCurrentState() { | |
super.jumpDrawablesToCurrentState(); | |
if (this.mForegroundSelector != null) { | |
this.mForegroundSelector.jumpToCurrentState(); | |
} | |
} | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
if(mForegroundSelector != null) { | |
if(mUseBackgroundPadding) { | |
mForegroundSelector.setBounds(getPaddingLeft(), getPaddingTop(), (w - getPaddingRight()), (h - getPaddingBottom())); | |
return; | |
} | |
mForegroundSelector.setBounds(0, 0, w, h); | |
} | |
} | |
public void setForeground(Drawable paramDrawable) { | |
if (mForegroundSelector != paramDrawable) { | |
if (mForegroundSelector != null) { | |
mForegroundSelector.setCallback(null); | |
unscheduleDrawable(mForegroundSelector); | |
} | |
mForegroundSelector = paramDrawable; | |
if (paramDrawable != null) { | |
setWillNotDraw(false); | |
paramDrawable.setCallback(this); | |
if (paramDrawable.isStateful()) { | |
paramDrawable.setState(getDrawableState()); | |
} | |
} else { | |
setWillNotDraw(true); | |
} | |
requestLayout(); | |
invalidate(); | |
} | |
} | |
protected boolean verifyDrawable(Drawable paramDrawable) { | |
return (super.verifyDrawable(paramDrawable)) || (paramDrawable == this.mForegroundSelector); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment