View的滑动及一个跟手滑动的效果

来源:互联网 发布:书生电子图书数据库 编辑:程序博客网 时间:2024/06/01 07:28

在Android开发中,经常会遇到一个view需要它能够支持滑动的需求。

安卓中,实现view的滑动可以通过三种方式:

1. 通过View本身提供的scrollTo和scrollBy方法;

2. 通过给View添加平移动画来实现平移效果;

3. 通过改变View的LayoutParams使得View重新布局;


1.使用scrollTo和scrollBy;

 /**     * 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.     * @param x the x position to scroll to     * @param y the y position to scroll to     */    public void scrollTo(int x, int y) {        if (mScrollX != x || mScrollY != y) {            int oldX = 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.     * @param x the amount of pixels to scroll by horizontally     * @param y the amount of pixels to scroll by vertically     */    public void scrollBy(int x, int y) {        scrollTo(mScrollX + x, mScrollY + y);    }

通过上面的源码,我们可以看出scrollBy其实也是调用了scrollTo方法从而实现View相对于当前位置的滑动,而scrollTo则是实现了基于所传递参数的绝对滑动。

且使用scrollTo和scrollBy实现滑动滑动的是View中的内容,而不是View本身,也就是说无论怎么滑,移动的都是View的内部内容而无法将当前View滑动到附近View所在的区域。




2.使用动画

使用动画中的translation属性实现View的平移。安卓3.0以前没有属性动画,所以动画后View并不会真的移动位置,如果用户点击移动后的View就不能执行设置的onClick方法.但是3.0以后的版本就不存在这个问题.

为了更好的兼容,此处推荐使用Github大神JakeWharton写的开源动画库nineoldandroids.







3. 改变布局参数

通过改变布局参数(LayoutParams)只需要将View的Margin等属性设置好就可以实现此View的平移.相比起前两种方法这种方法操作起来要稍微复杂一些,但更适用于3.0以下版本的有交互的View.



接下来实现一个利用动画效果实现View跟手滑动的效果:


布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.panda.view.MainActivity" >    <Button         android:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="拖"        android:layout_centerInParent="true"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv"        /></RelativeLayout>


JAVA代码: 

package com.panda.view;import com.nineoldandroids.view.ViewHelper;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {Button bt;TextView tv;int mLastX=0;int mLastY=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt=(Button) findViewById(R.id.bt);tv=(TextView) findViewById(R.id.tv);bt.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {int x=(int) event.getRawX();int y=(int) event.getRawY();switch (event.getAction()) {case MotionEvent.ACTION_MOVE:int deltaX =x-mLastX;int deltaY =y-mLastY;int tX=(int) (ViewHelper.getTranslationX(v)+deltaX);int tY=(int) (ViewHelper.getTranslationY(v)+deltaY);ViewHelper.setTranslationX(v, tX);ViewHelper.setTranslationY(v, tY);tv.setText("x: "+x+"   y: "+y);break;default:break;}mLastX=x;mLastY=y;return true;}});}}

此处加了一个专门显示Button绝对坐标的TextView,效果图如下:




0 0
原创粉丝点击