android三种方式实现自由移动的view
来源:互联网 发布:matlab生成任意矩阵 编辑:程序博客网 时间:2024/05/16 06:57
描述
实现一个可以在父容器中随着手指自由移动的view
思路
1.继承view,重写onTouchEvent()方法
2.通过getX(),getY()方法获取新的位置
3.将控件移动到当前位置
4.继承View实现感觉更高大上,这里继承ImageView是为了设置背景方便观察
实战
1.通过setX(),setY()来重新设置view的位置
public class MoveView extends ImageView { private int x, y; public MoveView(Context context) { this(context, null); } public MoveView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MoveView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { x = (int) event.getX(); y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: AnimatorSet setDown = new AnimatorSet(); setDown.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1f, 1.5f), ObjectAnimator.ofFloat(this, "scaleY", 1f, 1.5f), ObjectAnimator.ofFloat(this, "alpha", 1f, 0.5f) ); setDown.start(); break; case MotionEvent.ACTION_MOVE: //个人感觉跟手时,指尖在控件的中间比较好,所以减去宽高的一半 setX(x + getLeft() + getTranslationX() - getWidth() / 2); setY(y + getTop() + getTranslationY() - getHeight() / 2); break; case MotionEvent.ACTION_UP: AnimatorSet setUp = new AnimatorSet(); setUp.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1.5f, 1f), ObjectAnimator.ofFloat(this, "scaleY", 1.5f, 1f), ObjectAnimator.ofFloat(this, "alpha", 0.5f, 1f) ); setUp.start(); break; } return true; }}
在view按下时添加了动画。滑动时跟手的感觉很强,实现简洁。
2.通过layout()来重新设置view的位置
public class MoveView extends ImageView { private int x, y; private int r, l, t, b; public MoveView(Context context) { this(context, null); } public MoveView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MoveView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { x = (int) event.getX(); y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: AnimatorSet setDown = new AnimatorSet(); setDown.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1f, 1.5f), ObjectAnimator.ofFloat(this, "scaleY", 1f, 1.5f), ObjectAnimator.ofFloat(this, "alpha", 1f, 0.5f) ); setDown.start(); break; case MotionEvent.ACTION_MOVE: l = (int) (x + getTranslationX() - getWidth() / 2 + getLeft()); t = (int) (y + getTranslationY() - getHeight() / 2 + getTop()); r = l + getWidth(); b = t + getHeight(); layout(l,t,r,b); break; case MotionEvent.ACTION_UP: AnimatorSet setUp = new AnimatorSet(); setUp.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1.5f, 1f), ObjectAnimator.ofFloat(this, "scaleY", 1.5f, 1f), ObjectAnimator.ofFloat(this, "alpha", 0.5f, 1f) ); setUp.start(); break; } return true; }}
原理跟第一种方式差不多。滑动时跟手的感觉很强,代码多几行。
3.属性动画
public class MoveView extends ImageView { private int x, y; private int r, l, t, b; public MoveView(Context context) { this(context, null); } public MoveView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MoveView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { x = (int) event.getX(); y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: AnimatorSet setDown = new AnimatorSet(); setDown.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1f, 1.5f), ObjectAnimator.ofFloat(this, "scaleY", 1f, 1.5f), ObjectAnimator.ofFloat(this, "alpha", 1f, 0.5f) ); setDown.start(); break; case MotionEvent.ACTION_MOVE: l = (int) (x + getTranslationX() - getWidth() / 2 + getLeft()); t = (int) (y + getTranslationY() - getHeight() / 2 + getTop()); ObjectAnimator.ofFloat(this, View.TRANSLATION_X, l).start(); ObjectAnimator.ofFloat(this, View.TRANSLATION_Y, t).start(); break; case MotionEvent.ACTION_UP: AnimatorSet setUp = new AnimatorSet(); setUp.playTogether( ObjectAnimator.ofFloat(this, "scaleX", 1.5f, 1f), ObjectAnimator.ofFloat(this, "scaleY", 1.5f, 1f), ObjectAnimator.ofFloat(this, "alpha", 0.5f, 1f) ); setUp.start(); break; } return true; }}
跟手感差,3.0后才能使用
总结
1.scrollTo只能对view的内容滑动,不适应当前继承View的场景
2.ViewDragHelper实现也可,代码就不贴了
3.也可以通过继承ViewGroup实现对子view的自由移动
0 0
- android三种方式实现自由移动的view
- Android View 移动的几种方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的三种实现方式
- Android自定义View的实现方式(三种)
- android自定义view实现可自由放大缩小和移动的imageView
- android 实现自由移动的悬浮按钮
- android view滑动的三种方式
- 安卓实现自由拖拽view的几种方式
- android view 的移动的几种方式
- Apache shiro集群实现 (一) shiro入门介绍
- meter-察看结果树-响应数据,中文显示乱码问题处理
- A simple Scala call-by-name example
- 九十分钟极速入门Linux——Linux Guide for Developments 学习笔记
- Service的粘性
- android三种方式实现自由移动的view
- easyui datagrid 行数据处理
- 双边滤波(bilateral filter)快速算法 matlab实现代码
- ifconfig中lo、eth0、br0接口
- leetcode之3Sum
- C# 序列化
- 结构体
- python入门笔记(2)
- 基于事件分发机制,以最小代价实现listview顶部悬浮效果