android开发滑动srcollerview右下角按钮根据手势显示和隐藏动画简单动画效果

最近由于项目需要需要做个简单的动画效果

关于在srcollerview滑动时右下角按钮会跟随手势进行右侧缩进隐藏,在停止滑动时在滑动至原位置显示的简单动画效果,

下面是完整代码

MainActivity.java文件

package cn.com.dh;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private ScrollView scrollView;
    private static final String TAG = "MainActivity";
    private boolean isSroll = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.img);
        scrollView = (ScrollView) findViewById(R.id.sroll);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        Toast.makeText(MainActivity.this, "停止", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "onTouch: " + "停止滑动");
                        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "translationX", 300f, 0f).setDuration(1000);
                        objectAnimator1.start();
                        isSroll = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (isSroll) {
                            Toast.makeText(MainActivity.this, "滑动", Toast.LENGTH_SHORT).show();
                            Log.e(TAG, "onTouch: " + "正在滑动");
                            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 300f).setDuration(1000);
                            objectAnimator.start();
                            isSroll = false;
                            break;
                        }
                        break;
                }
                return false;
            }
        });
    }

    public void btn_click_1(View view) {
        Toast.makeText(MainActivity.this, "消失", Toast.LENGTH_SHORT).show();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 300f).setDuration(1000);
        objectAnimator.start();
        //imageView.setVisibility(View.GONE);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                imageView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                imageView.setVisibility(View.GONE);
            }
        });
    }

    public void btn_click_2(View view) {
        imageView.setVisibility(View.VISIBLE);
        Toast.makeText(MainActivity.this, "显示", Toast.LENGTH_SHORT).show();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 300f, 0f).setDuration(1000);
        objectAnimator.start();
    }
}

xml文件就是一个简单的srcollerview。

菜鸟一枚,不喜勿愤。

你可能感兴趣的:(android开发滑动srcollerview右下角按钮根据手势显示和隐藏动画简单动画效果)