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);
}
}