http://blog.csdn.net/zhbinary/article/details/7432059
android view中有layout(int l, int t, int r, int b) 和offsetTopAndBottom(int offset)和offsetLeftAndRight(int offset) 3个方法,可以用来操作view的位置,下面我们来移动一个view
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/ll" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> <com.test.gesture.TestView android:id="@+id/tv" android:layout_gravity="center" android:layout_width="70dp" android:layout_height="400dp" android:background="@android:color/white" android:text="adfsdfffffffffffffffffffffffffffffffffffff" android:textColor="@android:color/black" /> </LinearLayout>
public class Test_gestureActivity extends Activity { private Button button; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { button.offsetTopAndBottom(10); button.offsetLeftAndRight(10); // button.layout(120, 120, 240, 200); } }); } }
public class TestView extends TextView { private GestureDetector gd; private int scrollingOffset; public TestView(Context context, AttributeSet attrs) { super(context, attrs); gd = new GestureDetector(context, new InnerGestureListener()); } @Override protected void onDraw(Canvas canvas) { canvas.translate(0, scrollingOffset); super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return gd.onTouchEvent(event); } class InnerGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { scrollingOffset += -distanceY; invalidate(); return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return super.onFling(e1, e2, velocityX, velocityY); } } }