732003684

导航

viewpager切换时底下的背景图标动画切换

一:布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/applyHome"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg2"
    android:orientation="vertical">

    <RelativeLayout 
        android:id="@+id/rlApplyBottom"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bottombackground"
        >
         <ImageView
            android:id="@+id/ivTabNow"
            android:layout_width="75dp"
            android:layout_height="fill_parent"
            android:layout_centerVertical="true"
            android:background="#33333333" />
         
          <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            orientation="horizontal" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/ivHoliday"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:src="@drawable/apply_holiday1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="假单"
                    android:textColor="#ff000000"
                    android:textSize="12sp" />
            </LinearLayout>
            
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/ivOut"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:src="@drawable/apply_out1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="外出"
                    android:textColor="#ff000000"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/ivOverTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:src="@drawable/apply_extrawork1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="加班"
                    android:textColor="#ff000000"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/ivUse"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:src="@drawable/apply_use1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="领用"
                    android:textColor="#ff000000"
                    android:textSize="12sp" />
            </LinearLayout>
            
            
            
            </LinearLayout>
        
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/rlApplyBottom"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/vpApply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
        </android.support.v4.view.ViewPager>
    </LinearLayout>

</RelativeLayout>

二。MainActivity

package com.ct.viewpaper;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends ActivityGroup {
    /** Called when the activity is first created. */
    private ViewPager mViewPager;
    private ImageView ivTabNow;// 动画图片
    private ImageView mTab1, mTab2, mTab3, mTab4;
    private int zero = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int one;// 单个水平动画位移
    private int two;
    private int three;
    public static MainActivity instance;
    private LocalActivityManager mLocalActivityManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocalActivityManager = getLocalActivityManager();
        this.instance = this;
        initView();
        registerListener();
        
    }
    
    private void initView(){
        mViewPager = (ViewPager) findViewById(R.id.vpApply);

        ivTabNow = (ImageView) findViewById(R.id.ivTabNow);
        mTab1 = (ImageView) findViewById(R.id.ivHoliday);
        mTab2 = (ImageView) findViewById(R.id.ivOut);
        mTab3 = (ImageView) findViewById(R.id.ivOverTime);
        mTab4 = (ImageView) findViewById(R.id.ivUse);
        
        int ivTabWidth = getTabWidth(ivTabNow);
        one = zero + ivTabWidth;
        two = one + ivTabWidth;
        three = two + ivTabWidth;
        initMyViewPager();
    }
    
    private int getTabWidth(View child){
        ViewGroup.LayoutParams p = child.getLayoutParams();        
        if (p == null) {            
            System.out.println("new layoutParams p");
            p = new ViewGroup.LayoutParams(                    
                    ViewGroup.LayoutParams.FILL_PARENT,                    
                    ViewGroup.LayoutParams.WRAP_CONTENT);        
            
        }          
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0,                
                0 + 0, p.width);  
        
        int lpHeight = p.height;        
        int childHeightSpec;         
        if (lpHeight > 0) {            
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);       
            
        } else {           
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);        
        }          
        child.measure(childWidthSpec, childHeightSpec);         
        return child.getMeasuredWidth();
    }
    
    private void initMyViewPager(){
        // 将要分页显示的View装入数组中
                View view1 = activityToView(this, new Intent(
                        MainActivity.this, HolidayApplyActivity.class));
                View view2 = activityToView(this, new Intent(
                        MainActivity.this, OutApplyActivity.class));
                View view3 = activityToView(this, new Intent(
                        MainActivity.this, OverTimeApplyActivity.class));
                View view4 = activityToView(this, new Intent(
                        MainActivity.this, UseApplyActivity.class));
                final ArrayList<View> views = new ArrayList<View>();
                views.add(view1);
                views.add(view2);
                views.add(view3);
                views.add(view4);

                // 填充ViewPager的数据适配器
                PagerAdapter mPagerAdapter = new PagerAdapter() {

                    @Override
                    public boolean isViewFromObject(View arg0, Object arg1) {
                        return arg0 == arg1;
                    }

                    @Override
                    public int getCount() {
                        return views.size();
                    }

                    @Override
                    public void destroyItem(View container, int position, Object object) {
                        ((ViewPager) container).removeView(views.get(position));
                    }

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

                mViewPager.setAdapter(mPagerAdapter);
    }
    
    private View activityToView(Context context,Intent intent){
        final Window w = mLocalActivityManager.startActivity("id", intent);
        final View wd = w != null ? w.getDecorView() : null;
        if (wd != null) {
            wd.setVisibility(View.VISIBLE);
            wd.setFocusableInTouchMode(true);
            ((ViewGroup) wd)
                    .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        }
        return wd;
    }
    
    private void registerListener(){
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mTab1.setOnClickListener(new MyOnClickListener(0));
        mTab2.setOnClickListener(new MyOnClickListener(1));
        mTab3.setOnClickListener(new MyOnClickListener(2));
        mTab4.setOnClickListener(new MyOnClickListener(3));
    }
    
    public class MyOnClickListener implements View.OnClickListener{

        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mViewPager.setCurrentItem(index);// viewPager设置当前展示的page页
        }
        
    }
    
    public class MyOnPageChangeListener implements OnPageChangeListener{

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            Animation animation = null;
            switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                    // mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                    // mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
                } else if (currIndex == 3) {
                    animation = new TranslateAnimation(three, 0, 0, 0);
                    // mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
                }
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(zero, one, 0, 0);
                    // mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                    // mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
                } else if (currIndex == 3) {
                    animation = new TranslateAnimation(three, one, 0, 0);
                    // mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
                }
                break;
            case 2:
                // mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_pressed));
                if (currIndex == 0) {
                    animation = new TranslateAnimation(zero, two, 0, 0);
                    // mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(one, two, 0, 0);
                    // mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
                } else if (currIndex == 3) {
                    animation = new TranslateAnimation(three, two, 0, 0);
                    // mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
                }
                break;
            case 3:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(zero, three, 0, 0);
                    // mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(one, three, 0, 0);
                    // mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, three, 0, 0);
                    // mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
                }
                break;

            default:
                break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(150);
            ivTabNow.startAnimation(animation);
            
        }
        
    }
}

 

posted on 2013-01-07 17:12  732003684  阅读(394)  评论(0)    收藏  举报