Skip to content

Commit

Permalink
滑动按钮效果优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqp2010 committed Dec 9, 2013
1 parent 6749f9d commit 7562ba3
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 18 deletions.
Binary file modified AndBase/bin/andbase.jar
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/sliding/AbSlidingButton.class
Binary file not shown.
3 changes: 3 additions & 0 deletions AndBase/bin/jarlist.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cache for current jar dependency. DO NOT EDIT.
# format is <lastModified> <length> <SHA-1> <path>
# Encoding is UTF-8
145 changes: 127 additions & 18 deletions AndBase/src/com/ab/view/sliding/AbSlidingButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.CheckBox;

import com.ab.global.AbAppData;
import com.ab.global.AbConstant;


// TODO: Auto-generated Javadoc
Expand Down Expand Up @@ -68,6 +71,7 @@ public class AbSlidingButton extends CheckBox {
private float mBtnOffPos;
private float mBtnOnPos;
private float mBtnPos;
private float mLastBtnPos;
private float mRealPos;
private float mBtnWidth;

Expand All @@ -80,6 +84,25 @@ public class AbSlidingButton extends CheckBox {
private Bitmap mCurBtnPic;
private float mFirstDownX;
private boolean mMoveEvent;
private boolean mAnimating;
private float mAnimationPosition;
private float mAnimatedVelocity;

private Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
mRealPos = getRealPos((Float)msg.obj);
mLastBtnPos = (Float)msg.obj;
invalidate();
break;
default:
break;
}
}
};

public AbSlidingButton(Context context) {
super(context);
Expand All @@ -98,6 +121,9 @@ private void init(Context context) {

this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE);

float density = getResources().getDisplayMetrics().density;
this.mAnimatedVelocity = (int)(0.5F + 350.0F * density);
}

