gpt4 book ai didi

Android float Activity 与滑动关闭

转载 作者:IT老高 更新时间:2023-10-28 22:02:59 25 4
gpt4 key购买 nike

最新的 Facebook 安卓应用有一个非常漂亮的 float 评论窗口。在那里,用户可以关闭向上或向下滑动的窗口,使其真正易于使用。

我想在我的应用中实现类似的行为,但我不知道该怎么做。任何关于如何做的想法或线索将不胜感激。

Facebook 应用的屏幕截图
(抱歉,我截取屏幕截图的 Facebook 应用程序是日文的) Swipe up Swipe down

最佳答案

我编写了一些与这种关闭/调整大小行为相匹配的代码,我不知道这是否可行,但我的代码基于 Activity 类。我要做的第一件事是创建一个 Activity 并给它Transluscent主题以获得具有透明背景的 Activity 。

在我的 manifest.xml 中:

<activity
android:name=".PopupActivity"
android:label="@string/title_activity_popup"
<!-- Use Translucent theme to get transparent activity background
and NoTitleBar to avoid super old style title bar ;) -->
android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>

然后我创建一个简单的布局文件,其中包含一个 TextView (对应于 Facebook 聊天部分)和一个 View (对应于 Facebook“写你的消息”/“发送笑脸”选项​​卡)

我的布局/activity_popup.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base_popup_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="@android:color/darker_gray"
android:layout_marginBottom="124dp">

<TextView
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/black"/>

<View
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_blue_dark"/>

</RelativeLayout>

最后我在 PopupActivity 类中处理触摸和移动事件,我使用 onTouchListener在 onTouch 方法中提供回调。

PopupActivity

public class PopupActivity extends Activity implements View.OnTouchListener{

private RelativeLayout baseLayout;

private int previousFingerPosition = 0;
private int baseLayoutPosition = 0;
private int defaultViewHeight;

private boolean isClosing = false;
private boolean isScrollingUp = false;
private boolean isScrollingDown = false;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup);
baseLayout = (RelativeLayout) findViewById(R.id.base_popup_layout);
baseLayout.setOnTouchListener(this);
}


public boolean onTouch(View view, MotionEvent event) {

// Get finger position on screen
final int Y = (int) event.getRawY();

// Switch on motion event type
switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:
// save default base layout height
defaultViewHeight = baseLayout.getHeight();

// Init finger and view position
previousFingerPosition = Y;
baseLayoutPosition = (int) baseLayout.getY();
break;

case MotionEvent.ACTION_UP:
// If user was doing a scroll up
if(isScrollingUp){
// Reset baselayout position
baseLayout.setY(0);
// We are not in scrolling up mode anymore
isScrollingUp = false;
}

// If user was doing a scroll down
if(isScrollingDown){
// Reset baselayout position
baseLayout.setY(0);
// Reset base layout size
baseLayout.getLayoutParams().height = defaultViewHeight;
baseLayout.requestLayout();
// We are not in scrolling down mode anymore
isScrollingDown = false;
}
break;
case MotionEvent.ACTION_MOVE:
if(!isClosing){
int currentYPosition = (int) baseLayout.getY();

// If we scroll up
if(previousFingerPosition >Y){
// First time android rise an event for "up" move
if(!isScrollingUp){
isScrollingUp = true;
}

// Has user scroll down before -> view is smaller than it's default size -> resize it instead of change it position
if(baseLayout.getHeight()<defaultViewHeight){
baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition);
baseLayout.requestLayout();
}
else {
// Has user scroll enough to "auto close" popup ?
if ((baseLayoutPosition - currentYPosition) > defaultViewHeight / 4) {
closeUpAndDismissDialog(currentYPosition);
return true;
}

//
}
baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition));

}
// If we scroll down
else{

// First time android rise an event for "down" move
if(!isScrollingDown){
isScrollingDown = true;
}

// Has user scroll enough to "auto close" popup ?
if (Math.abs(baseLayoutPosition - currentYPosition) > defaultViewHeight / 2)
{
closeDownAndDismissDialog(currentYPosition);
return true;
}

// Change base layout size and position (must change position because view anchor is top left corner)
baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition));
baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition);
baseLayout.requestLayout();
}

// Update position
previousFingerPosition = Y;
}
break;
}
return true;
}
}

当用户滚动到足以关闭弹出窗口(即动画和完成 Activity )时,会调用两个小方法:

public void closeUpAndDismissDialog(int currentPosition){
isClosing = true;
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(baseLayout, "y", currentPosition, -baseLayout.getHeight());
positionAnimator.setDuration(300);
positionAnimator.addListener(new Animator.AnimatorListener()
{
. . .
@Override
public void onAnimationEnd(Animator animator)
{
finish();
}
. . .
});
positionAnimator.start();
}

public void closeDownAndDismissDialog(int currentPosition){
isClosing = true;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenHeight = size.y;
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(baseLayout, "y", currentPosition, screenHeight+baseLayout.getHeight());
positionAnimator.setDuration(300);
positionAnimator.addListener(new Animator.AnimatorListener()
{
. . .
@Override
public void onAnimationEnd(Animator animator)
{
finish();
}
. . .
});
positionAnimator.start();
}

使用所有这些代码,您应该能够启动全局匹配 Facebook 弹出行为的 PopupActivity。这只是一个草稿类,还有很多工作要做:添加动画、处理关闭参数等等......

截图:

Screenshot of PopupActivity as written above

关于Android float Activity 与滑动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246981/

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