Scroller 的运用案例(一)

来源:互联网 发布:里基戴维斯刷数据 编辑:程序博客网 时间:2024/06/05 03:24

此类内容也不多,而我们经常用到的就更少了,

我在这里记录下我经常用到的几个知识点 :

第一个知识点:public void startScroll(int startX, int startY, int dx, int dy) {    startScroll(startX, startY, dx, dy, DEFAULT_DURATION);}参数说明:startX开始变化时X的值,startY开始变化时Y的值,dx X的值变化的最大值,dy Y值变化的最大值public void startScroll(int startX, int startY, int dx, int dy, int duration) {}比上一方法多了一个执行时间的参数;第二个知识点:public boolean computeScrollOffset() {}此方法返回的是一个boolean型的值,这个boolean的只有什么用呢,看方法名我们应该可以猜到,此boolean值就是判断我们调用startScroll 方法执行是否已经结束。特别注意 :======返回true表示还没有结束============ 因为方法名的原因,这里我搞错好多次了此方法的用处可不止这一个,根据官方文档介绍    /**     * Call this when you want to know the new location.  If it returns true,     * the animation is not yet finished.     */大致意思是,调用此方法可以获取新的位置,也就是想要获取当前的X或Y的值吗,就得先调用此方法;那么怎么获取当前的location呢, public final int getCurrX() {        return mCurrX; } public final int getCurrX() {        return mCurrX; } 实战一: 实现图片移动动画效果 package moon.scrolldemo; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.RelativeLayout; import android.widget.Scroller; /**  * Created by moon.zhong on 2015/2/10.  */ public class ScrollLin extends RelativeLayout implements View.OnClickListener{     private Scroller mScroll ;     private View mView ;     private View mButton ;     private boolean isFirst = true ;     public ScrollLin(Context context) {         this(context, null);     }     public ScrollLin(Context context, AttributeSet attrs) {         this(context, attrs, 0);     }     public ScrollLin(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         mScroll = new Scroller(getContext()) ;     }     @Override     protected void onLayout(boolean changed, int l, int t, int r, int b) {         super.onLayout(changed, l, t, r, b);         if(isFirst){             Log.v("zgy", "=========isFirst==========") ;             mView = getChildAt(0) ;             mButton = getChildAt(1) ;             mButton.setOnClickListener(this);         }         isFirst = false ;     }     @Override     public void computeScroll() {         if(mScroll.computeScrollOffset()){             int currentY = mScroll.getCurrY() ;             /*这里让他平移*/             mView.setTranslationY(currentY);             /*currentY的变化范围 0 ~ -mView.getTop() */             Log.v("zgy", "=========mView=========="+currentY) ;             invalidate();         }     }     @Override     public void onClick(View v) {         Log.v("zgy", "=========onClick==========") ;         /*开始执行数据变化*/         mScroll.startScroll(0, 0, 0, -mView.getTop(), 2000);         invalidate();     } }xml的代码:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <moon.scrolldemo.ScrollLin        android:layout_width="match_parent"        android:layout_height="match_parent">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:src="@drawable/ic_launcher"/>        <Button            android:layout_width="100dp"            android:layout_height="50dp"            android:text="开始"            android:layout_centerHorizontal="true"            android:layout_alignParentBottom="true"/>        </moon.scrolldemo.ScrollLin></RelativeLayout>
0 0