閱讀966 返回首頁    go 阿裏雲 go 技術社區[雲棲]


模仿android網易新聞客戶端左右動畫效果

https://www.cnblogs.com/and_he/archive/2012/03/31/2426248.html

一、摘要

偶然在一個論壇上麵看到有人模擬網易新聞客戶端首頁頂部那個組件效果,一時興起,也來自己動手完整地模擬一個,包括頂部的特效組件和底部的類似於TabHost的組件。下麵就動手一步一步地Coding...
二、效果截圖

三、底部類似TabHost組件切換效果的實現
為了便於大家親自動手實踐,這裏的講解順序就按照開發的順序來講,所以先做這個底部的“TabHost”,然後再具體來實現裏麵的五個頁麵布局。
類似於圖3到圖5三張圖片所示,當點擊“新聞”或者“話題”或者“投票”的時候,有個稍微透明的紅色背景的ImageView做相應的移動。這其實就是給ImageView設置了一個位移動畫,當點擊事件觸發的時候,首先切換點擊後的圖片(有點類似於按下效果的圖片),然後開始移動鋪在上麵的紅色圖片,讓用戶感覺到有移動的過程,增強用戶體驗。
關於這個位移動畫,需要用到TranslateAnimation類,移動的核心代碼也就幾行,因為這個移動功能不但在底部控件上使用,而且在頂部也使用了,所以,為了以後使用方便,我們把它單獨定義在一個類裏麵MoveBg.java

package com.and.netease.utils;

import android.view.View;
import android.view.animation.TranslateAnimation;

public class MoveBg {
    /**
     * 移動方法
     * 
     * @param v
     *            需要移動的View
     * @param startX
     *            起始x坐標
     * @param toX
     *            終止x坐標
     * @param startY
     *            起始y坐標
     * @param toY
     *            終止y坐標
     */
    public static void moveFrontBg(View v, int startX, int toX, int startY, int toY) {
        TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
        anim.setDuration(200);
        anim.setFillAfter(true);
        v.startAnimation(anim);
    }
}

裏麵的各個參數有相應的說明。
然後就來開發這個帶有TabHost功能的組件。根據文檔https://developer.android.com/res ... ello-tabwidget.html說明,在xml中定義TabHost的時候,必須使用TabWidget和FrameLayou兩個組件,而且它們的id也應該是android:和android:,由於係統提供的TabHost界麵不怎麼好看,所以這裏想到自己來定義它,但是這兩個組件是不可以不寫的,這裏,把TabWidget界麵隱藏掉了,取而代之的是RadioGroup組件來實現底部類似於TabHost的控件。具體布局代碼如main.xml

View Code 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:
                android:layout_width="fill_parent"
                android:layout_height="0.0dip"
                android:layout_weight="1.0" >
            </FrameLayout>

            <TabWidget
                android:
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <RelativeLayout
                android:
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <RadioGroup
                    android:
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@drawable/bottombg"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:
                        android:layout_width="wrap_content"
                        android:background="@drawable/tab_selector_news"
                        android:button="@null"
                        android:checked="true" />

                    <RadioButton
                        android:
                        android:layout_width="wrap_content"
                        android:background="@drawable/tab_selector_topic"
                        android:button="@null" />

                    <RadioButton
                        android:
                        android:layout_width="wrap_content"
                        android:background="@drawable/tab_selector_pic"
                        android:button="@null" />

                    <RadioButton
                        android:
                        android:layout_width="wrap_content"
                        android:background="@drawable/tab_selector_follow"
                        android:button="@null" />

                    <RadioButton
                        android:
                        android:layout_width="wrap_content"
                        android:background="@drawable/tab_selector_vote"
                        android:button="@null" />
                </RadioGroup>
            </RelativeLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

