Android Open Source - nxt-remote-controller Slider






From Project

Back to project page nxt-remote-controller.

License

The source code is released under:

MIT License

If you think the Android project nxt-remote-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gc.materialdesign.views;
// w w  w .  j  a  v  a  2s .  com
import com.gc.materialdesign.R;
import com.gc.materialdesign.utils.Utils;
import com.nineoldandroids.view.ViewHelper;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Slider extends CustomView {
  
  // Event when slider change value
  public interface OnValueChangedListener {
    public void onValueChanged(int value);
  }
  
  Ball ball;
  NumberIndicator numberIndicator;

  boolean showNumberIndicator = false;
  boolean press = false;

  int value = 0;
  int max = 100;
  int min = 0;

  private OnValueChangedListener onValueChangedListener;

  public Slider(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttributes(attrs);
  }

  @Override
  protected void onInitDefaultValues() {
    minWidth = 80;// size of view
    minHeight = 48;
    backgroundColor = Color.parseColor("#4CAF50");
    backgroundResId = R.drawable.background_transparent;
  }
  
  @Override
  protected void setAttributes(AttributeSet attrs) {
    super.setAttributes(attrs);
    showNumberIndicator = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,"showNumberIndicator", false);
    min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
    max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 100);// max > min
    value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);

    float size = 20;
    String thumbSize = attrs.getAttributeValue(MATERIALDESIGNXML, "thumbSize");
    if (thumbSize != null) {
      size = Utils.dipOrDpToFloat(thumbSize);
    }

    ball = new Ball(getContext());
    setBallParams(size);
    addView(ball);

    // Set if slider content number indicator
    if (showNumberIndicator) {
      if (!isInEditMode()) {
        numberIndicator = new NumberIndicator(getContext());
      }
    }
  }
  
  private void setBallParams(float size) {
    RelativeLayout.LayoutParams params = new LayoutParams(
        Utils.dpToPx(size, getResources()), Utils.dpToPx(size, getResources()));
    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    ball.setLayoutParams(params);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!placedBall) {
      placeBall();
    }
    if (value == min) {
      // Crop line to transparent effect
      Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
      Canvas temp = new Canvas(bitmap);
      Paint paint = new Paint();
      paint.setColor(Color.parseColor("#B0B0B0"));
      paint.setStrokeWidth(Utils.dpToPx(2, getResources()));
      temp.drawLine(getHeight() / 2, getHeight() / 2, getWidth() - getHeight() / 2, getHeight() / 2, paint);
      Paint transparentPaint = new Paint();
      transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
      transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
      temp.drawCircle(ViewHelper.getX(ball) + ball.getWidth() / 2,
          ViewHelper.getY(ball) + ball.getHeight() / 2, 
          ball.getWidth() / 2, transparentPaint);

      canvas.drawBitmap(bitmap, 0, 0, new Paint());
    } else {
      Paint paint = new Paint();
      paint.setColor(Color.parseColor("#B0B0B0"));
      paint.setStrokeWidth(Utils.dpToPx(2, getResources()));
      canvas.drawLine(getHeight() / 2, getHeight() / 2, getWidth() - getHeight() / 2, getHeight() / 2, paint);
      paint.setColor(backgroundColor);
      float division = (ball.xFin - ball.xIni) / (max - min);
      int value = this.value - min;
      canvas.drawLine(getHeight() / 2, getHeight() / 2, value * division + getHeight() / 2, getHeight() / 2, paint);
      // init ball's X
      ViewHelper.setX(ball, value * division + getHeight() / 2 - ball.getWidth() / 2);
      ball.changeBackground();
    }
    if (press && !showNumberIndicator) {
      /**
       * ??????????????????????ball????????????????
       * ???????????getHeight() / x???????????????????????
       * ??x=2???????????????????view??
       * ??x=3?????????????????????view??????
       */
      Paint paint = new Paint();
      paint.setColor(backgroundColor);
      paint.setAntiAlias(true);
      canvas.drawCircle(ViewHelper.getX(ball) + ball.getWidth() / 2, getHeight() / 2, getHeight() / 3, paint);
    }
    invalidate();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    isLastTouch = true;
    if (isEnabled()) {
      if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
        if (numberIndicator != null && numberIndicator.isShowing() == false)
          numberIndicator.show();
        if ((event.getX() <= getWidth() && event.getX() >= 0)) {
          press = true;
          // calculate value
          int newValue = 0;
          float division = (ball.xFin - ball.xIni) / (max - min);
          if (event.getX() > ball.xFin) {
            newValue = max;
          } else if (event.getX() < ball.xIni) {
            newValue = min;
          } else {
            newValue = min + (int) ((event.getX() - ball.xIni) / division);
          }
          if (value != newValue) {
            value = newValue;
            if (onValueChangedListener != null)
              onValueChangedListener.onValueChanged(newValue);
          }
          // move ball indicator
          float x = event.getX();
          x = (x < ball.xIni) ? ball.xIni : x;
          x = (x > ball.xFin) ? ball.xFin : x;
          ViewHelper.setX(ball, x);
          ball.changeBackground();

          // If slider has number indicator
          if (numberIndicator != null) {
            // move number indicator
            numberIndicator.indicator.x = x;
            numberIndicator.indicator.finalY = Utils.getRelativeTop(this) 
                - getHeight() / 2;
            numberIndicator.indicator.finalSize = getHeight() / 2;
            numberIndicator.numberIndicator.setText("");
          }

        } else {
          press = false;
          isLastTouch = false;
          if (numberIndicator != null)
            numberIndicator.dismiss();

        }

      } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (numberIndicator != null)
          numberIndicator.dismiss();
        isLastTouch = false;
        press = false;
        if ((event.getX() <= getWidth() && event.getX() >= 0)) {

        }
      }
    }
    return true;
  }

  private void placeBall() {
    ViewHelper.setX(ball, getHeight() / 2 - ball.getWidth() / 2);
    ball.xIni = ViewHelper.getX(ball);
    ball.xFin = getWidth() - getHeight() / 2 - ball.getWidth() / 2;
    ball.xCen = getWidth() / 2 - ball.getWidth() / 2;
    placedBall = true;
  }

  // GETERS & SETTERS

  public OnValueChangedListener getOnValueChangedListener() {
    return onValueChangedListener;
  }

  public void setOnValueChangedListener(
      OnValueChangedListener onValueChangedListener) {
    this.onValueChangedListener = onValueChangedListener;
  }
  
  public void setThumbSize(float size) {
    setBallParams(size);
  }

  public int getValue() {
    return value;
  }

  public void setValue(int value) {
    setValue(value, false);
  }
  /**
   * @param value
   * @param inRunnable ???true???runnable?????????????????
   */
  public void setValue(int value,boolean inRunnable) {
    if (value <= min) {
      value = min;
    }
    if (value >= max) {
      value = max;
    }
    setValueInRunnable(value,inRunnable);
  }
  
  
  private void setValueInRunnable(final int value,final boolean inRunnable) {
    if(placedBall == false && inRunnable == true)
      post(new Runnable() {
        @Override
        public void run() {
          setValue(value,inRunnable);
        }
      });
    else{
      this.value = value;
      float division = (ball.xFin - ball.xIni) / max;
      ViewHelper.setX(ball,value*division + getHeight()/2 - ball.getWidth()/2);
      ball.changeBackground();
    }
  }
  
  public int getMax() {
    return max;
  }

  public void setMax(int max) {
    this.max = max;
  }

  public int getMin() {
    return min;
  }

  public void setMin(int min) {
    this.min = min;
  }

  public boolean isShowNumberIndicator() {
    return showNumberIndicator;
  }

  public void showNumberIndicator(boolean showNumberIndicator) {
    this.showNumberIndicator = showNumberIndicator;
    if (!isInEditMode()) {
      numberIndicator = (showNumberIndicator) ? new NumberIndicator(getContext()) : null;
    }
  }
  
  @Override
  public void setBackgroundColor(int color) {
    backgroundColor = color;
    if (isEnabled()) {
      beforeBackground = backgroundColor;
    }
  }

  private boolean placedBall = false;

  private class Ball extends View {

    private float xIni, xFin, xCen;

    public Ball(Context context) {
      super(context);
      if (!isInEditMode()) {
        setBackgroundResource(R.drawable.background_switch_ball_uncheck);
      } else {
        setBackgroundResource(android.R.drawable.radiobutton_off_background);
      }
    }

    public void changeBackground() {
      if (!isInEditMode()) {
        if (value != min) {
          setBackgroundResource(R.drawable.background_checkbox);
          LayerDrawable layer = (LayerDrawable) getBackground();
          GradientDrawable shape = (GradientDrawable) layer
              .findDrawableByLayerId(R.id.shape_bacground);
          shape.setColor(backgroundColor);
        } else {
          setBackgroundResource(R.drawable.background_switch_ball_uncheck);
        }
      }
    }

  }

  // Slider Number Indicator

  private class NumberIndicator extends Dialog {

    private Indicator indicator;
    private TextView numberIndicator;

    public NumberIndicator(Context context) {
      super(context, android.R.style.Theme_Translucent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.number_indicator_spinner);
      setCanceledOnTouchOutside(false);

      RelativeLayout content = (RelativeLayout) this.findViewById(R.id.number_indicator_spinner_content);
      indicator = new Indicator(this.getContext());
      content.addView(indicator);

      numberIndicator = new TextView(getContext());
      numberIndicator.setTextColor(Color.WHITE);
      numberIndicator.setGravity(Gravity.CENTER);
      content.addView(numberIndicator);

      indicator.setLayoutParams(new RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.MATCH_PARENT,
          RelativeLayout.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void dismiss() {
      super.dismiss();
      indicator.y = 0;
      indicator.size = 0;
      indicator.animate = true;
    }

    @Override
    public void onBackPressed() {
    }

  }

  private class Indicator extends RelativeLayout {

    // Position of number indicator
    private float x = 0;
    private float y = 0;
    // Size of number indicator
    private float size = 0;

    // Final y position after animation
    private float finalY = 0;
    // Final size after animation
    private float finalSize = 0;

    private boolean animate = true;

    private boolean numberIndicatorResize = false;

    public Indicator(Context context) {
      super(context);
      setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

    @Override
    protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

      if (numberIndicatorResize == false) {
        RelativeLayout.LayoutParams params = (LayoutParams) numberIndicator.
            numberIndicator.getLayoutParams();
        params.height = (int) finalSize * 2;
        params.width = (int) finalSize * 2;
        numberIndicator.numberIndicator.setLayoutParams(params);
      }

      Paint paint = new Paint();
      paint.setAntiAlias(true);
      paint.setColor(backgroundColor);
      if (animate) {
        if (y == 0)
          y = finalY + finalSize * 2;
        y -= Utils.dpToPx(6, getResources());
        size += Utils.dpToPx(2, getResources());
      }
      canvas.drawCircle(
          ViewHelper.getX(ball) + Utils.getRelativeLeft((View) ball.getParent())
              + ball.getWidth() / 2, y, size, paint);
      if (animate && size >= finalSize)
        animate = false;
      if (animate == false) {
        ViewHelper.setX(numberIndicator.numberIndicator, 
            (ViewHelper.getX(ball) + Utils.getRelativeLeft((View) ball.getParent()) + ball.getWidth() / 2) - size);
        ViewHelper.setY(numberIndicator.numberIndicator, y - size);
        numberIndicator.numberIndicator.setText(value + "");
      }
      invalidate();
    }

  }

}




Java Source Code List

.OldPadControllerFragment.java
com.andexert.library.ApplicationTest.java
com.andexert.library.RippleView.java
com.andexert.rippleeffect.ApplicationTest.java
com.andexert.rippleeffect.CustomAdapter.java
com.andexert.rippleeffect.MainActivity.java
com.andexert.rippleeffect.OnTapListener.java
com.gc.materialdesign.utils.Utils.java
com.gc.materialdesign.views.ButtonFlat.java
com.gc.materialdesign.views.ButtonFloatSmall.java
com.gc.materialdesign.views.ButtonFloat.java
com.gc.materialdesign.views.ButtonIcon.java
com.gc.materialdesign.views.ButtonRectangle.java
com.gc.materialdesign.views.Button.java
com.gc.materialdesign.views.Card.java
com.gc.materialdesign.views.CheckBox.java
com.gc.materialdesign.views.CustomView.java
com.gc.materialdesign.views.LayoutRipple.java
com.gc.materialdesign.views.ProgressBarCircularIndeterminate.java
com.gc.materialdesign.views.ProgressBarDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminateDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminate.java
com.gc.materialdesign.views.RippleView.java
com.gc.materialdesign.views.ScrollView.java
com.gc.materialdesign.views.Slider.java
com.gc.materialdesign.views.Switch.java
com.gc.materialdesign.widgets.ColorSelector.java
com.gc.materialdesign.widgets.Dialog.java
com.gc.materialdesign.widgets.SnackBar.java
git.egatuts.nxtremotecontroller.ApplicationTest.java
git.egatuts.nxtremotecontroller.GlobalUtils.java
git.egatuts.nxtremotecontroller.activity.ActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.BaseActivity.java
git.egatuts.nxtremotecontroller.activity.ControllerActivity.java
git.egatuts.nxtremotecontroller.activity.DefaultActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.MainActivity.java
git.egatuts.nxtremotecontroller.activity.SettingsActivity.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothConstants.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothUtils.java
git.egatuts.nxtremotecontroller.bluetooth.NXTConnector.java
git.egatuts.nxtremotecontroller.device.PairedDeviceAdapter.java
git.egatuts.nxtremotecontroller.device.PairedDeviceItemClickListener.java
git.egatuts.nxtremotecontroller.device.PairedDeviceViewHolder.java
git.egatuts.nxtremotecontroller.device.PairedDevice.java
git.egatuts.nxtremotecontroller.exception.SocketCreationException.java
git.egatuts.nxtremotecontroller.fragment.ActivityBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BluetoothFragment.java
git.egatuts.nxtremotecontroller.fragment.ControllerBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.DefaultFragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.FragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.HomeFragment.java
git.egatuts.nxtremotecontroller.fragment.LocalControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.OnlineControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.ScanFragment.java
git.egatuts.nxtremotecontroller.fragment.SettingsFragment.java
git.egatuts.nxtremotecontroller.listener.AnimationEndListener.java
git.egatuts.nxtremotecontroller.listener.AppKillerListener.java
git.egatuts.nxtremotecontroller.listener.BaseListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothDiscoveryListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothEnableListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothPairingListener.java
git.egatuts.nxtremotecontroller.navigation.DrawerItemViewHolder.java
git.egatuts.nxtremotecontroller.navigation.DrawerItem.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerAdapter.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerCallback.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerFragment.java
git.egatuts.nxtremotecontroller.preference.PreferencesUtils.java
git.egatuts.nxtremotecontroller.receiver.AppKillerReceiver.java
git.egatuts.nxtremotecontroller.receiver.BaseReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothDiscoveryReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothEnableReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothPairingReceiver.java
git.egatuts.nxtremotecontroller.thread.BaseThread.java
git.egatuts.nxtremotecontroller.thread.ConnectThread.java
git.egatuts.nxtremotecontroller.thread.ConnectedThread.java
git.egatuts.nxtremotecontroller.views.BaseIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.BaseProgressDialog.java
git.egatuts.nxtremotecontroller.views.JoystickView.java
git.egatuts.nxtremotecontroller.views.LongIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.ShortIndeterminateProgressDialog.java