实现页面切换(动画效果实现,不用ViewPager)
来源:互联网 发布:数据仓库软件有哪些 编辑:程序博客网 时间:2024/05/24 05:48
源码地址 http://download.csdn.net/detail/u013210620/8791687
先看主页面布局activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d1d1d1" android:orientation="vertical" > <RelativeLayout android:id="@+id/list_layout_visible" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginLeft="0dp" > </RelativeLayout> <!-- tab三选项 --> <LinearLayout android:id="@+id/mainTab" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@drawable/bottom_bg" android:orientation="horizontal" > <ImageView android:id="@+id/iv_tab_communication" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/bottom_sms_p" /> <ImageView android:id="@+id/iv_tab_contacts" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/bottom_contacts_p" /> <ImageView android:id="@+id/iv_tab_dail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/bottom_dial_p" /> <ImageView android:id="@+id/iv_tab_temp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/bottom_dial_p" android:visibility="gone" /> </LinearLayout></RelativeLayout>
再看代码块(有注释)
package com.example.commonpager;import android.animation.Animator;import android.animation.AnimatorListenerAdapter;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class MainActivity extends Activity implements OnClickListener {/** 内容区域 */private RelativeLayout list_layout_visible;/** Tab4键 */private LinearLayout mainTab;private ImageView iv_tab_communication, iv_tab_contacts, iv_tab_dail,iv_tab_temp;/** 当前的Tab键 */private ImageView middleTab;/**页面加载器*/private LayoutInflater mLayoutInflater;/**初始化布局view*/private View mBaseLayoutView;private boolean isStartAnimation = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initView();}private void initView() {mLayoutInflater = LayoutInflater.from(this);mBaseLayoutView = mLayoutInflater.inflate(R.layout.activity_main, null);// 获取list_layout_visible = (RelativeLayout) mBaseLayoutView.findViewById(R.id.list_layout_visible);mainTab = (LinearLayout) mBaseLayoutView.findViewById(R.id.mainTab);iv_tab_communication = (ImageView) mBaseLayoutView.findViewById(R.id.iv_tab_communication);iv_tab_contacts = (ImageView) mBaseLayoutView.findViewById(R.id.iv_tab_contacts);iv_tab_dail = (ImageView) mBaseLayoutView.findViewById(R.id.iv_tab_dail);iv_tab_temp = (ImageView) mBaseLayoutView.findViewById(R.id.iv_tab_temp);/**动态加载布局文件*/mLayoutInflater.inflate(R.layout.content_contacts, list_layout_visible);//展现页面setContentView(mBaseLayoutView);//设置监听事件iv_tab_communication.setOnClickListener(this);iv_tab_contacts.setOnClickListener(this);iv_tab_dail.setOnClickListener(this);//初始化当前按键是contact按键middleTab = iv_tab_contacts;}@Overridepublic void onClick(View v) {switch (v.getId()) {/***思路同iv_tab_contacts*/case R.id.iv_tab_communication:if (middleTab == iv_tab_communication) {return;}list_layout_visible.removeAllViews();mLayoutInflater.inflate(R.layout.content_msg, list_layout_visible);startAnimation(iv_tab_communication);break;case R.id.iv_tab_contacts://初始化点击通讯录按键,不做处理事件,直接返回if (middleTab == iv_tab_contacts) {return;}//如果加载过之前的消息或者拨号界面,先removeall界面,然后在加载通讯录界面list_layout_visible.removeAllViews();mLayoutInflater.inflate(R.layout.content_contacts,list_layout_visible);//加载通讯录界面时候,启动动画效果startAnimation(iv_tab_contacts);break;/***思路同iv_tab_contacts*/case R.id.iv_tab_dail:if (middleTab == iv_tab_dail) {return;}list_layout_visible.removeAllViews();mLayoutInflater.inflate(R.layout.content_dail, list_layout_visible);startAnimation(iv_tab_dail);break;default:break;}}private void startAnimation(final ImageView clickTab) {ImageView otherTab = null;float otherTab_start, otherTab_end;float tempTab_start, tempTab_end;// 消息不在中间,没点击信息if (iv_tab_communication != clickTab&& iv_tab_communication != middleTab) {otherTab = iv_tab_communication;}if (iv_tab_contacts != clickTab && iv_tab_contacts != middleTab) {otherTab = iv_tab_contacts;}if (iv_tab_dail != clickTab && iv_tab_dail != middleTab) {otherTab = iv_tab_dail;}setbottomView();iv_tab_temp.setBackground(null);iv_tab_temp.setBackground(otherTab.getBackground());if (otherTab.getX() > middleTab.getX()) {otherTab_start = clickTab.getX()- (otherTab.getX() - middleTab.getX());otherTab_end = clickTab.getX();tempTab_start = otherTab.getX();tempTab_end = otherTab.getX()+ (otherTab.getX() - middleTab.getX());} else {otherTab_start = clickTab.getX()+ (clickTab.getX() - middleTab.getX());otherTab_end = clickTab.getX();tempTab_start = otherTab.getX();tempTab_end = otherTab.getX()- (clickTab.getX() - middleTab.getX());}AnimatorSet set = new AnimatorSet();set.playTogether(ObjectAnimator.ofFloat(clickTab, "x", clickTab.getX(),middleTab.getX()), ObjectAnimator.ofFloat(middleTab, "x",middleTab.getX(), otherTab.getX()), ObjectAnimator.ofFloat(iv_tab_temp, "x", tempTab_start, tempTab_end), ObjectAnimator.ofFloat(otherTab, "x", otherTab_start, otherTab_end));set.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator anim) {}@Overridepublic void onAnimationStart(Animator anim) {middleTab = clickTab;}});set.setDuration(1 * 600).start();}private void setbottomView() {DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);int width = metrics.widthPixels;// 屏幕的宽dpLinearLayout.LayoutParams para;para = (LinearLayout.LayoutParams) iv_tab_communication.getLayoutParams();para.width = width / 3;para.weight = 0;iv_tab_communication.setLayoutParams(para);para = (LinearLayout.LayoutParams) iv_tab_contacts.getLayoutParams();para.width = width / 3;para.weight = 0;iv_tab_contacts.setLayoutParams(para);para = (LinearLayout.LayoutParams) iv_tab_dail.getLayoutParams();para.width = width / 3;para.weight = 0;iv_tab_dail.setLayoutParams(para);para = (LinearLayout.LayoutParams) iv_tab_temp.getLayoutParams();para.width = width / 3;para.weight = 0;iv_tab_temp.setVisibility(View.VISIBLE);}}
然后是3个Tab键对应的测试页面
content_contacts.xml
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv_tip_no_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="14dp" android:shadowDx="0" android:shadowDy="2" android:shadowRadius="2" android:text="contact!!!" android:textColor="#888" android:textSize="20dp" android:typeface="sans" />
content_msg.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_tip_no_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="14dp" android:shadowDx="0" android:shadowDy="2" android:shadowRadius="2" android:text="msg!!!" android:textColor="#888" android:textSize="20dp" android:typeface="sans" /></RelativeLayout>content_dail.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_tip_no_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="14dp" android:shadowDx="0" android:shadowDy="2" android:shadowRadius="2" android:text="msg!!!" android:textColor="#888" android:textSize="20dp" android:typeface="sans" /></RelativeLayout>
0 1
- 实现页面切换(动画效果实现,不用ViewPager)
- ViewPager实现多页面滑动切换和动画效果
- android用ViewPager实现带有动画效果的页面切换
- ViewPager实现滑屏切换页面及动画效果(仿优酷客户端)
- Android 中ViewPager 实现动画效果切换
- android ViewPager实现页面左右切换效果
- viewpager深度页面切换效果实现
- ViewFlipper实现ViewPager的页面切换效果
- ViewPager和RadioGroup实现页面切换效果
- RadioButton和ViewPager实现页面切换效果
- Android开发之ViewPager实现多页面切换及动画效果(仿Android的Launcher效果)
- ViewPager 切换动画实现
- ViewPager实现页面切换
- ViewPager实现页面切换
- ViewPager实现页面切换
- ViewPager实现页面切换
- 不用ViewPager和Fragment实现滑动页面的效果
- 实现Viewpager切换界面导航栏水波纹动画效果
- ApplicationId与PackageName的区别
- 分享一下我的vim配置
- androidの亮屏,灭屏,解锁广播使用
- 你还在用if else吗?
- 倒序打印一个单链表
- 实现页面切换(动画效果实现,不用ViewPager)
- 【Linux】Makefile详解
- DropdownList-下拉菜单
- NuPIC:一个开源的人工智能平台
- php 事务
- eclipse查看Android系统源代码
- Linux下*.tar.gz文件解压缩命令
- 【Unity】Unity Mesh 网格合并
- 【Facebook的UI开发框架React入门之四】index.ios.js解读(iOS平台)-goodmao