天天看點

安卓開發啟用線程動态顯示和隐藏懸浮控件(ImageButton),點選回到頁面頂部

需求分析:如下圖
安卓開發啟用線程動态顯示和隐藏懸浮控件(ImageButton),點選回到頁面頂部

讓上圖中綠色向上箭頭在點選或滑動螢幕一秒後顯示,顯示3s後自動隐藏,再次點選或者滑動1s後再次顯示,以此類推,并且點選這個箭頭的時候要回到頁面頂部

實作過程:

acvitity根部局中添加ImageButton控件,預設設定是不顯示的,acvitity根部局用ScrollView包裹,確定頁面是可以滑動的

<ImageButton
            android:id="@+id/IB_top"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="80dp"
            android:layout_marginRight="@dimen/margin_15dp"
            android:background="@drawable/indicator_blue"
            android:scaleType="centerInside"
            android:src="@drawable/topback"
            android:visibility="gone" />
           
在Java代碼中找到并執行個體化ImageButton和ScrollView,添加點選事件,實作點選按鈕傳回頂部的功能
IB_top= (ImageButton) findViewById(R.id.IB_top);
        sv= (ScrollView) findViewById(R.id.sv);
        IB_top.setOnClickListener(this);
、、、
  switch (v.getId()) {
            case R.id.IB_top://傳回頂部按鈕
                sv.post(new Runnable() {
                    @Override
                    public void run() {
                        sv.fullScroll(ScrollView.FOCUS_UP);
                    }
                });
                break;
           

此處也可以在onResume方法中直接先執行一次,進來時候確定頁面在頂部

下邊就是控制顯示和隐藏控件的方法,重寫dispatchTouchEvent方法,用線程實作

private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_WHAT:
                if (countTime > ) {
                    countTime--;
                    if (countTime==){
                        IB_top.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (timer != null) {
                        timer.cancel();
                        timer = null;
                        countTime=;
                        IB_top.setVisibility(View.GONE);
                    }
                }
                break;
            case :
                if (countTime > ) {
                    countTime--;
                } else {
                    if (timer != null) {
                        timer.cancel();
                        timer = null;
                        countTime=;
                        IB_top.setVisibility(View.GONE);
                    }
                }
                break;
            default:
                break;
        }
    }
};

  @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(MSG_WHAT);
                }
            }, , );
        } else {
            timer.cancel();
            timer = null;
            countTime=;
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(MSG_WHAT);
                }
            }, , );//延時啟動0.8s,每隔一秒發送一次,螢幕擷取焦點0.8s後顯示控件
//            IB_top.setVisibility(View.VISIBLE);
//            return true;
        }
//        timer.schedule(task,1000,1000);
        return super.dispatchTouchEvent(ev);

    }
           
至此就實作了懸浮按鈕的動态顯示和隐藏,以及點選回到頂部的功能