Android滑动控件.md
1.概述
最近写代码临时加了个功能主要是滑动选择的功能效果图如下:
2.代码
这里主要是用属性动画做的
<ImageButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab"
android:background="@drawable/selector_add_fab"
android:clickable="true" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 获得焦点但未按下时的背景图片 -->
<item android:drawable="@drawable/add_fab" android:state_enabled="true" android:state_focused="false" android:state_pressed="false" />
<item android:drawable="@drawable/add_fab_un" android:state_enabled="false" android:textColor="@color/white" />
<!-- 按下时的背景图片 -->
<item android:drawable="@drawable/add_fab_un" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="@drawable/add_fab_un" android:state_enabled="true" android:state_focused="true" />
</selector>
private void setImageButtonTouch() {
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - DensityUtil.dpToPx(activity, 70);
fb.setOnTouchListener(new View.OnTouchListener() {
float lastX = 0;
float lastY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "ontouch" + event.getActionMasked());
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
lastX = event.getRawX();
lastY = event.getRawY();
isDrag = false;
break;
case MotionEvent.ACTION_MOVE:
// 不要直接用getX和getY,这两个获取的数据已经是经过处理的,容易出现图片抖动的情况
isDrag = true;
float distanceX = lastX - event.getRawX();
float distanceY = lastY - event.getRawY();
float nextY = fb.getY() - distanceY;
float nextX = fb.getX() - distanceX;
// 不能移出屏幕
if (nextY < 0) {
nextY = 0;
} else if (nextY > screenHeight - fb.getHeight()) {
nextY = screenHeight - fb.getHeight();
}
if (nextX < 0)
nextX = 0;
else if (nextX > screenWidth - fb.getWidth())
nextX = screenWidth - fb.getWidth();
// 属性动画移动
ObjectAnimator y = ObjectAnimator.ofFloat(fb, "y", fb.getY(), nextY);
ObjectAnimator x = ObjectAnimator.ofFloat(fb, "x", fb.getX(), nextX);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(x, y);
animatorSet.setDuration(0);
animatorSet.start();
lastX = event.getRawX();
lastY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
if (!isDrag) {
startActivity(new Intent(activity, OtherActivity.class));
}
break;
}
return false;
}
});
}
3.踩坑
这里有个需要注意的是我一开始用的是
view.layout(上,下,左,右)设置位置,这样实际上是改变不了他的实际位置,只能改变视图,每次数据刷新或者点击原来位置都会恢复到原来的位置
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int l = v.getLeft() + dx;
int b = v.getBottom() + dy;
int r = v.getRight() + dx;
int t = v.getTop() + dy;
// 下面判断移动是否超出屏幕
if (l < 0) {
l = 0;
r = l + v.getWidth();
}
if (t < 0) {
t = 0;
b = t + v.getHeight();
}
if (r > screenWidth) {
r = screenWidth;
l = r - v.getWidth();
}
if (b > screenHeight) {
b = screenHeight;
t = b - v.getHeight();
}
v.layout(l, t, r, b);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
转载请注明来源谢谢