热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

安卓学习UI组件TextSwitcher和ImageSwitcher文字和图片切换

2019独角兽企业重金招聘Python工程师标准TextSwitcher滑动屏幕,左滑或者右滑代码:

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

4P{L]4%U_%%UX_C_6LE1FTD

78~SNSXPBHML1[J6G$1%P09

TextSwitcher

image

滑动屏幕,左滑或者右滑

image

代码:


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.example.administrator.myapplication.MainActivity2">

android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textSwitcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

 

 

package com.example.administrator.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity2 extends ActionBarActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener {

private TextSwitcher textSwitcher;
private String[] texts={"岁月是一把无情的杀猪刀","岁月静好,现世安稳","我有钱我任性"};
private int index; //默认为0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textSwitcher= (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setOnTouchListener(this);
textSwitcher.setFactory(this);
}

@Override
public View makeView() {

TextView tv=new TextView(this);
tv.setText(texts[index]);
return tv;
}

float startX=0.0f;
float endX=0.0f;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();//获取当前的事件动作
System.out.println("action="+action);//sout

if(action==MotionEvent.ACTION_DOWN)
{
startX=event.getX();
return true;
}
if(action==MotionEvent.ACTION_UP)
{
endX=event.getX();
if(startX-endX>20)//下一张
{
index=(index+1 textSwitcher.setInAnimation(this,android.R.anim.fade_in);
textSwitcher.setOutAnimation(this, android.R.anim.fade_out);

// imageSwitcher.setImageResource(images[index]);
}
else if(endX-startX>20){ //上一张
index=(index-1>=0?--index:texts.length-1);
// imageSwitcher.setImageResource(images[index]);
textSwitcher.setInAnimation(this,android.R.anim.fade_in);
textSwitcher.setOutAnimation(this,android.R.anim.fade_out);

}
System.out.println("index"+index);
textSwitcher.setText(texts[index]);

}

return true;
}
}

 

ImageSwitcher

滑动后

代码:


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.example.administrator.myapplication.MainActivity">

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageSwitcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />




 

 

package com.example.administrator.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity implements ViewSwitcher.ViewFactory,View.OnTouchListener{

private ImageSwitcher imageSwitcher;

private int[] images={R.mipmap.pic11,R.mipmap.pic13,R.mipmap.pic15,R.mipmap.pic6};
private String[] texts={"岁月是一把无情的杀猪刀","岁月静好,现世安稳","我有钱我任性","当你老了"};
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);

imageSwitcher.setOnTouchListener(this);
imageSwitcher.setFactory(this);

}

@Override
public View makeView() {
ImageView iv=new ImageView(this);
iv.setImageResource(images[0]);
TextView tv=new TextView(this);
tv.setText(texts[index]);
return iv;

}


float startX=0.0f;
float endX=0.0f;
// 触屏事件
@Override
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();//获取当前的事件动作
System.out.println("action="+action);//sout

if(action==MotionEvent.ACTION_DOWN)
{
startX=event.getX();
return true;
}
if(action==MotionEvent.ACTION_UP)
{
endX=event.getX();
if(startX-endX>20)//下一张
{
index=(index+1 imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
imageSwitcher.setOutAnimation(this, android.R.anim.fade_out);

// imageSwitcher.setImageResource(images[index]);
}
else if(endX-startX>20){ //上一张
index=(index-1>=0?--index:images.length-1);
// imageSwitcher.setImageResource(images[index]);
imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
imageSwitcher.setOutAnimation(this,android.R.anim.fade_out);

}
System.out.println("index"+index);
imageSwitcher.setImageResource(images[index]);

}

return true;
}
}














转:https://my.oschina.net/xiaofeiandroid/blog/632834



推荐阅读
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
author-avatar
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有