首先是控件轮换

一.创建主布局

1.用到的控件是 TextSwitcher (文本轮换)

  那么其他对应的也就是 ImageSwitcher (图片轮换)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yuxuan.lunhuan.activity.MainActivity" > <TextSwitcher
android:id="@+id/textSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#440000ff" > </TextSwitcher> <ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#44ff0000" > </ImageSwitcher> </LinearLayout>

activity_main.xml

二.主要代码

1.声明一下控件 TextSwitcher

private TextSwitcher textSwitcher1;

2.定义一个String类型的数组用来做数据

private String[] strs = new String[] { "1", "2", "3" };

3.创建一个int型的变量用来记录下标

private int index = 0;

4.创建两个int型的值用来保存下面会用到的触摸事件手机按下和松开的X值

private int startx;

private int endx;

5.在初始化事件里开始写代码把!

textSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);

                // 创建工厂 匿名内部类
textSwitcher1.setFactory(new ViewFactory() { @Override
// 用这个方法创建TextView
public View makeView() { return new TextView(MainActivity.this);
}
}); // 定义一个动画(可有可无)
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f); animation.setDuration(1500);
animation.setFillAfter(true); textSwitcher1.setInAnimation(animation);
textSwitcher1.setText(strs[0]); textSwitcher1.setOnTouchListener(new OnTouchListener() { @Override
// 设置触摸事件
public boolean onTouch(View v, MotionEvent event) {
// 按下
if (event.getAction() == MotionEvent.ACTION_DOWN) { startx = event.getX(); }
// 松开
if (event.getAction() == MotionEvent.ACTION_UP) { endx = event.getX();
// 滑动一定距离才执行
if (startx - endx > 100) {
// 判断下标
if (index == strs.length) {
index = 0;
}
// 设置文本 下标加1
textSwitcher1.setText(strs[index]);
index = index + 1;
} }
return true;
}
});

然后是图片轮换 其实大致上是一样的 直接上代码

首先在主布局文件中添加一个 ImageSwitcher 控件

然后进入代码编写

1.声明一下控件 ImageSwitcher

private ImageSwitcher imageSwitcher1;

2.定义一个int类型的数组用来保存所需图片的ID

private int[] imgs = new int[] { android.R.drawable.alert_dark_frame,
android.R.drawable.arrow_down_float,
android.R.drawable.btn_dropdown, };

3.创建一个int型的变量用来记录下标 (同上)

4.创建两个int型的值用来保存下面会用到的触摸事件手机按下和松开的X值 (同上)

5.在初始化事件里开始写代码把!

imageSwitcher1 = (ImageSwitcher) findViewById(R.id.imageSwitcher1);

        imageSwitcher1.setFactory(new ViewFactory() {

            @Override
// 创建 ImageView 这里我们需要处理一下背景
public View makeView() { ImageView lv = new ImageView(MainActivity.this);
lv.setBackgroundColor(Color.RED);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
lv.setLayoutParams(params);
return lv;
}
}); TranslateAnimation inanimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f); inanimation.setDuration(1500);
inanimation.setFillAfter(true); imageSwitcher1.setInAnimation(inanimation);
imageSwitcher1.setImageResource(imgs[index]); imageSwitcher1.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { startx = event.getX(); }
if (event.getAction() == MotionEvent.ACTION_UP) { endx = event.getX(); if (startx - endx > 100) {
if (index == strs.length) {
index = 0;
} imageSwitcher1.setImageResource(imgs[index]);
index = index + 1;
} }
return true;
}
});

最后写一写页面间的轮换

首先放上主布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yuxuan.lunhuanym.MainActivity" > // 这里用的是用的是 android-support-v4 里面的控件 详情可以百度
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager> </LinearLayout>

activity_main.xml

下面的直接附上主代码

package com.yuxuan.lunhuanym;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup; public class MainActivity extends Activity { private ViewPager pager; private int[] pagids = new int[] { R.layout.activity_view1,
R.layout.activity_view2, R.layout.activity_view3, }; private PagerAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
} private void initView() {
setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.vp); adapter = new MyPageAdapter(); pager.setAdapter(adapter);
} // 自定义适配器
private class MyPageAdapter extends PagerAdapter { private List<View> vs; public MyPageAdapter() {
vs = new ArrayList<View>();
for (int i = 0; i < pagids.length; i++) {
View view = View.inflate(MainActivity.this, pagids[i], null);
vs.add(view);
}
} @Override
// 要轮放的页面总共有多少
public int getCount() { return pagids.length;
} @Override
public boolean isViewFromObject(View view, Object object) { return view == object;
} @Override
// 初始化一个条目
// container viewpager 本身
// position 马上出来的试图
public Object instantiateItem(ViewGroup container, int position) { container.addView(vs.get(position)); return vs.get(position);
} @Override
// 销毁一个条目
// container 容器本身
// position 销毁的下标
// object 销毁的page
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
} }
}

感觉稍微牛逼点的控件都和适配器有关 有木有~

Android 轮换控件的更多相关文章

  1. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  2. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  3. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  4. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  5. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  6. Android给控件添加触摸回调

    Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...

  7. Android 基本控件相关知识整理

    Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...

  8. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

  9. Android:控件布局(相对布局)RelativeLayout(转)

    相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above----------位于给定DI控件之上android:layout_below ------ ...

随机推荐

  1. 关于MySql的1146错误修正

    在Mysql数据库中建立连接Mysql后建立了一个数据库名叫Mysql后删除了系统自动建立的数个表,导入.sql文件运行后,想要运行相关的SQL语句却发现一些未知错误为 Table 'mysql.pr ...

  2. 网址http换成https ----js

    <script type="text/javascript"> var url = window.location.href; if (url.indexOf(&quo ...

  3. 5.2 Array类型介绍

    Array类型是数组类型,Array(数组)类型也是引用类型中的一种. js 数组中的每一项可以保存任何类型的数据. js数组的大小/长度是可以动态调整的.如果你往数组中添加数据,数组长度会自动增加. ...

  4. Ubantu【第一篇】:Ubantu中openssh连接

    h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...

  5. 深入理解javascript选择器API系列第三篇——h5新增的3种selector方法

    × 目录 [1]方法 [2]非实时 [3]缺陷 前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuer ...

  6. heartbeat在yum系发行版本的处理资料

    centos 安装包[rpm]和光盘iso文件 http://mirror.centos.org/centos/ 对应如上包的代码 http://vault.centos.org/ git.cento ...

  7. Looper.prepare()和Looper.loop()

    什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建 ...

  8. minSdkVersion maxSdkVersion targetSdkVersion target 的区别

    minSdkVersion 描述:app最低支持的sdk版本号 作用:如果手机SdkVersion小于app中定义的minSdkVersion,则android系统不允许安装该app 定义位置:And ...

  9. 原创 C++之常量(一)

    1概述 一个C++程序就是一系列数据与操作的集合.当一个C++程序开始运行的时候,与该程序相关的数据就会被加载到内存中.当数据与内存发生关联的时候,这些数据就会具有如下的特性: 数据在内存中的地址.这 ...

  10. VmWare平台Windows Server 2012 无响应宕机

    我们生产服务器都部署在VMware ESXi 5.5平台上,最近大半年的时间,偶尔就会出现操作系统为Windows Servre 2012的服务器出现没有任何响应(unresponsive)的情况,出 ...