自定义滑动按钮
下载地址:http://download.youkuaiyun.com/my/uploads
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.customslidedemo.MainActivity" >
<com.example.customslidedemo.SlipButton
android:id="@+id/slipButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="@drawable/ic_launcher"
android:gravity="center"
android:text="滑动按钮"
android:textColor="#ff00ff"
android:textSize="16sp" />
</LinearLayout>
2.自定义一个类SlipButton
package com.example.customslidedemo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.TextView;
/**
* 自定义滑动按钮
* @author haiyangyin
*
*/
public class SlipButton extends TextView {
private int startX;
private int lastX;
private int screenWidth ;
private int oldLeft, oldRight, top, bottom;
public SlipButton(Context context) {
super(context);
init();
}
public SlipButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SlipButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX();//获取点击事件距离整个屏幕左边的距离
lastX = (int) event.getRawX();
top = this.getTop();//view自身的顶边到其父布局顶边的距离
bottom = this.getBottom();//view自身的底边到其父布局顶边的距离
oldLeft = this.getLeft();//view自身的左边到其父布局左边的距离
oldRight = this.getRight();//view自身的右边到其父布局左边的距离
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;//移动的距离
int left = this.getLeft() + dx;
int right = this.getRight() + dx;
//控制按钮滑动范围 不会滑出屏幕
if (left < 0) {
left = 0;
right = left + this.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - this.getWidth();
}
this.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
break;
case MotionEvent.ACTION_UP:
if (Math.abs(lastX - startX) >= screenWidth / 2) {//滑动的距离大于等于屏幕宽度的一半 执行以下操作
// Toast.makeText(MainActivity.this, "大于半屏了", 0).show();
System.out.println("ddddddd");
lister.success();
} else {
// Toast.makeText(MainActivity.this, "不到半屏", 0).show();
System.out.println("eeee");
}
this.layout(oldLeft, top, oldRight, bottom);// 放置view的位置
break;
}
return true;
}
private OnChangedStateListener lister;
public void setOnChangedListener(OnChangedStateListener l) {// 设置监听器,当状态修改的时候
this.lister = l;
}
public interface OnChangedStateListener {
abstract void success();
}
}
3.在MainActivity里调用
package com.example.customslidedemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.customslidedemo.SlipButton.OnChangedStateListener;
public class MainActivity extends Activity {
private SlipButton mSlipButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSlipButton=(SlipButton) this.findViewById(R.id.slipButton);
mSlipButton.setOnChangedListener(new OnChangedStateListener() {
@Override
public void success() {
// TODO Auto-generated method stub
//成功 执行自己的逻辑
Toast.makeText(MainActivity.this, "成功了!!!!!!!!!", Toast.LENGTH_SHORT);
}
});
}
}