1. layout()
这个方法给一个视图分配了大小和位置,调用该方法会使View的位置发生改变,改变的位置根据传入的参数进行确定。
测试代码:
tvMove.layout(tvMove.getLeft() + 5, tvMove.getTop() + 5, tvMove.getRight() + 5, tvMove.getBottom() + 5);
2. offsetLeftAndRight()和offsetTopAndBottom()
offsetLeftAndRight()可以在水平方向上 移动View的位置,offsetTopAndBottom则在垂直方向移动View。
测试代码:
tvMove.offsetLeftAndRight(5);
tvMove.offsetTopAndBottom(5);
3. scrollBy()
scrollBy可以移动View到指定的区域,需要注意的是,scrollBy传入的参数和实际移动的方向是相反的。例如想移动View移动到(50,50)的位置,那么调用方式:scrollBy(-50, -50)。
测试代码:
((View)(tvMove.getParent())).scrollBy(-5, -5);
4. scrollTo()
这个方法也可以把一个View移动到设定的位置,传入的参数一样和实际位置方向相反,它的参考点是(0, 0),即使View移动之后也不会加上当前的偏移量。它和scrollBy()的区别就在这里,相对的参考点不一样,scrollBy()会自动加上移动后的坐标作为新的参考点。
测试代码:
((View)(tvMove.getParent())).scrollTo(-150, -150);
5. Scroller
使用Scroller类也能够实现移动View的功能,上面使用scrollTo()和ScrollBy()也可以实现这样的功能,Scroller类增加了对移动过程的控制,这点和上面的scrollTo()和ScrollBy()是有区别的。
使用Scroller类的步骤:
1. 自定义一个需要移动的View
2. 实例化一个Scroller的对象
3. 重写computeScroll()方法
4. 启动scroll过程,调用startScroll()方法。
测试代码如下:
public class MoveView extends TextView {
private Scroller scroller;
public MoveView(Context context, AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
public void moveTo(int dx, int dy) {
scroller.startScroll(scroller.getFinalX(), scroller.getFinalY(), dx, dy);
invalidate(); // 必须调用invalidate()才,否则不会调用computeScroll()方法。看不到滚动效果
}
public void computeScroll() {
if(scroller.computeScrollOffset()) {
((View)getParent()).scrollTo(scroller.getCurrX(), scroller.getCurrY());
}
invalidate();//一定要调用
super.computeScroll();
}
}
6. setLayoutParams()
这个方法可以设置View视图相关联的布局参数,它可以为View的父视图提供子类如何放置的参数信息。
LinearLayout.LayoutParamsparams = (LinearLayout.LayoutParams)tvMove.getLayoutParams();
params.leftMargin = tvMove.getLeft() + 5;
params.topMargin = tvMove.getTop() + 5;
tvMove.setLayoutParams(params);
最后贴上测试代码:
MoveView.java
public class MoveView extends TextView {
private Scroller scroller;
public MoveView(Context context, AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
public void moveTo(int dx, int dy) {
scroller.startScroll(scroller.getFinalX(), scroller.getFinalY(), dx, dy);
invalidate(); // 必须调用invalidate()才,否则不会调用computeScroll()方法。看不到滚动效果
}
public void computeScroll() {
if(scroller.computeScrollOffset()) {
((View)getParent()).scrollTo(scroller.getCurrX(), scroller.getCurrY());
}
invalidate();//一定要调用
super.computeScroll();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.jy.moveview.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
<com.example.jy.moveview.MoveView
android:id="@+id/tv_move"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ffff0000"
android:gravity="center"
android:text="test move" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_test1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAllCaps="false"
android:text="test1" />
<Button
android:id="@+id/btn_test2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:gravity="center"
android:text="test1" />
<Button
android:id="@+id/btn_test3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:gravity="center"
android:text="test1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_test4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAllCaps="false"
android:text="test4" />
<Button
android:id="@+id/btn_test5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:gravity="center"
android:text="test5" />
<Button
android:id="@+id/btn_test6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:gravity="center"
android:text="test6" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private MoveView tvMove = null;
private Button btnTest1 = null;
private Button btnTest2 = null;
private Button btnTest3 = null;
private Button btnTest4 = null;
private Button btnTest5 = null;
private Button btnTest6 = null;
private Scroller scroller = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMove = (MoveView) findViewById(R.id.tv_move);
btnTest1 = (Button) findViewById(R.id.btn_test1);
btnTest2 = (Button) findViewById(R.id.btn_test2);
btnTest3 = (Button) findViewById(R.id.btn_test3);
btnTest4 = (Button) findViewById(R.id.btn_test4);
btnTest5 = (Button) findViewById(R.id.btn_test5);
btnTest6 = (Button) findViewById(R.id.btn_test6);
btnTest1.setOnClickListener(clickListener);
btnTest2.setOnClickListener(clickListener);
btnTest3.setOnClickListener(clickListener);
btnTest4.setOnClickListener(clickListener);
btnTest5.setOnClickListener(clickListener);
btnTest6.setOnClickListener(clickListener);
scroller = new Scroller(MainActivity.this);
}
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_test1:
test1();
break;
case R.id.btn_test2:
test2();
break;
case R.id.btn_test3:
test3();
break;
case R.id.btn_test4:
test4();
break;
case R.id.btn_test5:
test5();
break;
case R.id.btn_test6:
test6();
break;
}
}
};
private void test1() {
tvMove.layout(tvMove.getLeft() + 5, tvMove.getTop() + 5, tvMove.getRight() + 5, tvMove.getBottom() + 5);
}
private void test2() {
tvMove.offsetLeftAndRight(5);
tvMove.offsetTopAndBottom(5);
}
private void test3() {
((View)(tvMove.getParent())).scrollBy(-5, -5);
}
private void test4() {
((View)(tvMove.getParent())).scrollTo(-150, -150);
}
private void test5() {
tvMove.setScroller(scroller);
tvMove.moveTo(-5, -5);
}
private void test6() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tvMove.getLayoutParams();
params.leftMargin = tvMove.getLeft() + 5;
params.topMargin = tvMove.getTop() + 5;
tvMove.setLayoutParams(params);
}