view的基本介绍
来源:互联网 发布:刀路仿真软件 编辑:程序博客网 时间:2024/04/29 13:18
View是android系统中所有控件的基类,是一种界面层空间的抽象。
下面开始介绍view的基本api
1.view的位置参数
相对父容器位置坐标
Left=getLeft();
Right=getRight();
Top=getTop();
bottom=getBottom();
可以通过以上方法得出view的宽高
width=right-left
height=top-bottom
2.MotionEvent和TouchSlop
1)MotionEvent
- ACTION_DOWN-手指刚接触屏幕
- ACTION_MOVE-手指在屏幕上移动
- ACTION_UP-手指从屏幕上松开的一瞬间
通过MotionEvent对象可以得到点击事件发生的x和y坐标
系统提供两个方法getX/getY和getRawX/getRawY
区别:getX/getY返回当前view上的x和y坐标
getRawX/getRawY相对于手机屏幕的x和y坐标
2)TouchSlop
TouchSlop是系统所能识别出滑动的最小距离。这是一个常量,具体的值和设备有关系
ViewConfiguration.get(this).getScaledTouchSlop();
3.VelocityTracker、GestureDetector和Scroller
1)VelocityTracker
用于追踪手指滑动过程中的速度
longtmp;
@Override
public booleanonTouchEvent(MotionEvent event) {
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
Trace.e("ACTION_DOWN");
tmp=System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.addMovement(event);
// Trace.d("ACTION_MOVE:x="+event.getX()+",y="+event.getY());
break;
case MotionEvent.ACTION_UP:
Trace.e("ACTION_UP");
long during=System.currentTimeMillis()-tmp;
velocityTracker.computeCurrentVelocity((int) during);
int xVelocity= (int)velocityTracker.getXVelocity();
int yVelocity= (int)velocityTracker.getYVelocity();
Trace.e("xVelocity:"+xVelocity+",yVelocity"+yVelocity);
break;
default:
break;
}
return super.onTouchEvent(event);
@Override
public booleanonTouchEvent(MotionEvent event) {
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
Trace.e("ACTION_DOWN");
tmp=System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.addMovement(event);
// Trace.d("ACTION_MOVE:x="+event.getX()+",y="+event.getY());
break;
case MotionEvent.ACTION_UP:
Trace.e("ACTION_UP");
long during=System.currentTimeMillis()-tmp;
velocityTracker.computeCurrentVelocity((int) during);
int xVelocity= (int)velocityTracker.getXVelocity();
int yVelocity= (int)velocityTracker.getYVelocity();
Trace.e("xVelocity:"+xVelocity+",yVelocity"+yVelocity);
break;
default:
break;
}
return super.onTouchEvent(event);
}
2)GestureDetector
用于手势检测,检测用户的点击、滑动、长按、双击等行为。
private voidinitializeView() {
mGestureDetector=newGestureDetector(this);
mGestureDetector=newGestureDetector(this);
mGestureDetector.setIsLongpressEnabled(false);
}
3.Scroller 弹性滑动对象
实现view滑动的三种方式
1)使用scrollTo/scrollBy
view提供两种方法实现滑动效果
/**
* Set the scrolled position of your view. This will cause a call to
* {@link#onScrollChanged(int,int,int,int)} and the view will be
* invalidated.
* @paramxthe x position to scroll to
* @paramythe y position to scroll to
*/
public voidscrollTo(intx, inty) {
if(mScrollX!= x ||mScrollY!= y) {
intoldX =mScrollX;
int oldY =mScrollY;
mScrollX= x;
mScrollY= y;
invalidateParentCaches();
onScrollChanged(mScrollX,mScrollY,oldX,oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
* Set the scrolled position of your view. This will cause a call to
* {@link#onScrollChanged(int,int,int,int)} and the view will be
* invalidated.
* @paramxthe x position to scroll to
* @paramythe y position to scroll to
*/
public voidscrollTo(intx, inty) {
if(mScrollX!= x ||mScrollY!= y) {
intoldX =mScrollX;
int oldY =mScrollY;
mScrollX= x;
mScrollY= y;
invalidateParentCaches();
onScrollChanged(mScrollX,mScrollY,oldX,oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
}
/**
* Move the scrolled position of your view. This will cause a call to
* {@link#onScrollChanged(int,int,int,int)} and the view will be
* invalidated.
* @paramxthe amount of pixels to scroll by horizontally
* @paramythe amount of pixels to scroll by vertically
*/
public voidscrollBy(intx, inty) {
scrollTo(mScrollX+ x,mScrollY+ y);
* Move the scrolled position of your view. This will cause a call to
* {@link#onScrollChanged(int,int,int,int)} and the view will be
* invalidated.
* @paramxthe amount of pixels to scroll by horizontally
* @paramythe amount of pixels to scroll by vertically
*/
public voidscrollBy(intx, inty) {
scrollTo(mScrollX+ x,mScrollY+ y);
}
从源码可以看出scrollBy其实内部就是调用了scrollTo方法
两个方法的区别:scrollBy实现了基于当前位置的滑动,scrollTo实现基于传进参数的绝对滑动
注意scrollBy和scrollTo并不能改变view在布局中的位置,只能改变mScrollX,mScrollY
如何让view动起来?
调用view的invalidate方法 会导致view的draw方法被调用 draw方法在去调用computeScroll
@Override
public voidcomputeScroll() {
if(mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
public voidcomputeScroll() {
if(mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
}
postInvalidateI()方法进行二次重绘和第一次重绘一样,还是会调用computeScroll 然后继续获取当前scrollX和scrollY,如此反复,直到整个滑动过程结束
2)使用动画
可以使用动画效果来实现view的滑动
动画操作的是view的translationX和translationY两个属性(view的绝对位置)
实现方式:可以采用view的传统动画和(v3.0)属性动画(为了能兼容3.0以下版本需要采用兼容库nineoldandroids)
下面我们来实现一个动画场景:
view在100ms内向右平移100像素
@Override
public voidonClick(View v) {
floatx=findViewById(R.id.mAnimalBtn).getX();
ObjectAnimator.ofFloat(findViewById(R.id.mAnimalBtn),"translationX",x,x+100).setDuration(100).start();
public voidonClick(View v) {
floatx=findViewById(R.id.mAnimalBtn).getX();
ObjectAnimator.ofFloat(findViewById(R.id.mAnimalBtn),"translationX",x,x+100).setDuration(100).start();
}
可以利用动画来模仿Scroller实现view的弹性滑动
mAnimalBtn= (Button) findViewById(R.id.mAnimalBtn);
final int startX =0;
final int deltaX =100;
final ValueAnimator animator = ValueAnimator.ofInt(0,1).setDuration(1000);
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
@Override
public voidonAnimationUpdate(ValueAnimator animation) {
floatfraction = animation.getAnimatedFraction();
mAnimalBtn.scrollTo((int) ( startX+deltaX* fraction),0);
}
});
mAnimalBtn.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
animator.start();
}
final int startX =0;
final int deltaX =100;
final ValueAnimator animator = ValueAnimator.ofInt(0,1).setDuration(1000);
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
@Override
public voidonAnimationUpdate(ValueAnimator animation) {
floatfraction = animation.getAnimatedFraction();
mAnimalBtn.scrollTo((int) ( startX+deltaX* fraction),0);
}
});
mAnimalBtn.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
animator.start();
}
});
0 0
- view的基本介绍
- View的基本位置参数介绍
- UIView 的简单介绍和一些基本view的使用
- View的基本绘制
- html view的基本框架
- 自定义View的基本步骤
- 自定义view的基本流程
- 视图View的基本属性
- 自定义view的基本运用
- 关于Table View的介绍
- Struts的基本介绍
- Invision的基本介绍
- 基本的RAID介绍
- Silverlight 的基本介绍
- UI的基本介绍
- 基本的RAID介绍
- maven的基本介绍
- Shellcode的基本介绍
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- 音视频编码格式汇总
- 一次完整的HTTP事务是怎样一个过程
- 双目测距
- Nginx配置文件详细说明
- view的基本介绍
- C++命名空间
- rman
- sizeof与strlen
- 基于AJAX的长轮询(long-polling)方式实现COMET例子
- 双向链表学习
- js的call函数
- JDBC高级编程笔记
- android基础部分再学习---Binder