protected void onDraw(Canvas canvas){
Expand Down Expand Up @@ -178,7 +204,7 @@ private float getRealPos(float paramFloat){
public boolean isChecked() {
return isChecked;
}

/**
*
* 描述:设置选中
Expand All @@ -188,13 +214,30 @@ public boolean isChecked() {
* @version v1.0
*/
public void setChecked(boolean isChecked) {
setChecked(isChecked,false);
}

/**
*
* 描述:设置选中
* @see android.widget.CompoundButton#setChecked(boolean)
* @author: zhaoqp
* @date:2013-11-29 下午3:54:33
* @version v1.0
*/
public void setChecked(boolean isChecked,boolean anim) {
this.isChecked = isChecked;
if(this.isChecked){
this.mBtnPos = this.mBtnOnPos;
}else{
this.mBtnPos = this.mBtnOffPos;
}
startAnimation();
if(anim){
startAnimation();
}else{
moveViewToTarget();
}

if(onCheckedChangeListener!=null){
onCheckedChangeListener.onCheckedChanged(this, isChecked);
}
Expand Down Expand Up @@ -223,6 +266,9 @@ public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
* @version v1.0
*/
public boolean onTouchEvent(MotionEvent event){
if(this.mAnimating){
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mMoveEvent = false;
Expand All @@ -236,17 +282,19 @@ public boolean onTouchEvent(MotionEvent event){
}else{
this.mBtnPos = this.mBtnOffPos;
}
Log.d(TAG, "原来的X位置:"+this.mBtnPos);
mLastBtnPos = mBtnPos;
if(D)Log.d(TAG, "原来的X位置:"+this.mBtnPos);
break;
case MotionEvent.ACTION_MOVE:

if(D)Log.d(TAG, "----移动----");
//当前点击位置
float x = event.getX();
//差
float offsetX = x - this.mFirstDownX;
Log.d(TAG, "X需要移动:"+offsetX);
if(D)Log.d(TAG, "X需要移动:"+offsetX);

if(offsetX < 5){
//转换为点击事件
if(Math.abs(offsetX) < 5){
break;
}else{
mMoveEvent = true;
Expand All @@ -256,7 +304,7 @@ public boolean onTouchEvent(MotionEvent event){

//移动后的应该在的位置
this.mBtnPos = this.mBtnPos + offsetX;
Log.d(TAG, "现在的X位置:"+this.mBtnPos);
if(D)Log.d(TAG, "现在的X位置:"+this.mBtnPos);
//超出控件的设置
if (this.mBtnPos < this.mBtnOffPos){
this.mBtnPos = this.mBtnOffPos;
Expand All @@ -266,23 +314,24 @@ public boolean onTouchEvent(MotionEvent event){
this.mBtnPos = this.mBtnOnPos;
}

startAnimation();
moveViewToTarget();
break;
default:
if(mMoveEvent){
if(D)Log.d(TAG, "----弹起----");
//弹起
this.mCurBtnPic = this.mBtnNormal;
//本次移动最后结果
if (this.mBtnPos < (this.mBtnOnPos-this.mBtnOffPos) / 2F + this.mBtnOffPos){
this.mBtnPos = this.mBtnOffPos;
}else{
this.mBtnPos = this.mBtnOnPos;
}
}
startAnimation();
offsetX = 0;
}else{
//点击事件
setChecked(!isChecked);
setChecked(!isChecked,true);
}

break;
Expand All @@ -294,28 +343,88 @@ public boolean onTouchEvent(MotionEvent event){
/**
*
* 描述:滑块移动
* @param paramFloat
* @param pos
* @throws
* @date:2013-12-5 上午11:46:56
* @version v1.0
*/
private void moveView(float paramFloat){
this.mBtnPos = paramFloat;
this.mRealPos = getRealPos(this.mBtnPos);
invalidate();
private void moveView(float pos){
moveView(pos,false);
}

/**
*
* 描述:滑块移动
* @param pos
* @param delay
* @throws
* @date:2013-12-5 上午11:46:56
* @version v1.0
*/
private void moveView(final float pos,boolean delay){
if(handler!=null){
handler.obtainMessage(0, pos).sendToTarget();
}
}

/**
*
* 描述:用位移加速度实现动画
* @throws
* @date:2013-12-9 上午9:47:59
* @version v1.0
*/
private void startAnimation(){
Log.d(TAG, "移动X:"+this.mBtnPos);
moveView(this.mBtnPos);
//已经在目标位置
if(mLastBtnPos == this.mBtnPos){
return;
}
this.mAnimating = true;
if(D)Log.d(TAG, "目标移动X到:"+this.mBtnPos+",当前在:"+mLastBtnPos);
float mVelocity = this.mAnimatedVelocity;
if(mLastBtnPos > this.mBtnPos){
mVelocity = -this.mAnimatedVelocity;
}
this.mAnimationPosition = mLastBtnPos;
int i = 0;
while(true){
this.mAnimationPosition = (this.mAnimationPosition + 16.0F * mVelocity / 1000.0F);
if(D)Log.d(TAG, i+"次移动X到:"+this.mAnimationPosition);
if(this.mAnimationPosition >= this.mBtnOnPos){
this.mAnimationPosition = this.mBtnOnPos;
moveView(this.mAnimationPosition,true);
isChecked = true;
break;
}else if(this.mAnimationPosition <= this.mBtnOffPos){
this.mAnimationPosition = this.mBtnOffPos;
moveView(this.mAnimationPosition,true);
isChecked = false;
break;
}else{
moveView(this.mAnimationPosition,true);
}
i++;
}
this.mAnimating = false;

}

/**
*
* 描述:直接移动到位置
* @throws
* @date:2013-12-9 上午9:20:38
* @version v1.0
*/
private void moveViewToTarget(){
moveView(this.mBtnPos);
if (this.mBtnPos == this.mBtnOnPos){
isChecked = true;
return;
}else if(this.mBtnPos == this.mBtnOffPos){
isChecked = false;
return;
}

}


Expand Down
Binary file modified AndBaseDemo/bin/AndBaseDemo.apk
Binary file not shown.
Binary file modified AndBaseDemo/bin/classes.dex
Binary file not shown.
Binary file not shown.
Binary file modified AndBaseDemo/bin/resources.ap_
Binary file not shown.

0 comments on commit 7562ba3

Please sign in to comment.