Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 像Pinterest或Tumblr一样向后滑动_Android_Swipe - Fatal编程技术网

Android 像Pinterest或Tumblr一样向后滑动

Android 像Pinterest或Tumblr一样向后滑动,android,swipe,Android,Swipe,有人知道Pinterest或Tumblr是如何实现“刷回”方法的吗 i、 e.在Pinterest上,您可以单击新闻提要上的帖子。然后启动DetailActivity,并显示所选帖子的详细信息。然后,您可以按“上一步”按钮返回新闻提要活动,也可以向左滑动(详细信息活动)返回新闻提要活动 视频: 通常我会使用overridePendingTransition(),但是overridePendingTransition()会使用动画(资源ID如R.anim.foo)。Pinterest和Tumblr

有人知道Pinterest或Tumblr是如何实现“刷回”方法的吗

i、 e.在Pinterest上,您可以单击新闻提要上的帖子。然后启动
DetailActivity
,并显示所选帖子的详细信息。然后,您可以按“上一步”按钮返回新闻提要活动,也可以向左滑动(详细信息活动)返回新闻提要活动

视频:

通常我会使用
overridePendingTransition()
,但是
overridePendingTransition()
会使用动画(资源ID如
R.anim.foo
)。Pinterest和Tumblr仅在用户执行滑动手势时启动动画。根据手指的移动,它们还支持某种“逐帧动画”。因此,它们跟踪手指移动的距离,并将过渡设置为相应的百分比值

我知道如何使用带有
FragmentTransaction
的“RealJava”动画/动画集对象来为片段替换设置动画。对于片段,我必须重写
onCreateAnimator()
,但我不知道如何在活动中实现类似的内容。活动是否有
onCreateAnimator()
(或类似的东西)?也不知道如何刷行为,因为它不是现在开始动画,而是一步一步地改变窗口/活动/片段或其他属性

有什么建议吗

编辑: 我在youtube上找到了pinterest应用程序的视频: 这就是我想要实现的

我猜Pinterest正在使用片段和
onCreateAnimator()
来实现“向后滑动”。 因为我的应用程序已经在一个活动中包含了Fragment和ChildFragments,所以如果我能为活动实现这些,对我来说会容易得多


再说一次:我知道如何检测刷卡手势,这不是我想要的。观看youtube视频:


更新: 我创建了一个小库,它的行为与Pinterest或Tumblrs实现不完全相同,但是对于我的应用程序来说,这似乎是一个很好的解决方案:

我建议您执行以下操作:

首先检测用户在设备中所做的手势。你可以参考

我不会从上面的链接复制相关代码,因为我相信这是公认的答案

其次,你可以用这种方法

public void onSwipeLeft() {
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
按照建议执行以下操作:

他们在那里谈论用动画完成一项活动

Look into doing it through a theme. You can define enter exit animations for activities or the entire application

希望这对您有所帮助

我建议您执行以下操作:

首先检测用户在设备中所做的手势。你可以参考

我不会从上面的链接复制相关代码,因为我相信这是公认的答案

其次,你可以用这种方法

public void onSwipeLeft() {
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
按照建议执行以下操作:

他们在那里谈论用动画完成一项活动

Look into doing it through a theme. You can define enter exit animations for activities or the entire application

希望这对你有所帮助

我能在15分钟内做到这一点,这对一个开始来说还不错。如果你花一些时间,你也许能够优化它

package mobi.sherif.activitydrag;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout.LayoutParams;

public class MainActivity extends Activity {
    private static final double PERCENT_OF_SCROLL_OF_ACTIVITY_TO_FINISH = 0.3;
    View mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(mView);
    }

    private boolean isDragging = false;
    int startX;
    int currentX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.v("sherif", isDragging?"YES":"NO" + ": " + event.getX());
        if(!isDragging) {
            if(event.getAction() == MotionEvent.ACTION_DOWN && event.getX()<24) {
                isDragging = true;
                startX = (int) event.getX();
                currentX = 0;
                return true;
            }
            return super.onTouchEvent(event);
        }
        switch(event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            currentX = (int) event.getX() - startX;
            LayoutParams params = (LayoutParams) mView.getLayoutParams();
            params.leftMargin = currentX;
            params.rightMargin = -1 * currentX;
            mView.requestLayout();
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            isDragging = false;
            double currentPercent1 = (double) currentX / mView.getWidth();
            float currentPercent = (float) currentPercent1;
            if(currentX > PERCENT_OF_SCROLL_OF_ACTIVITY_TO_FINISH * mView.getWidth()) {
                AnimationSet animation = new AnimationSet(false);
                Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f - currentPercent, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                anim.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
                anim.setInterpolator(new LinearInterpolator());
                anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
                animation.addAnimation(anim);
                anim = new AlphaAnimation(1.0f, 0.5f);
                anim.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
                anim.setInterpolator(new LinearInterpolator());
                anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
                animation.addAnimation(anim);
                animation.setFillAfter(true);
                animation.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {}
                    @Override
                    public void onAnimationRepeat(Animation animation) {}
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        finish();
                    }
                });
                mView.startAnimation(animation);
            }
            else {
                AnimationSet animation = new AnimationSet(false);
                Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f -1 * currentPercent, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                anim.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
                anim.setInterpolator(new LinearInterpolator());
                anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
                animation.addAnimation(anim);
                animation.setFillAfter(true);
                animation.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {}
                    @Override
                    public void onAnimationRepeat(Animation animation) {}
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        LayoutParams params = (LayoutParams) mView.getLayoutParams();
                        params.leftMargin = 0;
                        params.rightMargin = 0;
                        mView.requestLayout();
                        mView.clearAnimation();
                    }
                });
                mView.startAnimation(animation);
            }
            break;
        }
        return true;

    }
}
包mobi.sherif.activitydrag;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.animation.AlphaAnimation;
导入android.view.animation.animation;
导入android.view.animation.animation.AnimationListener;
导入android.view.animation.AnimationSet;
导入android.view.animation.AnimationUtils;
导入android.view.animation.LinearInterpolator;
导入android.view.animation.TranslateAnimation;
导入android.widget.FrameLayout.LayoutParams;
公共类MainActivity扩展了活动{
私有静态最终双百分比\u滚动\u活动\u到\u完成=0.3;
视图视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mView=LayoutInflater.from(this).充气(R.layout.activity\u main,空);
setContentView(mView);
}
私有布尔IsDraging=false;
int startX;
int-currentX;
@凌驾
公共布尔onTouchEvent(运动事件){
Log.v(“sherif”,isDraging?“YES”:“NO”+:“+event.getX());
如果(!IsDraging){
如果(event.getAction()==MotionEvent.ACTION\u DOWN&&event.getX()百分比\u滚动\u活动\u到\u完成*mView.getWidth()){
AnimationSet animation=新的AnimationSet(false);
Animation anim=新的TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,1.0f-currentPercent,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f);
setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
anim.setInterpolator(新的LinearInterpolator());
anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
动画。添加动画(动画);
动画=新的AlphaAnimation(1.0f,0.5f);
setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
anim.setInterpolator(新的LinearInterpolator());
anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
动画。添加动画(动画);
animation.setFillAfter(true);
setAnimationListener(新的AnimationListener(){
@凌驾
onAnimationStart(动画){}上的公共无效
@凌驾
公共无效onAnimationRepeat(动画){}
@凌驾
onAnimationEnd上的公共无效(动画){
完成();
}
});
mView.startAnimation(动画);
}
否则{
动画