Android Open Source - nxt-remote-controller Check Box






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  ww . j  a  v a  2  s .  c  o  m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.gc.materialdesign.R;
import com.gc.materialdesign.utils.Utils;

public class CheckBox extends CustomView {

  private Check checkView;

  private boolean idPressed = false;
  private boolean isChecked = false;

  private OnCheckListener onCheckListener;

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

  @Override
  protected void onInitDefaultValues() {
    minWidth = 48;
    minHeight = 48;
    backgroundColor = Color.parseColor("#4CAF50");// default color
    backgroundResId = R.drawable.background_checkbox;
  }
  
  @Override
  protected void setAttributes(AttributeSet attrs) {
    super.setAttributes(attrs);
    boolean isChecked = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "checked", false);
    if (isChecked) {
      post(new Runnable() {

        @Override
        public void run() {
          setChecked(true);
          setPressed(false);
          changeBackgroundColor(getResources().getColor(android.R.color.transparent));
        }
      });
    }

    float size = 20;
    String checkBoxSize = attrs.getAttributeValue(MATERIALDESIGNXML, "checkBoxSize");
    if (checkBoxSize != null) {
      size = Utils.dipOrDpToFloat(checkBoxSize);
    }
    checkView = new Check(getContext());
    setCheckBoxParams(size);
    addView(checkView);
  }

  private void setCheckBoxParams(float size) {
    RelativeLayout.LayoutParams params = new LayoutParams(
        Utils.dpToPx(size, getResources()), Utils.dpToPx(size, getResources()));
    params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    checkView.setLayoutParams(params);
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (isEnabled()) {
      isLastTouch = true;
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        changeBackgroundColor((isChecked) ? makePressColor(70) : Color.parseColor("#446D6D6D"));
      } else if (event.getAction() == MotionEvent.ACTION_UP) {
        changeBackgroundColor(getResources().getColor(android.R.color.transparent));
        idPressed = false;
        if ((event.getX() <= getWidth() && event.getX() >= 0)
            && (event.getY() <= getHeight() && event.getY() >= 0)) {
          isLastTouch = false;
          isChecked = !isChecked;
          if (onCheckListener != null)
            onCheckListener.onCheck(isChecked);
          if (isChecked) {
            step = 0;
          }
          if (isChecked) {
            checkView.changeBackground();
          }
        }
      }
    }
    return true;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (idPressed) {
      Paint paint = new Paint();
      paint.setAntiAlias(true);
      paint.setColor((isChecked) ? makePressColor(70) : Color.parseColor("#446D6D6D"));
      canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint);
    }
    invalidate();
  }
  
  // Indicate step in check animation
  private int step = 0;

  // View that contains checkbox
  private class Check extends View {

    private Bitmap sprite;

    public Check(Context context) {
      super(context);
      if (!isInEditMode()) {
        setBackgroundResource(R.drawable.background_checkbox_uncheck);
      }
      sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.sprite_check);
    }

    public void changeBackground() {
      if (isChecked) {
        setBackgroundResource(R.drawable.background_checkbox_check);
        LayerDrawable layer = (LayerDrawable) getBackground();
        GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
        shape.setColor(backgroundColor);
      } else {
        if (!isInEditMode()) {
          setBackgroundResource(R.drawable.background_checkbox_uncheck);
        }
      }
    }

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

      if (isChecked) {
        if (step < 11)
          step++;
      } else {
        if (step >= 0)
          step--;
        if (step == -1)
          changeBackground();
      }
      Rect src = new Rect(40 * step, 0, (40 * step) + 40, 40);
      Rect dst = new Rect(0, 0, this.getWidth() - 2, this.getHeight());
      if (!isInEditMode()) {
        canvas.drawBitmap(sprite, src, dst, null);
      }
      invalidate();

    }

  }

  private void changeBackgroundColor(int color) {
    if (!isInEditMode()) {
      LayerDrawable layer = (LayerDrawable) getBackground();
      GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
      shape.setColor(color);
    }
  }
  
  @Override
  public void setBackgroundColor(int color) {
    backgroundColor = color;
    if (isEnabled()) {
       beforeBackground = backgroundColor;
    }
    changeBackgroundColor(getResources().getColor(android.R.color.transparent));
//    changeBackgroundColor(color);
  }

  public void setChecked(boolean checked) {
    this.isChecked = checked;
    setPressed(false);
    changeBackgroundColor(getResources().getColor(android.R.color.transparent));
    if (checked) {
      step = 0;
    }
    if (checked) {
      checkView.changeBackground();
    }
  }

  public boolean isChecked() {
    return isChecked;
  }
  
  public void setCheckBoxSize(float size) {
    setCheckBoxParams(size);
  }

  public void setOncheckListener(OnCheckListener onCheckListener) {
    this.onCheckListener = onCheckListener;
  }

  public interface OnCheckListener {
    public void onCheck(boolean isChecked);
  }

}




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