注意裏麵的RadioButton組件,當初測試的時候沒有設置android:button="@null",隻設置了background="@drawable/..."屬性(這是一個selector屬性,可以在xml文件中定義一些控件的按下效果,或者獲取焦點等不同狀態下的資源),出現點擊不切換圖片的問題。
對應的selector文件對應如下tab_selector_news.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>

    <item android:drawable="@drawable/current_news_tab" android:state_checked="true"/>
    <item android:drawable="@drawable/back_news_tab" android:state_checked="false"/>

</selector>

其它幾個,隻是替換不同的圖片資源罷了,不再一一列出。這些資源文件放在res目錄下的drawable文件夾下(如果沒有,則新建)
有了布局文件,還需要在Activity中設置一下,為每個TabHost添加具體的Tab頁麵,如下

tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("news").setIndicator("News").setContent(new Intent(this, TabNewsActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("topic").setIndicator("Topic").setContent(new Intent(this, TabTopicActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("picture").setIndicator("Picture").setContent(new Intent(this, TabPicActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("follow").setIndicator("Follow").setContent(new Intent(this, TabFollowActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("vote").setIndicator("Vote").setContent(new Intent(this, TabVoteActivity.class)));

當然,相應的目標Activity自然暫且隨意創建
然後為RadioGroup設置選擇改變事件監聽器,當選擇改變,改變TabHost中當前顯示的Activity頁麵

private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio_news:
                tabHost.setCurrentTabByTag("news");
                break;
            case R.id.radio_topic:
                tabHost.setCurrentTabByTag("topic");
                break;
            case R.id.radio_pic:
                tabHost.setCurrentTabByTag("picture");
                break;
            case R.id.radio_follow:
                tabHost.setCurrentTabByTag("follow");
                break;
            case R.id.radio_vote:
                tabHost.setCurrentTabByTag("vote");
                break;

            default:
                break;
            }
        }
    };

至此就實現了一個自定義的“TabHost”,接下來再添加那個移動的特效
頁麵上的

是一個RelativeLayout布局,我隻是在這個layout上麵添加了一個ImageView,然後當點擊的時候,移動它的位置來實現效果

private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio_news:
                tabHost.setCurrentTabByTag("news");
//                moveFrontBg(img, startLeft, 0, 0, 0);
                MoveBg.moveFrontBg(img, startLeft, 0, 0, 0);
                startLeft = 0;
                break;
            case R.id.radio_topic:
                tabHost.setCurrentTabByTag("topic");
                MoveBg.moveFrontBg(img, startLeft, img.getWidth(), 0, 0);
                startLeft = img.getWidth();
                break;
            case R.id.radio_pic:
                tabHost.setCurrentTabByTag("picture");
                MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0);
                startLeft = img.getWidth() * 2;
                break;
            case R.id.radio_follow:
                tabHost.setCurrentTabByTag("follow");
                MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 3, 0, 0);
                startLeft = img.getWidth() * 3;
                break;
            case R.id.radio_vote:
                tabHost.setCurrentTabByTag("vote");
                MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 4, 0, 0);
                startLeft = img.getWidth() * 4;
                break;

            default:
                break;
            }
        }
    };

此處要記住移動的初始位置和起始位置就行了。Y坐標軸上不變,隻橫向移動。至此,這個功能實現完了

四、頂部按下效果實現

頂部和底部那個自定義控件的實現效果大體是一樣的,唯一不同的就是,這個移動的不再是ImageView,而是一個TextView,在移動完成之後還需要改變這個TextView上的文字,僅此而已,而已文件如下layout_news.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="#990000" >

        <ImageView
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:src="@drawable/netease_top" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/img_netease_top"
            android:text="@string/news_top_left_text"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/duoyun" />
    </RelativeLayout>

    <RelativeLayout
        android:
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:background="@android:color/white"
        android:paddingLeft="10dip"
        android:paddingRight="10dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RelativeLayout
                android:
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_tops" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_sport" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_play" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_finance" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_science" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <TextView
                    android:
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/title_news_category_more" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

應的Activity代碼TabNewsActivity.java

package com.and.netease;

