子View按規則自動移動Layout

來源:互聯網
上載者:User

標籤:layout   擴充   

之前看到一個螢幕上有點不斷閃動的,並且移動的介面。當時久萌生一種想法,能不能做一個這樣的選擇介面呢?

實現起來其實比較簡單,寫了一下之後,感覺完全可以做一個可以很好地重用,並且方便擴充移動規則的View。趁著平時時間,就乾脆實現了它,並且粗略寫了幾種移動規則。

ChildAutoMoviLayout實現父類

考慮到平時使用起來比較方便使用的是RelativeLayout,所以用ChildAutoMoviLayout作為RelativeLayout的子類。這樣使用者在使用這個Layout的時候,
可以更加方便地擴充。比如使用Margins。

添加子View方式

另外,因為每個子View可以要求為一致,因為自動移動,基本上是一些差不多形式的View移動。所以我使用Android AbsListView的Adapter作為ChildAutoMoviLayout
的Adapter。ChildAutoMoviLayout通過讀取Adapter來給它自己添加子View 。ChildAutoMoviLayout不通過addView來添加View。這樣使用者只要實現一個
Adapter就可以了。甚至可以直接把ListView的Adapter拿來使用。

移動規則

移動規則,我設計一個介面,使用者只要實現那個介面,並且給ChildAutoMoviLayout就可以了。setMoviInterface。移動規則採用兩個方法

        /**                 * 移動                 * @param view                 * @param height 移動高度                 * @param width 移動寬度                 */            void move(View view,int width,int height);            /**                 * 初始化位置                 * @param view                 * @param height 移動高度                 * @param width 移動寬度                 */            void init(View view,int width,int height);    
動畫實現

我使用的是TimeTask作為一個定時器,然後利用定時器,每隔20毫秒移動一中的標籤(子類別檢視)

    timer.scheduleAtFixedRate(timerTask, 1000, 20);
使用介紹

###xml使用:

            <com.houzhi.childautomovi.view.ChildAutoMoviLayout                android:id="@+id/tagView"                android:layout_below="@+id/tag_container"                android:layout_width="match_parent"                android:layout_height="match_parent">                </com.houzhi.childautomovi.view.ChildAutoMoviLayout>    
java代碼使用樣本:
  1. 從xml中解析出

     //從xml中解析出ChildAutoMoviLayout tagRandomView = (ChildAutoMoviLayout) findViewById(R.id.tagView);    final LinearLayout linear = (LinearLayout) findViewById(R.id.tag_container);    
  2. 設定Adapter和移動策略

    final TagAdapter adapter = new TagAdapter();    tagRandomView.setAdapter(adapter, new BubbleLineUpMoving());    
  3. 監聽標籤(子View)的移動

    tagRandomView.setOnTagClickListener(new ChildAutoMoviLayout.TagClickListener() {                    @Override                    public void onTagClickListener(View view, int position, long id) {                            //TODO click things                    }            });    
  4. 開始動畫

    tagRandomView.startMoving();    

子View按規則自動移動Layout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.