Last active
June 10, 2017 18:51
-
-
Save sujithkanna/48103b9f83d4648ba2a8de297043d14a to your computer and use it in GitHub Desktop.
This file contains 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
public class SliderImageView extends View { | |
private static final String TAG = "SliderImageView"; | |
private Bitmap mBitmapImage; | |
private float mAnimateFactor = 0f; | |
private Rect mSrcBounds = new Rect(); | |
private Rect mDrawBounds = new Rect(); | |
private Paint mBitmapPaint = new Paint(); | |
public SliderImageView(Context context) { | |
super(context); | |
} | |
public SliderImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public SliderImageView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
public void setImageBitmap(Bitmap bitmap) { | |
mBitmapImage = bitmap; | |
invalidate(); | |
} | |
public void setAnimationFactor(float factor) { | |
if (factor < 0 || factor > 1) { | |
return; | |
} | |
mAnimateFactor = 1 - factor; | |
invalidate(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
mSrcBounds.set(0, 0, getMeasuredWidth(), getMeasuredHeight()); | |
mDrawBounds.set(0, 0, mSrcBounds.right, mSrcBounds.bottom); | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (mBitmapImage != null) { | |
int range = mBitmapImage.getWidth() - mDrawBounds.width(); | |
mSrcBounds.left = Math.round(mAnimateFactor * range); | |
mSrcBounds.right = mDrawBounds.width() + mSrcBounds.left; | |
canvas.drawBitmap(mBitmapImage, mSrcBounds, mDrawBounds, mBitmapPaint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment