Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562275
  • 博文数量: 192
  • 博客积分: 3780
  • 博客等级: 中校
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:11
文章存档

2012年(6)

2011年(160)

2010年(26)

分类: Java

2011-10-28 21:30:36

package cn.edu.lzu.MyScroler;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class MyScroler extends Activity {
public static final String TAG = "MyScroler";
    /** Called when the activity is first created. */
    LinearLayout lay1,lay2,lay;
     private Scroller mScroller;
     private boolean s1,s2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScroller = new Scroller(this);
         lay1 = new LinearLayout(this){
        /**
         * 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
         */
             @Override 
             public void computeScroll() { 
                 if (mScroller.computeScrollOffset()) { 
                Log.i(TAG, "computeScroll_1"+mScroller.getCurrX() +" :"+ mScroller.getCurrY() );
                /**
                 * Set the scrolled position of your view. 
                 * This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.
                 */
                     scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); 
                     /**
                      * Cause an invalidate to happen on a subsequent cycle through the event loop. 
                      * Use this to invalidate the View from a non-UI thread.
                      */
                     postInvalidate(); 
                 } 
             } 
         };
         lay2 = new LinearLayout(this){
             @Override 
             public void computeScroll() { 
                 if (mScroller.computeScrollOffset()) { 
                    // mScrollX = mScroller.getCurrX(); 
                Log.i(TAG, "computeScroll_lay21"+mScroller.getCurrX() +" :"+ mScroller.getCurrY() );
                     scrollTo(mScroller.getCurrX(),mScroller.getCurrY()); 
                     postInvalidate(); 
                 } 
             } 
         };
      /*lay1.setBackgroundColor(this.getResources().getColor(android.R.color.darker_gray));
        lay2.setBackgroundColor(this.getResources().getColor(android.R.color.white));*/
         lay1.setBackgroundResource(R.drawable.wallpaper_galaxy);
         lay2.setBackgroundResource(R.drawable.wallpaper_goldengate);
         
        lay = new LinearLayout(this);
        lay.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        this.setContentView(lay, p0);
        
        LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        p1.weight=1;
        lay.addView(lay1,p1);
        LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     
        p2.weight=1;
        lay.addView(lay2,p2);
        Button tx = new Button(this);
        Button tx2 = new Button(this);
        tx.setText("Button1");  
        tx2.setText("Button2");
        tx.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!s1){
                Log.i(TAG, "computeScroll_2" +mScroller.getCurrX() +" :"+ mScroller.getCurrY());
                    mScroller.startScroll(-310, -100, 310, 100, 10000);
                    Log.i(TAG, "computeScroll_3" +mScroller.getCurrX() +" :"+ mScroller.getCurrY());
                    s1 = true;
                }else{
                    mScroller.startScroll(10, 20, -50, -10,10000);
                    s1 = false;
                }
            }
            
        });
        tx2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!s2){
                    mScroller.startScroll(0, 0, 5, 20,10);
                    s2=true;
                }else{
                    mScroller.startScroll(20, 20, -50, -20,10);
                    s2=false;
                }
            }
        });
        lay1.addView(tx);
        lay2.addView(tx2);
    }
}

如果点击BUTTON1时,mScroller开始滚动,导致LAY1发生滚动,LAY2不变(背景LAY也不便)

如果点击BUTTON2时,mScroller开始滚动,导致LAY2发生滚动,LAY1不变(背景LAY也不便)

通过这个例子看出

/**
* 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 {@link android.widget.Scroller Scroller}
* object.
*/
public void computeScroll()

只有当前LAYOUT中的某个CHILD导致SCROLL发生滚动,才会致使自己的COMPUTESCROLL被调用。

并没有直接的代码指示,哪一个LAYOUT和SCROLLER有联系。

以下理解未考证:

每一个窗口打开时,默认的整个背景LAYOUT长宽与一个虚拟的scroller保持一致,当scroller移动坐标时

layout可以通过计算scroll移动的横向纵向坐标及距离来引发自己的scroll

每一个view都会有scrollto,by方法,通过此方法可以滚动本身view.

原作者:赵新

原文链接:http://blog.csdn.net/G_rrrr/archive/2009/11/24/4864161.aspx

阅读(2409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~