android viewpage的使用

2024-05-16 10:48
文章标签 android 使用 viewpage

本文主要是介绍android viewpage的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


android viewpage的使用


android animation layout listview encoding matrix

在使用之前要加support.v4包哦,一般在D:\android-sdk-windows\extras\android\support\v4目录下面,好像4.0以后见工程的时候自动加载的......

下面贴代码及效果图:


mainactivity类代码如下:

[html]  view plain copy print ?
  1. package com.xy.viewpager;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.util.DisplayMetrics;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.view.animation.Animation;  
  17. import android.view.animation.TranslateAnimation;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20. import android.widget.RelativeLayout;  
  21. import android.widget.TextView;  
  22. import android.app.Activity;  
  23. import android.app.AlertDialog;  
  24. import android.content.DialogInterface;  
  25. import android.graphics.BitmapFactory;  
  26.   
  27. public class MainActivity extends Activity {  
  28.     private ViewPager mPager;// 页卡内容  
  29.     private List<View> listViews; // Tab页面列表  
  30.     private ImageView cursor;// 动画图片  
  31.     private TextView t1, t2, t3;// 页卡头标  
  32.     private int offset = 0;// 动画图片偏移量  
  33.     private int currIndex = 0;// 当前页卡编号  
  34.     private int bmpW;// 动画图片宽度  
  35.     MyPagerAdapter adapter;  
  36.     LayoutInflater mInflater;  
  37.   
  38.     RelativeLayout rel;  
  39.   
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.         Log.i("Viewpage", "--onCreate--");  
  46.         initImageView();  
  47.         initTextView();  
  48.         initPageView();  
  49.   
  50.     }  
  51.   
  52.     private void initPageView() {  
  53.         mInflater = getLayoutInflater();  
  54.         listViews = new ArrayList<View>();  
  55.         listViews.add(mInflater.inflate(R.layout.layou1, null));  
  56.         listViews.add(mInflater.inflate(R.layout.layou2, null));  
  57.         listViews.add(mInflater.inflate(R.layout.layou3, null));  
  58.         adapter = new MyPagerAdapter(listViews);  
  59.         mPager = (ViewPager) findViewById(R.id.page);  
  60.         mPager.setAdapter(adapter);  
  61.         mPager.setCurrentItem(0);  
  62.         mPager.setOnPageChangeListener(new MyOnPageChangeListener());  
  63.     }  
  64.   
  65.     private void initTextView() {  
  66.         t1 = (TextView) findViewById(R.id.tab1);  
  67.         t2 = (TextView) findViewById(R.id.tab2);  
  68.         t3 = (TextView) findViewById(R.id.tab3);  
  69.         t1.setOnClickListener(new MyOnClickListener(0));  
  70.         t2.setOnClickListener(new MyOnClickListener(1));  
  71.         t3.setOnClickListener(new MyOnClickListener(2));  
  72.     }  
  73.   
  74.     private void initImageView() {  
  75.         cursor = (ImageView) findViewById(R.id.cursor);  
  76.         rel = (RelativeLayout) findViewById(R.id.layout);  
  77.           
  78.   
  79.         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.png)  
  80.                 .getWidth();  
  81.         DisplayMetrics dm = new DisplayMetrics();  
  82.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  83.         int screenW = dm.widthPixels;  
  84.         offset = (screenW / 3 - bmpW) / 2;  
  85.         // Matrix matrix = new Matrix();  
  86.         // matrix.postTranslate(offset, 0);  
  87.         cursor.setBackgroundResource(R.drawable.png);  
  88.         // cursor.setScaleType(ScaleType.MATRIX);  
  89.         // cursor.setImageMatrix(matrix);  
  90.         rel.setPadding(offset, 0, 0, 0);  
  91.   
  92.     }  
  93.   
  94.     public class MyOnClickListener implements View.OnClickListener {  
  95.         private int index = 0;  
  96.   
  97.         public MyOnClickListener(int i) {  
  98.             index = i;  
  99.         }  
  100.   
  101.         @Override  
  102.         public void onClick(View v) {  
  103.             // TODO Auto-generated method stub  
  104.             mPager.setCurrentItem(index);  
  105.         }  
  106.     }  
  107.   
  108.     public class MyPagerAdapter extends PagerAdapter implements OnClickListener {  
  109.         public List<View> mListViews;  
  110.         public View v1;  
  111.         public View v2;  
  112.         public View v3;  
  113.         public Button mButton;  
  114.   
  115.         public MyPagerAdapter(List<View> mListViews) {  
  116.             this.mListViews = mListViews;  
  117.             getViewClickListener(mListViews);  
  118.         }  
  119.   
  120.         public void getViewClickListener(List<View> listview) {  
  121.             v1 = listview.get(0);  
  122.             v2 = listview.get(1);  
  123.             v3 = listview.get(2);  
  124.             mButton = (Button) v1.findViewById(R.id.button);  
  125.             mButton.setOnClickListener(this);  
  126.         }  
  127.   
  128.         public void destroyItem(View arg0, int arg1, Object arg2) {  
  129.             ((ViewPager) arg0).removeView(mListViews.get(arg1));  
  130.         }  
  131.   
  132.         public void finishUpdate(View arg0) {  
  133.         }  
  134.   
  135.         @Override  
  136.         public int getCount() {  
  137.             return mListViews.size();  
  138.         }  
  139.   
  140.         @Override  
  141.         public Object instantiateItem(View arg0, int arg1) {  
  142.             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
  143.             return mListViews.get(arg1);  
  144.         }  
  145.   
  146.         @Override  
  147.         public boolean isViewFromObject(View arg0, Object arg1) {  
  148.             return arg0 == (arg1);  
  149.         }  
  150.   
  151.         @Override  
  152.         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  153.         }  
  154.   
  155.         @Override  
  156.         public Parcelable saveState() {  
  157.             return null;  
  158.         }  
  159.   
  160.         @Override  
  161.         public void startUpdate(View arg0) {  
  162.         }  
  163.   
  164.         @Override  
  165.         public void onClick(View v) {  
  166.             AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)  
  167.                     .setIcon(null)  
  168.                     .setTitle("dialog")  
  169.                     .setMessage("nihao")  
  170.                     .setPositiveButton("确定",  
  171.                             new DialogInterface.OnClickListener() {  
  172.   
  173.                                 @Override  
  174.                                 public void onClick(DialogInterface arg0,  
  175.                                         int arg1) {  
  176.   
  177.                                     MainActivity.this.finish();  
  178.   
  179.                                 }  
  180.   
  181.                                 })  
  182.                     .setNegativeButton("取消",  
  183.                             new DialogInterface.OnClickListener() {  
  184.   
  185.                                 @Override  
  186.                                 public void onClick(DialogInterface arg0,  
  187.                                         int arg1) {  
  188.   
  189.                                 }  
  190.   
  191.                             }).create();  
  192.   
  193.             // 显示对话框也可以使用showDialog(int id)方法显示对话框  
  194.   
  195.             dialog.show();  
  196.         }  
  197.     }  
  198.   
  199.     public class MyOnPageChangeListener implements OnPageChangeListener {  
  200.   
  201.         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量  
  202.         int two = one * 2;// 页卡1 -> 页卡3 偏移量  
  203.   
  204.         @Override  
  205.         public void onPageSelected(int arg0) {  
  206.             Animation animation = null;  
  207.             switch (arg0) {  
  208.             case 0:  
  209.                 if (currIndex == 1) {  
  210.                     animation = new TranslateAnimation(one, 0, 0, 0);  
  211.                 } else if (currIndex == 2) {  
  212.                     animation = new TranslateAnimation(two, 0, 0, 0);  
  213.                 }  
  214.                 break;  
  215.             case 1:  
  216.                 if (currIndex == 0) {  
  217.                     animation = new TranslateAnimation(offset, one, 0, 0);  
  218.                 } else if (currIndex == 2) {  
  219.                     animation = new TranslateAnimation(two, one, 0, 0);  
  220.                 }  
  221.                 break;  
  222.             case 2:  
  223.                 if (currIndex == 0) {  
  224.                     animation = new TranslateAnimation(offset, two, 0, 0);  
  225.                 } else if (currIndex == 1) {  
  226.                     animation = new TranslateAnimation(one, two, 0, 0);  
  227.                 }  
  228.                 break;  
  229.             }  
  230.             currIndex = arg0;  
  231.             animation.setFillAfter(true);// True:图片停在动画结束位置  
  232.             animation.setDuration(300);  
  233.             rel.startAnimation(animation);  
  234.         }  
  235.   
  236.         @Override  
  237.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  238.               
  239.         }  
  240.   
  241.         @Override  
  242.         public void onPageScrollStateChanged(int arg0) {  
  243.               
  244.         }  
  245.     }  
  246. }  

