700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android--多View切换viewpager

android--多View切换viewpager

时间:2023-03-07 05:49:29

相关推荐

android--多View切换viewpager

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

先看效果图:

先看主类的layout

<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/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是最简单,就是定义了不同的背景颜色,这里不给出。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。