标签:android
废话不多说,直接上思路---
1:试想一个界面,父布局是LinearLayout,竖直方向排列,然后里面添加两个View,如果我点击第一个View,直接调用第二个View的scrollTo或者scrollBy方法,第二个View会移动吗?给出代码和布局文件----
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff00">
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button1"
android:background="#ff0000"/>
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button2"
android:background="#0000ff"/>
</LinearLayout>public class ActivityText extends Activity implements View.OnClickListener {
private View button1;
private View button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button1) {
// button2.scrollBy(-30, -30);
button2.scrollTo(30, 30);
}
}
}我就开始想了,猜测--是不是必须移动移动View的父容器,它里面的孩子才会移动?好,我就进行了下面的第二个实验--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff00"
android:id="@+id/ll">
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button1"
android:background="#ff0000"/>
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button2"
android:background="#0000ff"/>
</LinearLayout>public class ActivityText extends Activity implements View.OnClickListener {
private View button1;
private View button2;
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
ll = (LinearLayout) findViewById(R.id.ll);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button1) {
ll.scrollBy(-30, -30);
}
}
}查看ViewGroup的dispatchDraw()方法,发现一些蛛丝马迹----
int saveCount = 0;
2777 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
2778 if (clipToPadding) {
2779 saveCount = canvas.save();
2780 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
2781 mScrollX + mRight - mLeft - mPaddingRight,
2782 mScrollY + mBottom - mTop - mPaddingBottom);
2783
2784 }布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff00"
android:id="@+id/ll">
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button1"
android:background="#ff0000"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:id="@+id/fl">
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/button2"
android:background="#0000ff"/>
</FrameLayout>
</LinearLayout>public class ActivityText extends Activity implements View.OnClickListener {
private View button1;
private View button2;
private LinearLayout ll;
private FrameLayout fl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
ll = (LinearLayout) findViewById(R.id.ll);
fl = (FrameLayout) findViewById(R.id.fl);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button1) {
fl.scrollBy(-30, -30);
}
}
}读者可以自己实验以下,看一下效果---
标签:android
原文地址:http://blog.csdn.net/ly985557461/article/details/44957749