我有两个图像布局,当我向上滑动时,底部正在增长,一个正在缩小。当我向下滑动时,一个正在缩小,一个正在增长。这是Java代码:
public class MainActivity extends AppCompatActivity{
GestureDetectorCompat detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int minH = 350;
final LinearLayout root = (LinearLayout) findViewById(R.id.root);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if ( iv == null || iv2 == null ) {
return true;
}
distanceY*=-1;
int height = iv.getHeight ( ) + iv2.getHeight ( );
ViewGroup.LayoutParams layoutParams = iv.getLayoutParams ( );
if ( ( int ) distanceY + layoutParams.height < minH )
layoutParams.height = minH;
else if ( ( int ) distanceY + layoutParams.height > height - minH )
layoutParams.height = height - minH;
else
layoutParams.height = ( int ) distanceY + layoutParams.height;
ViewGroup.LayoutParams layoutParams2 = iv2.getLayoutParams ( );
layoutParams2.height = height - layoutParams.height;
iv.setLayoutParams ( layoutParams );
iv2.setLayoutParams ( layoutParams2 );
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
}
这是XML:
<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"
tools:context=".MainActivity"
android:id="@+id/root"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/as" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="@id/imageView"
android:scaleType="fitXY"
android:id="@+id/imageView2"
android:src="@drawable/as" />
</LinearLayout>
此代码效果很好。现在我想做的是。在这两个图像视图之间放置一个按钮。当你向上拖动按钮时,上面的一个缩小,一个下面增长。当你向下拖动按钮时,一个缩小,一个缩小,一个增长。我怎样才能做到这一点?如何将按钮移动集成到此配置中?感谢。
答案 0 :(得分:1)
您只需要创建新的ImageView
,使其可点击以便接收触摸事件并将onTouchListener
设置为:
<强> activity_main.xml中强>
<LinearLayout android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/as"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/as"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/imageView"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/as"/>
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
GestureDetectorCompat detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int minH = 350;
final LinearLayout root = (LinearLayout) findViewById(R.id.root);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
final ImageView iv3 = (ImageView) findViewById(R.id.imageView3);
detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if ( iv == null || iv3 == null ) {
return true;
}
distanceY*=-1;
int height = iv.getHeight ( ) + iv3.getHeight ( );
ViewGroup.LayoutParams layoutParams = iv.getLayoutParams ( );
if ( ( int ) distanceY + layoutParams.height < minH )
layoutParams.height = minH;
else if ( ( int ) distanceY + layoutParams.height > height - minH )
layoutParams.height = height - minH;
else
layoutParams.height = ( int ) distanceY + layoutParams.height;
ViewGroup.LayoutParams layoutParams2 = iv3.getLayoutParams ( );
layoutParams2.height = height - layoutParams.height;
iv.setLayoutParams ( layoutParams );
iv3.setLayoutParams ( layoutParams2 );
return true;
}
});
iv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return detector.onTouchEvent(motionEvent);
}
});
}
}