【Android】案頭歌詞懸浮效果簡單實現,android懸浮

來源:互聯網
上載者:User

【Android】案頭歌詞懸浮效果簡單實現,android懸浮

在使用"網易雲音樂"的時候,發現有一個顯示"案頭歌詞"的功能,於是就想著自己實現下。查了下資料,是用WindowManage實現的。實現過程中也出現了些問題,看了官方文檔,也算解決了。這裡貼上簡單代碼。

public class MainActivity extends Activity implements View.OnTouchListener{    MyView myView;    WindowManager wm;    WindowManager.LayoutParams layoutParams;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);        //設定TextView的屬性        layoutParams = new WindowManager.LayoutParams();        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;        //這裡是關鍵,使控制項始終在最上方        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;        //這個Gravity也不能少,不然的話,下面"移動歌詞"的時候就會出問題了~ 可以試試[官網文檔有說明]        layoutParams.gravity = Gravity.LEFT|Gravity.TOP;        //建立自訂的TextView        myView = new MyView(this);        myView.setText("Test Touch");        myView.setTextColor(Color.BLACK);        myView.setBackgroundColor(Color.WHITE);        //監聽 OnTouch 事件 為了實現"移動歌詞"功能        myView.setOnTouchListener(this);        wm.addView(myView, layoutParams);    }    @Override    public boolean onTouch(View v, MotionEvent event) {         switch (event.getAction()){            case MotionEvent.ACTION_UP:                //getRawX/Y 是擷取相對於Device的座標位置 注意區別getX/Y[相對於View]                layoutParams.x = (int) event.getRawX();                layoutParams.y = (int) event.getRawY();                //更新"案頭歌詞"的位置                wm.updateViewLayout(myView,layoutParams);                //下面的removeView 可以去掉"案頭歌詞"                //wm.removeView(myView);                break;            case MotionEvent.ACTION_MOVE:                layoutParams.x = (int) event.getRawX();                layoutParams.y = (int) event.getRawY();                wm.updateViewLayout(myView,layoutParams);                break;        }        return false;    }    //繼承 TextView  好吧,貌似有點多此一舉,其實直接用TextView就好    public class MyView extends TextView{        public MyView(Context context) {            super(context);        }    }}

完整代碼:https://github.com/MummyDing/DisplayLyricsonDesktop

【轉載請註明出處】

Author: MummyDing

出處:http://blog.csdn.net/mummyding/article/details/



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.