弹性滑动(二)--使用ViewHelper
来源:互联网 发布:mac大于号怎么打出来 编辑:程序博客网 时间:2024/06/06 16:36
使用nineoldandroids开源动画库.
- 通过getRawX和getRawY获得手指当前坐标A(x,y).
- 与上一次移动后的控件坐标B(mLastX,mLastY)相减得到偏移量坐标C(deltaX,deltaY),B初始化为(0,0).
- 使用ViewHelper类将控件进行动画偏移,偏移C.
- 将手指坐标即当前坐标A赋值给B.
- 循环上述操作.
例子:将button随手指移动而移动
自定义button
package com.example.test;import com.nineoldandroids.view.ViewHelper;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.Button;public class MyView extends Button { int mLastX = 0; // 上次移动后最终的横坐标 int mLastY = 0; // 上次移动后最重的纵坐标 public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { // 当前手指坐标 int x = (int) event.getRawX(); int y = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int deltaX = x - mLastX; // x方向移动量 int deltaY = y - mLastY; // y方向移动量 int translationX = (int) (ViewHelper.getTranslationX(this) + deltaX); // x方向平移deltaX int translationY = (int) (ViewHelper.getTranslationY(this) + deltaY); // y方向平移deltaY ViewHelper.setTranslationX(this, translationX); ViewHelper.setTranslationY(this, translationY); break; case MotionEvent.ACTION_UP: break; } // 更新位置 mLastX = x; mLastY = y; return true; }}
布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.test.MyView android:id="@+id/btn" android:layout_width="100dp" android:layout_height="100dp" android:text="qqq" /></LinearLayout>
Demo下载地址:
http://pan.baidu.com/s/1jIdfhPk
0 0
- 弹性滑动(二)--使用ViewHelper
- Android Scroll实现弹性滑动 二 随手弹性滑动
- 使用动画完成弹性滑动
- 使用Scoller实现弹性滑动
- 弹性滑动(一)--使用Scroller
- 使用Scroller实现弹性滑动
- ViewHelper的使用
- Scroller弹性滑动对象(二)
- ViewHelper
- 弹性滑动
- 使用延时策略实现弹性滑动
- 使用Scroller实现View的弹性滑动
- android使用延时策略实现弹性滑动
- NineOldAndroids动画兼容库的使用-ViewHelper
- 滑动盒子/弹性盒子
- Android弹性滑动
- 弹性滑动导航菜单:
- 实现弹性滑动
- sqoop安装与配置
- Git小白教程2---远程仓库
- 成熟的夜间模式解决方案
- maven项目在eclipse的library中没有Maven Dependencies
- 好好活着,因为我们睡了就再也醒不过来了
- 弹性滑动(二)--使用ViewHelper
- OpenCV基础介绍
- 开发环境—The selected wizard could not be started
- MySQL 安装
- zookeeper安装与配置
- Java代码中判断字符是否为中文或中文符号
- win10怎么给多个桌面屏幕设置不同壁纸
- R语言Formula/公式转化为数值
- Python使用Ckan API查询用户的私有数据集