仿OPPO R7 plus中的时钟应用的导航效果

效果图:

仿OPPO R7 plus中的时钟应用的导航效果_第1张图片

效果基本上和oppo上的效果一样,应为图片原因少了一些立体效果。


MainActivity:

package com.example.chl.myapplication;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements View.OnClickListener{
    private ViewPager mViewPager;
    private List<View> mList=new ArrayList<View>();
    private TextView mTabline;
    private int  mScreen1_4;
    private int mCurrentPageIndex;
    private int mAlpha=255;
    private String mText="闹钟";
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initTabLine();
    }

    private void initView() {
        View view1 = LayoutInflater.from(this).inflate(R.layout.tab1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.tab2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.tab3, null);
        View view4 = LayoutInflater.from(this).inflate(R.layout.tab4, null);
        rb1= (RadioButton) findViewById(R.id.button1);
        rb2= (RadioButton) findViewById(R.id.button2);
        rb3= (RadioButton) findViewById(R.id.button3);
        rb4= (RadioButton) findViewById(R.id.button4);
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
        rb4.setOnClickListener(this);


        mList.add(view1);
        mList.add(view2);
        mList.add(view3);
        mList.add(view4);
        mViewPager= (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mList.size();
            }

            @Override
            public int getItemPosition(Object object) {
                return super.getItemPosition(object);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager) container).addView(mList.get(position));
                return mList.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return super.getPageTitle(position);
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return view == o;
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d("caohaidemo", "position:" + position);
                Log.d("caohaidemo", "mCurrentPageIndex:" + mCurrentPageIndex);
                Log.d("caohaidemo", "positionOffset:" + positionOffset);
                FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams) mTabline
                        .getLayoutParams();

                if (mCurrentPageIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * mScreen1_4 + mCurrentPageIndex
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));

                        mText = "闹钟";

                        Log.d("naozhong", "naonaonao");

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);

                        mText = "世界时钟";

                        // mTabline.setTextColor(Color.argb((int) (mAlpha+(positionOffset-0.5)*255), 0, 134, 139));
                    }
                } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));

                        mText = "闹钟";

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);

                        mText = "世界时钟";
                        Log.d("naozhong", "zhongzhong");


                    }

                } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + positionOffset
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));
                        mText = "世界时钟";

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);
                        ;
                        mText = "秒表";
                    }
                } else if (mCurrentPageIndex == 2 && position == 1) // 2->1
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));
                        mText = "世界时钟";

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);
                        mText = "秒表";
                    }
                } else if (mCurrentPageIndex == 2 && position == 2) {//2->3
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + positionOffset
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));
                        mText = "秒表";

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);
                        mText = "倒计时";
                    }
                } else if (mCurrentPageIndex == 3 && position == 2) {//3->2
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                    if (positionOffset > 0 && positionOffset < 0.5) {
                        mAlpha = 255 - ((int) (255 * (positionOffset)));
                        mText = "秒表";

                    } else if (positionOffset > 0.5 && positionOffset < 1) {
                        // mAlpha=(int) (255*positionOffset);
                        mAlpha = (int) (122 + (positionOffset - 0.5) * 255);
                        mText = "倒计时";
                    }
                }
                mTabline.setText(mText);
                mTabline.setTextColor(Color.argb(mAlpha, 0, 134, 139));
                mTabline.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPageIndex = position;
                switch (position) {
                    case 0:
                        mText = "闹钟";
                        break;
                    case 1:
                        mText = "世界时钟";
                        break;
                    case 2:
                        mText = "秒表";
                        break;
                    case 3:
                        mText = "倒计时";
                        break;
                }
                mTabline.setText(mText);
                Log.d("naozhong", "0000:" + position);
                // mTabline.setTextColor(Color.argb(255, 0, 134, 139));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

//设置滑动的tab的长度
 private void initTabLine()
    {
        mTabline = (TextView) findViewById(R.id.text);
        mTabline.setTextColor(Color.argb(255, 0, 134, 139));
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_4 = outMetrics.widthPixels / 4;
        LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_4;
        mTabline.setLayoutParams(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                mViewPager.setCurrentItem(0,true);
                 break;
            case R.id.button2:
                mViewPager.setCurrentItem(1,true);
                 break;
            case R.id.button3:
                mViewPager.setCurrentItem(2,true);
                break;
            case R.id.button4:
                mViewPager.setCurrentItem(3,true);
                break;
            default:
                break;
        }
    }
}


主布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

  <RadioGroup
      android:background="@drawable/tab_bg"
      android:id="@+id/button"
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="45dp">
    <RadioButton
        android:textColor="#000000"
        android:textSize="16sp"
        android:button="@null"
        android:gravity="center"
        android:text="闹钟"
    android:id="@+id/button1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent" />

    <RadioButton

        android:textColor="#000000"
        android:textSize="16sp"
        android:button="@null"
        android:gravity="center"
        android:text="世界时钟"
        android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
      <RadioButton
          android:textColor="#000000"
          android:textSize="16sp"
          android:button="@null"
          android:gravity="center"
          android:text="秒表"
          android:id="@+id/button3"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
      <RadioButton
          android:textColor="#000000"
          android:textSize="16sp"
          android:button="@null"
          android:gravity="center"
          android:text="倒计时"
          android:id="@+id/button4"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
</RadioGroup>
 <TextView
     android:id="@+id/text"
     android:textColor="#000000"
     android:textSize="16sp"
     android:background="#ffffff"
     android:gravity="center"
     android:text="动态"
     android:layout_width="30dp"
     android:layout_height="45dp" />
    <!--<View-->
        <!--android:layout_marginTop="60dp"-->
        <!--android:background="#000000"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="1dp"/>-->
    <android.support.v4.view.ViewPager
        android:background="#ffffff"
        android:id="@+id/viewpager"
        android:layout_marginTop="45dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>
</FrameLayout>

tab背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#DCDCDC"
        android:centerColor="#F8F8FF"
        android:endColor="#ffffff"
        android:angle="270" />
        />

</shape>

4个tab页的布局就不上传了。都是布局里面加上一个TextView

你可能感兴趣的:(android,viewpager,导航,oppo时钟导航页)