gpt4 book ai didi

android - 在 Android 中使用按钮滑动调整元素大小

转载 作者:行者123 更新时间:2023-11-30 01:15:48 27 4
gpt4 key购买 nike

我有两个图像布局,当我向上滑动时,底部的一个在增长,上面的一个在缩小。当我向下滑动底部时,一个正在缩小,上面一个正在增长。这是 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>

这段代码运行良好。现在我要做的是。在这两个 ImageView 之间放置一个按钮。当你向上拖动按钮时,上面一个缩小,下面一个放大。向下拖动按钮时,下方一个缩小,上方一个增大。我怎样才能做到这一点?如何将按钮移动集成到此配置中?谢谢。

最佳答案

您只需要创建新的 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);
}
});
}
}

关于android - 在 Android 中使用按钮滑动调整元素大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com