网上看到viewpager的多view动画切换,模仿制作了一个  学习到了。

先看效果图:

    

先看主类的layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:background="#222222" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView android:id="@+id/textView1"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="50dp"android:background="#999999"android:gravity="center"android:text="页面1"android:textColor="#222222"/><TextView android:id="@+id/textView2"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="50dp"android:background="#999999"android:gravity="center"android:text="页面2"android:textColor="#222222"/><TextViewandroid:id="@+id/textView3"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="50dp"android:background="#999999"android:gravity="center"android:text="页面3"android:textColor="#222222"/></LinearLayout><ImageView android:id="@+id/cursor"android:layout_width="fill_parent"android:layout_height="wrap_content"android:scaleType="matrix"android:src="@drawable/cursor"android:background="#222222"/><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager"android:layout_width="fill_parent"android:layout_height="fill_parent"/></LinearLayout>

viewPager需要一个pagerAdapter的子类

package com.example.myview;import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.support.v4.view.ViewPager;public class MyAdapter extends PagerAdapter{List<View> viewLists;public MyAdapter(List<View> lists){viewLists = lists;}@Overridepublic int getCount() {                                                                 //获得size// TODO Auto-generated method stubreturn viewLists.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {                         // TODO Auto-generated method stubreturn arg0 == arg1;}@Overridepublic void destroyItem(View view, int position, Object object)                       //销毁Item{((ViewPager) view).removeView(viewLists.get(position));}@Overridepublic Object instantiateItem(View view, int position)                                //实例化Item{((ViewPager) view).addView(viewLists.get(position), 0);return viewLists.get(position);}}

最后Main类

package com.example.myview;import java.util.List;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.graphics.Matrix;
import android.widget.ImageView;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;public class MainActivity extends Activity {private ViewPager viewPager;private ImageView imageView;private List<View> lists = new ArrayList<View>();private MyAdapter myAdapter;private Bitmap cursor;private int offSet;private int currentItem;private Matrix matrix = new Matrix();private int bmWidth;private Animation animation;private TextView textView1;private TextView textView2;private TextView textView3;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById (R.id.cursor);textView1 = (TextView) findViewById (R.id.textView1);textView2 = (TextView) findViewById (R.id.textView2);textView3 = (TextView) findViewById (R.id.textView3);lists.add(getLayoutInflater().inflate(R.layout.layout1, null));lists.add(getLayoutInflater().inflate(R.layout.layout2, null));lists.add(getLayoutInflater().inflate(R.layout.layout3, null));initeCursor();myAdapter = new MyAdapter(lists);viewPager = (ViewPager) findViewById (R.id.viewPager);viewPager.setAdapter(myAdapter);viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {                                 //当滑动式,顶部的imageView是通过animation缓慢的滑动// TODO Auto-generated method stubswitch (arg0){case 0:if (currentItem == 1){animation = new TranslateAnimation(offSet * 2 + bmWidth, 0 , 0, 0);}else if(currentItem == 2){animation = new TranslateAnimation(offSet * 4 + 2 * bmWidth, 0, 0, 0);}break;case 1:if (currentItem == 0){animation = new TranslateAnimation(0, offSet * 2 + bmWidth, 0, 0);}else if (currentItem == 2){animation = new TranslateAnimation(4 * offSet + 2 * bmWidth, offSet * 2 + bmWidth, 0, 0);}break;case 2:if (currentItem == 0){animation = new TranslateAnimation(0, 4 * offSet + 2 * bmWidth, 0, 0);}else if (currentItem == 1){animation = new TranslateAnimation(offSet * 2 + bmWidth, 4 * offSet + 2 * bmWidth, 0, 0);}}currentItem = arg0;animation.setDuration(500);animation.setFillAfter(true);imageView.startAnimation(animation);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub
                }@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub
                }});textView1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubviewPager.setCurrentItem(0);}});textView2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubviewPager.setCurrentItem(1);}});textView3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubviewPager.setCurrentItem(2);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}private void initeCursor(){cursor = BitmapFactory.decodeResource(getResources(), R.drawable.cursor);bmWidth = cursor.getWidth();DisplayMetrics dm;dm = getResources().getDisplayMetrics();offSet = (dm.widthPixels - 3 * bmWidth) / 6;matrix.setTranslate(offSet, 0);imageView.setImageMatrix(matrix);                                             //需要iamgeView的scaleType为matrixcurrentItem = 0;}
}

