1. 程式人生 > >自定義的可以移動和點選的ImageView

自定義的可以移動和點選的ImageView

1.話不多說直接上程式碼:(this指的是控制元件本身)

@SuppressLint("AppCompatCustomView")
public class ToucherActionIcon extends ImageView{
    private OnMyListener mOnmylistenter = null;
    private int xDelta;//橫座標
    private int yDelta;//縱座標
    private long startTime = 0;//小球,按下去的時間
    private long endTime = 0;//小球,離開的時間
    private boolean isclick;//是否為點選事件

    public ToucherActionIcon(Context context) {
        super(context);
    }
    /**
     * Method name : ToucherActionIcon
     * Specific description :必須要重寫這個構造方法,不然會報錯
     *@param   context
     *@param   attrs
     */
    public ToucherActionIcon(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ToucherActionIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ToucherActionIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    /**
     * Method name : onTouchEvent
     * Specific description :重寫這個方法,實現可以移動控制元件,並解決點選和觸碰我的衝突問題
     *@return boolean
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //觸碰的位置x和y,每次的按下去和移動操作,都會初始化一次按下去的座標
        final int x = (int) event.getRawX();
        final int y = (int) event.getRawY();
        //確定是觸控事件以後
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            //動作為按下
            case MotionEvent.ACTION_DOWN:
                //獲取這個控制元件的左上角的座標
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this
                        .getLayoutParams();
                //得到按下去的點和這個控制元件的左上點的座標x,y的相對距離
                xDelta = x - params.leftMargin;
                yDelta = y - params.topMargin;
                //按下去時候的系統時間
                startTime = System.currentTimeMillis();
                LogUtils.d("開始時間:" + startTime);
                break;
            case MotionEvent.ACTION_MOVE:
                //獲取這個控制元件的寬高
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)this
                        .getLayoutParams();
                //得到按下去的點減原來他們就存在的相對距離,得到左上角的點的座標
                int xDistance = x - xDelta;
                int yDistance = y - yDelta;
                //將其設定為控制元件的左上角的座標
                layoutParams.leftMargin = xDistance;
                layoutParams.topMargin = yDistance;
                //把得到的新的位置設入
                this.setLayoutParams(layoutParams);
                break;
            case MotionEvent.ACTION_UP:// 手指離開螢幕對應事件
                //up時候的系統時間
                endTime = System.currentTimeMillis();
                LogUtils.d("結束時間:" + endTime);
                //當從點選到彈起小於半秒的時候,則判斷為點選,如果超過則不響應點選事件
                if ((endTime - startTime) > 0.1 * 1000L) {
                    isclick = false;
                } else {
                    isclick = true;
                }
                System.out.println("執行順序up");
                if (isclick) {
                    //呼叫介面中的方法
                    mOnmylistenter.myClick(isclick);
            }
                break;
        }
        this.invalidate();
        return true;
    }
    

    /**
     * interface name : OnMyListener
     * Specific description :自定義介面,實現這個介面必須實現介面的方法
     */
    public static interface OnMyListener{
        void myClick(Boolean isclick);
    }
    
    /**
     * Method name : setOnMyClickListener
     * Specific description :利用這個方法,暴露給外面的呼叫者
     */
    public void setOnMyClickListener(OnMyListener listener) {
        this.mOnmylistenter = listener;
    }
    
}

2.使用:

在佈局檔案中:

    <com.echo.quick.utils.ToucherActionIcon
        android:id="@+id/action_icon"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:src="@drawable/ic_english"/>

在Activity中: 

    /**
     * Method name : initball
     * Specific description :小球的控制元件初始化
     */
    private void initball(){
        toucherActionIcon = (ToucherActionIcon) findViewById(R.id.action_icon);
        toucherActionIcon.setX(900);
        toucherActionIcon.setY(20);
        //移動小球的控制元件
        toucherActionIcon = new ToucherActionIcon(this);
        toucherActionIcon.setOnMyClickListener(new ToucherActionIcon.OnMyListener() {
            @Override
            public void myClick(Boolean isclick) {
                if (isclick){
                    Intent intent = new Intent(ReadActivity.this, ReadingTranslateActivity.class);
                    startActivity(intent);
                }
            }
        });

    }