mainlayout.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/nav"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dp"  
  11.         android:background="#efefef" >  
  12.   
  13.         <TextView  
  14.             android:id="@+id/tab1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_weight="1.0"  
  18.             android:gravity="center"  
  19.             android:text="页片1"  
  20.             android:textColor="#000000" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tab2"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:layout_weight="1.0"  
  27.             android:gravity="center"  
  28.             android:text="页片2"  
  29.             android:textColor="#000000" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/tab3"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="fill_parent"  
  35.             android:layout_weight="1.0"  
  36.             android:gravity="center"  
  37.             android:text="页片3"  
  38.             android:textColor="#000000" />  
  39.     </LinearLayout>  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/layout"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content" >  
  45.   
  46.         <ImageView  
  47.             android:id="@+id/cursor"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content" />  
  50.     </RelativeLayout>  
  51.   
  52.     <android.support.v4.view.ViewPager  
  53.         android:id="@+id/page"  
  54.         android:layout_width="fill_parent"  
  55.         android:layout_height="fill_parent" >  
  56.     </android.support.v4.view.ViewPager>  
  57.   
  58. </LinearLayout>  

layout1.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#565656"  
  6. android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="点击事件" />  
  13.   
  14. </LinearLayout>  

layout2.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:orientation="vertical"    
  6.     android:background="#abab00">    
  7. </LinearLayout>    

layout3.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#00abcd"  
  6.     android:orientation="vertical" >  
  7.   
  8. </LinearLayout>  




quot;fill_parent

这篇关于android viewpage的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994695

相关文章

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停