view with drag your finger as well as animation

     The custom interior view point of attack has to deal with external events compatible, you can directly set the click event on the outside, the code can go to the warehouse https://github.com/wangsen927/MoveImageView , also can refer to the following code:

/**
 * 如果view随手指拖动时,同时需要点击事件,直接在外部设置即可,内部已解决冲突
 *
 * */
public class MoveImageView extends android.support.v7.widget.AppCompatImageView {


    private int start_x;
    private int start_y;
    long exitTi;
    private int width;
    private int screenHeight;
    private int statusHeight;
    private int navigationHeight;

    public MoveImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MoveImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MoveImageView(Context context) {
        super(context);
        init();
    }

    private void init() {

        width = ScreenUtils.getScreenWidth(getContext());

        screenHeight = ScreenUtils.getScreenHeight(getContext());//获得屏幕高度
        statusHeight = ScreenUtils.getStatusHeight(getContext());//获得状态栏的高度
        navigationHeight = ScreenUtils.getVirtualBarHeigh(getContext());//获取虚拟功能键高度

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                start_x = (int) event.getX();
                start_y = (int) event.getY();
                exitTi = System.currentTimeMillis();//记录按下时间

                break;

            case MotionEvent.ACTION_MOVE:

                int loast_x = (int) event.getX();
                int loast_y = (int) event.getY();

                int px = loast_x - start_x;
                int py = loast_y - start_y;


                float x = getX() + px;
                float y = getY() + py;


                //检测是否到达边缘 左上右下
                //x = x < 0 ? 0 : x > width - getWidth() ? width - getWidth() : x;//暂时去掉x方向的判断

                if (y < 0) {//顶部界限判断,可以根据需求加上标题栏等高度
                    y = 0;
                }

                //底部界限判断,可以根据需求减去底部tap切换栏高度
                if (y > screenHeight - getHeight() - statusHeight) {
                    y = screenHeight - getHeight() - statusHeight;
                }


                setX(x);
                setY(y);

                //this.x = loast_x;
                //this.y = loast_y;

                break;
            case MotionEvent.ACTION_UP:

                int rawX = (int) event.getRawX();

                if (rawX >= width / 2) {
                    animate().setInterpolator(new DecelerateInterpolator())
                            .setDuration(500)
                            .xBy(width - getWidth() - getX())
                            .start();

                } else {
                    ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x", getX(), 0);
                    oa.setInterpolator(new DecelerateInterpolator());
                    oa.setDuration(500);
                    oa.start();
                }

                if ((System.currentTimeMillis() - exitTi) > 200) {// 系统时间和记录的退出时间差大于2秒
                    //只触发滑动事件
                    return true;
                }

                break;
        }

        //触发点击事件
        return super.onTouchEvent(event);
    }


}

After referenced in the layout file, directly available, such as:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.moveimageview.MainActivity">

    <com.moveimageview.MoveImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.iv).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                Toast.makeText(MainActivity.this,"onClickonClick",Toast.LENGTH_SHORT).show();
            }
        });
    }
    
}

Results are as follows:

Published 32 original articles · won praise 54 · views 8031

Guess you like

Origin blog.csdn.net/wangsen927/article/details/104789472