Skip to content

Instantly share code, notes, and snippets.

@sujithkanna
Last active June 10, 2017 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujithkanna/48103b9f83d4648ba2a8de297043d14a to your computer and use it in GitHub Desktop.
Save sujithkanna/48103b9f83d4648ba2a8de297043d14a to your computer and use it in GitHub Desktop.
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