效果图 代码 public class DragView extends ImageView implements View.OnTouchListener{ pri
效果图
代码
public class DragView extends ImageView implements View.OnTouchListener{
private int screenWidth;
private int screenHeight;
private Context mContext;
private int lastX, lastY;
private int left ,top;
private ViewGroup.MarginLayoutParams layoutParams;
private int startX;
private int endX;
private boolean isMoved = false;
private onDragViewClickListener mLister;
public interface onDragViewClickListener{
void onDragViewClick();
}
public void setOnDragViewClickListener(onDragViewClickListener listener){
this.mLister = listener;
}
public DragView(Context context) {
this(context,null);
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels-getStatusBarHeight();
init();
}
public void init(){
setOnTouchListener(this);
post(new Runnable() {
@Override
public void run() {
layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
layoutParams.topMargin = screenHeight - getHeight();
layoutParams.leftMargin = screenWidth - getWidth();
setLayoutParams(layoutParams);
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
startX = lastX;
break;
case MotionEvent.ACTION_MOVE:
isMoved = true;
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
left = v.getLeft() + dx;
top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
// 设置不能出界
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom );
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//只有滑动改变上边距时,抬起才进行设置
if (isMoved) {
layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
layoutParams.topMargin = top;
setLayoutParams(layoutParams);
}
endX = (int) event.getRawX();
//滑动距离比较小,当作点击事件处理
if (Math.abs(startX - endX) < 6) {
return false;
}
if (left +v.getWidth()/2 < screenWidth/2) {
startScroll(left,screenWidth/2,true);
} else {
startScroll(left,screenWidth/2,false);
}
break;
}
return true;
}
//在此处理点击事件
@Override
public boolean onTouchEvent(MotionEvent event) {
mLister.onDragViewClick();
return super.onTouchEvent(event);
}
public void startScroll(final int start, int end, final boolean isLeft){
ValueAnimator valueAnimator = ValueAnimator.ofFloat(start,end).setDuration(800);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (isLeft) {
layoutParams.leftMargin = (int) (start*(1-animation.getAnimatedFraction()));
} else {
layoutParams.leftMargin = (int) (start + (screenWidth - start - getWidth())*(animation.getAnimatedFraction()));
}
setLayoutParams(layoutParams);
}
});
valueAnimator.start();
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "Android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
以上所述是小编给大家介绍的Android实现跟随手指拖动并自动贴边的View样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android自定义圆形View实现小球跟随手指移动效果Android实现拖动小球跟随手指移动效果Android中View跟随手指移动效果Android自定义View圆形和拖动圆、跟随手指拖动效果Android实现View拖拽跟随手指移动效果Android中View跟随手指滑动效果的实例代码Android自定义View圆形和拖动圆跟随手指拖动Android游戏开发学习①弹跳小球实现方法Android开发实现跟随手指的小球效果示例
--结束END--
本文标题: Android实现跟随手指拖动并自动贴边的View样式(实例demo)
本文链接: https://lsjlt.com/news/22607.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0