类外还有3个layout:layout1,layout2, layout3是最简单,就是定义了不同的背景颜色,这里不给出。

转载于:https://www.cnblogs.com/lky-mily/archive/2012/09/01/2666712.html

android--多View切换viewpager相关推荐

  1. Android自定义view之ViewPager指示器——2

    Android自定义view之ViewPager指示器--2 上一篇<Android自定义view之ViewPager指示器--1>中我们一起写了测量和布局的流程.本篇我们继续讲解事件分发 ...

  2. Android自定义view之ViewPager指示器——1

    Android自定义view之ViewPager指示器--1 在上两篇文章<Android自定义view之measure.layout.draw三大流程>以及<Android自定义v ...

  3. android fragment界面滑动切换效果,Android App中使用ViewPager+Fragment实现滑动切换效果...

    在android应用中,多屏滑动是一种很常见的风格,没有采用viewpager的代码实现会很长,如果采用ViewPager,代码就会短很多,但是使用ViewPager也有弊端:需要导入android- ...

  4. android本页切换子页,android ViewPager控件实现手势滑动切换页签-Fun言

    1,主布局文件 android:layout_width="match_parent" android:layout_height="match_parent" ...

  5. Android横竖屏切换View设置不同尺寸或等比例缩放的自定义View的onMeasure解决方案(2)...

    Android横竖屏切换View设置不同尺寸或等比例缩放的自定义View的onMeasure解决方案(2) 附录文章1以xml布局文件方式实现了一个view在横竖屏切换时候的大小尺寸缩放,实现这种需求 ...

  6. android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...

    转载:http://blog.csdn.net/xiabing082/article/details/48781489 1.  大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...

  7. Android开源控件ViewPager Indicator的使用方法

     1月16日厦门 OSC 源创会火热报名中,奖品多多哦   摘要 Android开源控件ViewPager Indicator的使用介绍 ViewPagerIndicator 目录[-] 1. V ...

  8. android fragment实例化,Android使得Fragment 切换时不重新实例化

    以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...

  9. Android 自定义View合集

    http://blog.csdn.net/u011507982/article/details/51199644 自定义控件学习  https://github.com/GcsSloop/Androi ...

最新文章

  1. 利用DNS Zone Transfers漏洞工具dnswalk
  2. vivo X21低调奢华 彭于晏携手黑金版来袭
  3. javascript 模块模式
  4. 【具体数学--读书笔记】1.1 The Power of Hanoi
  5. 毕设日志5.12凌晨
  6. hbase java 建表_Java在HBase数据库创建表
  7. 软件测试常见的风险,软件测试中常见的风险分析
  8. C++流操作练习:统计一篇英文(The_Holy_Bible.txt)文章中出现的单词和词频 并组成字典
  9. 异构计算-1-10x10:一种异质性和提高能量效率的通用架构方法
  10. python常用的颜色英文表达_python 中颜色的表示
  11. Mybatis-Plus整理知识点01
  12. 微信小程序申请开通直播功能
  13. linux服务器停止步骤,停止和重新启动许可服务器的步骤
  14. python写入csv指定单元格_使用python将值插入csv中的特定单元格
  15. 414 Request-URI Too Large
  16. 软件工程导论——总体设计
  17. 如何获取红米手机5A的Root权限
  18. 形象设计专业AR虚拟仿真实训方案
  19. 凡是你排斥的,就是你要学习的(二)
  20. 官方兼职人员/直招全报名处

热门文章

  1. 拟合公式_Graphpad Prism 8.0对散点图进行拟合
  2. Java中抽象类和接口的区别(来源二,原始来源不明确)
  3. 长时间使用s档有危害吗_空调长时间不清洗竟有这么多危害 你知道吗?
  4. micropython是什么意思_MicroPython到底是啥-百度经验
  5. 在pycharm/IDEA里编辑latex:TeXiFy-IDEA/Pycharm
  6. 私人飞机包机运营商flyExclusive通过与BitPay合作支持加密货币付款
  7. 去中心化存储项目Sia计划于2月初启动Sia基金会
  8. 港股区块链概念股走强,火币科技涨超17%
  9. Arcgis for android 100.4 getFieldType ()
  10. 2019.01.13 bzoj4137: [FJOI2015]火星商店问题(线段树分治+可持久化01trie)