zoukankan      html  css  js  c++  java
  • android 使用Scroller实现缓慢移动

    在Launcher中的Workspace中实现了左右屏幕切换效果,里面就用到了Scroller记录滑动轨迹,实现一种缓慢地向左或向右移动的效果,这里我对这种效果进行总结:

    我们先看一个例子:点击按钮时红经块会从左边缓慢地移向左右,这个该怎么实现呢

    红色块在右边 

    我们先来看一下,Scroller,这个对象里有startScroll方法 

     void android.widget.Scroller.startScroll(int startX, int startY, int dx, int dy, int duration)
    第一个参数是起始移动的x坐标值,第二个是起始移动的y坐标值,第三个第四个参数都是移到某点的坐标值,而duration 当然就是执行移动的时间。这个有什么用呢。要知道有什么用还得再看一个方法

    boolean android.widget.Scroller.computeScrollOffset()
     

    当startScroll执行过程中即在duration时间内,computeScrollOffset  方法会一直返回false,但当动画执行完成后会返回返加true.

    有了这两个方法还不够,我们还需要再重写viewGroup的一个方法,

    computeScroll 这个方法什么时候会被调用呢

    官网上这样说的

    public void computeScroll ()

    Since: API Level 1

    Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller object.

    当我们执行ontouch或invalidate()或postInvalidate()都会导致这个方法的执行

    所以我们像下面这样调用,postInvalidate执行后,会去调computeScroll 方法,而这个方法里再去调postInvalidate,这样就可以不断地去调用scrollTo方法了,直到mScroller动画结束,当然第一次时,我们需要手动去调用一次postInvalidate才会去调用 

    computeScroll 方法

    [java] view plaincopy
     
    1. @Override  
    2. public void computeScroll() {  
    3.     if (mScroller.computeScrollOffset()) {  
    4.         scrollTo(mScroller.getCurrX(), 0);  
    5.         postInvalidate();  
    6.     }  
    7. }  


    下面附上上面那个例子的源代码

    首先是MyViewGroup.java

    [java] view plaincopy
     
    1. package com.wb;  
    2.   
    3. import android.content.Context;  
    4. import android.util.AttributeSet;  
    5. import android.view.View;  
    6. import android.view.ViewGroup;  
    7. import android.widget.LinearLayout;  
    8. import android.widget.Scroller;  
    9.   
    10. public class MyViewGroup extends LinearLayout {  
    11.     private boolean s1=true;  
    12.     Scroller mScroller=null;  
    13.     public MyViewGroup(Context context, AttributeSet attrs) {  
    14.         super(context, attrs);  
    15.         mScroller=new Scroller(context);  
    16.         // TODO Auto-generated constructor stub  
    17.     }  
    18.     @Override  
    19.     public void computeScroll() {  
    20.         if (mScroller.computeScrollOffset()) {  
    21.             scrollTo(mScroller.getCurrX(), 0);  
    22.             postInvalidate();  
    23.         }  
    24.     }  
    25.     public void beginScroll(){  
    26.         if (!s1) {  
    27.             mScroller.startScroll(0, 0, 0, 0, 1000);  
    28.             s1 = true;  
    29.         } else {  
    30.             mScroller.startScroll(0, 0, -500, 0, 1000);  
    31.             s1 = false;  
    32.         }  
    33.         invalidate();  
    34.     }  
    35. }  


    然后是WheelActivity.java

    [java] view plaincopy
     
    1. package com.wb;  
    2.   
    3. import android.app.Activity;  
    4. import android.graphics.Color;  
    5. import android.os.Bundle;  
    6. import android.view.Gravity;  
    7. import android.view.View;  
    8. import android.view.ViewGroup;  
    9. import android.widget.AbsListView;  
    10. import android.widget.AbsListView.LayoutParams;  
    11. import android.widget.AbsListView.OnScrollListener;  
    12. import android.widget.Adapter;  
    13. import android.widget.BaseAdapter;  
    14. import android.widget.ListView;  
    15. import android.widget.TextView;  
    16.   
    17. public class WheelActivity extends Activity {  
    18.   
    19.     private ListView listView = null;  
    20.     private MyViewGroup myViewGroup;  
    21.   
    22.     @Override  
    23.     public void onCreate(Bundle savedInstanceState) {  
    24.         super.onCreate(savedInstanceState);  
    25.         setContentView(R.layout.main);  
    26.         myViewGroup = (MyViewGroup) findViewById(R.id.myviewGroup);  
    27.   
    28.     }  
    29.   
    30.     public void scroll(View view) {  
    31.   
    32.         myViewGroup.beginScroll();  
    33.   
    34.     }  
    35.   
    36. }  

    main.xml

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <Button  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:text="scroll"  
    11.         android:onClick="scroll" />  
    12.   
    13.     <com.wb.MyViewGroup  
    14.         xmlns:android="http://schemas.android.com/apk/res/android"  
    15.         android:layout_width="fill_parent"  
    16.         android:layout_height="fill_parent"  
    17.         android:orientation="vertical" android:id="@+id/myviewGroup">  
    18.   
    19.         <TextView  
    20.             android:layout_width="wrap_content"  
    21.             android:layout_height="fill_parent"  
    22.             android:background="#ff0000"  
    23.             android:text="我在這"/>  
    24.   
    25.     
    26.     </com.wb.MyViewGroup>  
    27.   
    28. </LinearLayout>  


    源代码下载地址:http://download.csdn.net/detail/c_weibin/4208751

  • 相关阅读:
    Asp.Net MVC 路由
    EntityFramework执行SQL语句
    partial类与[MetadataType(typeof(类名))]有什么区别?
    Asp.net mvc + .net ef database first 或 model first 时如何添加验证特性
    Entity Framework 学习总结之一:ADO.NET 实体框架概述
    C#中dynamic的正确用法
    Func与Action
    SVN的使用和问题解决方法总结
    微信小程序---绘图
    微信小程序---动画
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4800090.html
Copyright © 2011-2022 走看看