import com.and.netease.utils.MoveBg;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class TabNewsActivity extends Activity {
    RelativeLayout layout;
    TextView tv_front;//需要移動的View

    TextView tv_bar_news;
    TextView tv_bar_sport;
    TextView tv_bar_play;
    TextView tv_bar_finance;
    TextView tv_bar_science;
    TextView tv_bar_more;

    int avg_width = 0;// 用於記錄平均每個標簽的寬度,移動的時候需要

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

        initViews();
    }

    private void initViews() {
        layout = (RelativeLayout) findViewById(R.id.layout_title_bar);

        tv_bar_news = (TextView) findViewById(R.id.tv_title_bar_news);
        tv_bar_sport = (TextView) findViewById(R.id.tv_title_bar_sport);
        tv_bar_play = (TextView) findViewById(R.id.tv_title_bar_play);
        tv_bar_finance = (TextView) findViewById(R.id.tv_title_bar_finance);
        tv_bar_science = (TextView) findViewById(R.id.tv_title_bar_science);
        tv_bar_more = (TextView) findViewById(R.id.tv_title_bar_more);

        tv_bar_news.setOnClickListener(onClickListener);
        tv_bar_sport.setOnClickListener(onClickListener);
        tv_bar_play.setOnClickListener(onClickListener);
        tv_bar_finance.setOnClickListener(onClickListener);
        tv_bar_science.setOnClickListener(onClickListener);
        tv_bar_more.setOnClickListener(onClickListener);

        tv_front = new TextView(this);
        tv_front.setBackgroundResource(R.drawable.slidebar);
        tv_front.setTextColor(Color.WHITE);
        tv_front.setText("頭條");
        tv_front.setGravity(Gravity.CENTER);
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        param.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        layout.addView(tv_front, param);

    }

    private OnClickListener onClickListener = new OnClickListener() {
        int startX;//移動的起始位置

        @Override
        public void onClick(View v) {
            avg_width = findViewById(R.id.layout).getWidth();
            switch (v.getId()) {
            case R.id.tv_title_bar_news:
                MoveBg.moveFrontBg(tv_front, startX, 0, 0, 0);
                startX = 0;
                tv_front.setText(R.string.title_news_category_tops);
                break;
            case R.id.tv_title_bar_sport:
                MoveBg.moveFrontBg(tv_front, startX, avg_width, 0, 0);
                startX = avg_width;
                tv_front.setText(R.string.title_news_category_sport);
                break;
            case R.id.tv_title_bar_play:
                MoveBg.moveFrontBg(tv_front, startX, avg_width * 2, 0, 0);
                startX = avg_width * 2;
                tv_front.setText(R.string.title_news_category_play);
                break;
            case R.id.tv_title_bar_finance:
                MoveBg.moveFrontBg(tv_front, startX, avg_width * 3, 0, 0);
                startX = avg_width * 3;
                tv_front.setText(R.string.title_news_category_finance);
                break;
            case R.id.tv_title_bar_science:
                MoveBg.moveFrontBg(tv_front, startX, avg_width * 4, 0, 0);
                startX = avg_width * 4;
                tv_front.setText(R.string.title_news_category_science);
                break;
            case R.id.tv_title_bar_more:
                MoveBg.moveFrontBg(tv_front, startX, avg_width * 5, 0, 0);
                startX = avg_width * 5;
                tv_front.setText(R.string.title_news_category_more);
                break;

            default:
                break;
            }

        }
    };

}

五、總結

通過這種例子,我個人總結有兩點需要掌握,一個是TranslateAnimation類的使用,另一個就是布局文件的嵌套使用,經驗多了,慢慢就會有感覺了。以上僅代表我個人的一點點想法和總結,還請各位多多指教。


下載地址:

https://download.csdn.net/detail/jdsjlzx/4326508


最後更新:2017-04-02 17:09:25

  上一篇:go struts2實戰:全文搜索引擎
  下一篇:go android 異步加載圖片縮略圖