Android控件拖动
来源:互联网 发布:淡雅女士香水知乎 编辑:程序博客网 时间:2024/06/02 13:41
这里演示控件拖动的动画:
原理就是响应控件的Touch事件,在Touch事件中对移动进行处理,注意,一定要在布局文件中设置控件的
android:clickable="true"。
代码如下:
package com.zhycheng.draftdemo;import android.app.Activity;import android.os.Bundle;import android.view.Display;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;public class DraftDemoActivity extends Activity implements OnTouchListener {ImageView iv;int screenWidth,screenHeight;int lastX,lastY; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv=(ImageView) findViewById(R.id.imageView1); iv.setOnTouchListener(this); Display dis=this.getWindowManager().getDefaultDisplay(); screenWidth=dis.getWidth(); screenHeight=dis.getHeight(); }@Overridepublic boolean onTouch(View v, MotionEvent event) {switch(event.getAction()){case MotionEvent.ACTION_DOWN:lastX=(int)event.getRawX();lastY=(int)event.getRawY();break;case MotionEvent.ACTION_MOVE:int dx=(int)event.getRawX()-lastX;int dy=(int)event.getRawY()-lastY;int top=v.getTop()+dy;int left=v.getLeft()+dx;if(top<=0){top=0;}if(top>=screenHeight-iv.getHeight()){top=screenHeight-iv.getHeight();}if(left>=screenWidth-iv.getWidth()){left=screenWidth-iv.getWidth();}if(left<=0){left=0;}v.layout(left, top, left+iv.getWidth(), top+iv.getHeight());lastX=(int)event.getRawX();lastY=(int)event.getRawY();break;case MotionEvent.ACTION_UP:break;}return false;}}
工程代码下载:Android控件拖动
- Android 控件 拖动
- Android拖动控件
- Android 控件随意拖动
- Android拖动控件
- Android控件拖动
- Android控件拖动
- android 拖动控件例子
- android控件拖动
- android 可拖动控件
- Android控件的拖动
- Android控件拖动效果
- Android 控件的拖动
- Android控件拖动实现
- Android控件拖动的实现
- Android控件拖动的实现
- android可拖动控件初探
- Android控件拖动的实现
- Android控件拖动的实现
- php试题与答案(二)
- libc.so.6: not found
- 判断输入的日期是一年中第几天(调用函数)
- 虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
- android SIM卡状态
- Android控件拖动
- 【基础知识】使用Darts
- 电池电量分析 从上层到底层
- linux设置IP
- JAVA如何实现条件编译(从思路开始)
- 自定义el表达式方法
- atoi 和itoa用法
- 自动变色的小程序(点击可停止变色也可显示当前颜色的16进制数)
- 设计模式学习-----外观模式