- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- import com.pubukeji.gamecenter.R;
- public class MySlipSwitch extends View
- implements View.OnTouchListener
- {
- private long clickTimeMillis = 0L;
- private float currentX;
- private boolean isSlipping = false;
- private boolean isSwitchOn = false;
- private Rect off_Rect;
- private OnSwitchListener onSwitchListener;
- private Rect on_Rect;
- private float previousX;
- private Bitmap slip_Btn;
- private Bitmap switch_off_Bkg;
- private Bitmap switch_on_Bkg;
- public MySlipSwitch(Context paramContext)
- {
- super(paramContext);
- init();
- }
- public MySlipSwitch(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public MySlipSwitch(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
- private void init()
- {
- setImageResource(R.drawable.split_left_1, R.drawable.split_right_1,
- R.drawable.split_1);
- setOnTouchListener(this);
- }
- protected boolean getSwitchState()
- {
- return this.isSwitchOn;
- }
- public void onClick()
- {
- boolean bool;
- if (!this.isSwitchOn)
- bool = true;
- else
- bool = false;
- this.isSwitchOn = bool;
- if (this.onSwitchListener != null)
- this.onSwitchListener.onSwitched(this, this.isSwitchOn);
- invalidate();
- }
- protected void onDraw(Canvas paramCanvas)
- {
- super.onDraw(paramCanvas);
- Matrix localMatrix = new Matrix();
- Paint localPaint = new Paint();
- if (this.currentX >= this.switch_on_Bkg.getWidth() / 2)
- paramCanvas.drawBitmap(this.switch_on_Bkg, localMatrix, localPaint);
- else
- paramCanvas.drawBitmap(this.switch_off_Bkg, localMatrix, localPaint);
- float f;
- if (!this.isSlipping)
- {
- if (!this.isSwitchOn)
- {
- f = this.off_Rect.left;
- paramCanvas.drawBitmap(this.switch_off_Bkg, localMatrix, localPaint);
- }
- else
- {
- f = this.on_Rect.left;
- paramCanvas.drawBitmap(this.switch_on_Bkg, localMatrix, localPaint);
- }
- }
- else if (this.currentX <= this.switch_on_Bkg.getWidth())
- f = this.currentX - this.slip_Btn.getWidth() / 2;
- else
- f = this.switch_on_Bkg.getWidth() - this.slip_Btn.getWidth();
- if (f >= 0.0F)
- {
- if (f > this.switch_on_Bkg.getWidth() - this.slip_Btn.getWidth())
- f = this.switch_on_Bkg.getWidth() - this.slip_Btn.getWidth();
- }
- else
- f = 0.0F;
- paramCanvas.drawBitmap(this.slip_Btn, f, 0.0F, localPaint);
- }
- protected void onMeasure(int paramInt1, int paramInt2)
- {
- setMeasuredDimension(this.switch_on_Bkg.getWidth(), this.switch_on_Bkg.getHeight());
- }
- public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
- {
- boolean bool1 = false;
- switch (paramMotionEvent.getAction())
- {
- case 0:
- if ((paramMotionEvent.getX() > this.switch_on_Bkg.getWidth()) || (paramMotionEvent.getY() > this.switch_on_Bkg.getHeight()))
- break;
- this.isSlipping = true;
- this.clickTimeMillis = System.currentTimeMillis();
- this.previousX = paramMotionEvent.getX();
- this.currentX = this.previousX;
- break;
- case 1:
- case 3:
- this.isSlipping = bool1;
- if (System.currentTimeMillis() - this.clickTimeMillis >= 300L)
- {
- boolean bool2 = this.isSwitchOn;
- if (paramMotionEvent.getX() < this.switch_on_Bkg.getWidth() / 2)
- this.isSwitchOn = bool1;
- else
- this.isSwitchOn = true;
- if ((this.onSwitchListener == null) || (bool2 == this.isSwitchOn))
- break;
- this.onSwitchListener.onSwitched(this, this.isSwitchOn);
- }
- else
- {
- onClick();
- }
- break;
- case 2:
- this.currentX = paramMotionEvent.getX();
- }
- invalidate();
- bool1 = true;
- return bool1;
- }
- protected void setImageResource(int paramInt1, int paramInt2, int paramInt3)
- {
- this.switch_on_Bkg = BitmapFactory.decodeResource(getResources(), paramInt1);
- this.switch_off_Bkg = BitmapFactory.decodeResource(getResources(), paramInt2);
- this.slip_Btn = BitmapFactory.decodeResource(getResources(), paramInt3);
- this.on_Rect = new Rect(this.switch_off_Bkg.getWidth() - this.slip_Btn.getWidth(), 0, this.switch_off_Bkg.getWidth(), this.slip_Btn.getHeight());
- this.off_Rect = new Rect(0, 0, this.slip_Btn.getWidth(), this.slip_Btn.getHeight());
- }
- public void setOnSwitchListener(OnSwitchListener paramOnSwitchListener)
- {
- this.onSwitchListener = paramOnSwitchListener;
- }
- public void setSwitchState(boolean paramBoolean)
- {
- this.isSwitchOn = paramBoolean;
- invalidate();
- }
- public static abstract interface OnSwitchListener
- {
- public abstract void onSwitched(MySlipSwitch paramMySlipSwitch, boolean paramBoolean);
- }
- }