Skip to content

Instantly share code, notes, and snippets.

@miguelespinoza
Last active August 29, 2015 14:08
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 miguelespinoza/86baa23b9c36b8a00c04 to your computer and use it in GitHub Desktop.
Save miguelespinoza/86baa23b9c36b8a00c04 to your computer and use it in GitHub Desktop.
Trying to implement a Discrete Slider using material design specifications.
public class MainActivity extends Activity {
private RelativeLayout mTagContainer;
private RelativeLayout mDiscreteTag;
private ImageView mImageTag;
private TextView mLabelTag;
private SeekBar mSeekbar;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTagContainer = (RelativeLayout) findViewById(R.id.discrete_container);
mDiscreteTag = (RelativeLayout) findViewById(R.id.discrete_tag);
mImageTag = (ImageView) findViewById(R.id.image_tag);
mLabelTag = (TextView) findViewById(R.id.text_tag);
mSeekbar = (SeekBar) findViewById(R.id.seek_bar);
mContext = MainActivity.this;
mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
mLabelTag.setText(String.valueOf(progress));
// Move mDiscreteTag according to the thumb of the seekBar
mDiscreteTag.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2 - 25);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
mLabelTag.setText(String.valueOf(progress));
mDiscreteTag.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2 - 25);
//Animation scaleOut = AnimationUtils.loadAnimation(mContext, R.anim.scale_out);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleAnimation.setDuration(300);
// Time for mDiscreteTag to appear
mDiscreteTag.startAnimation(scaleAnimation);
mDiscreteTag.setVisibility(View.VISIBLE);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
mLabelTag.setText(String.valueOf(progress));
mDiscreteTag.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2 - 25);
Animation scaleIn = AnimationUtils.loadAnimation(mContext, R.anim.scale_in);
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0, 1f, 0, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleAnimation.setDuration(800);
// Time for mDiscreteTag to vanish
scaleAnimation.setAnimationListener(scaleInListener);
mDiscreteTag.startAnimation(scaleAnimation);
}
});
}// End onCreate
Animation.AnimationListener scaleInListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mDiscreteTag.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};// End ScaleInAnimListener
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment