ImageSwitch和TextSwitcher和ViewFlipper切换

博客介绍了图片、文本及屏幕间切换的ViewFlipper类,阐述其几个动画相关函数,如setInAnimation、setOutAnimation等,还提及不同方向进出屏幕的动画xml文件,最后提到了相关java代码。

图片划屏切换

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity_pswitch extends AppCompatActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener {

    private ImageSwitcher imageSwitcher;
    private int[] images = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};
    private int index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_pswitch);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitch);
        imageSwitcher.setOnTouchListener(this);
        imageSwitcher.setFactory(this);
    }

    @Override
    public View makeView() {
        ImageView iv = new ImageView(this);
        iv.setImageResource(images[0]);
        return iv;
    }

    float startX = 0.0f;
    float endX = 0.0f;
    //触屏事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();//获取当前的事件动作
        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<images.length?++index:0;
                imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
                imageSwitcher.setInAnimation(this,android.R.anim.fade_out);
            }else if(endX-startX>20){//上一张
                index = index-1>=0?--index:images.length-1;
                imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
                imageSwitcher.setInAnimation(this,android.R.anim.fade_out);
            }
            imageSwitcher.setImageResource(images[index]);
        }

        return true;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity_pswitch"
    android:padding="16dp">

    <ImageSwitcher
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageSwitch"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

文本切换

import android.support.v7.app.AppCompatActivity;
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 MainActivity_textsw extends AppCompatActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener {

    private TextSwitcher textSwitcher;
    private String[] texts = {"疏星淡月秋千院","愁云恨雨芙蓉面"};
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_textsw);
        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();//获取当前的事件动作
        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<texts.length?++index:0;
                textSwitcher.setInAnimation(this,android.R.anim.fade_in);
                textSwitcher.setInAnimation(this,android.R.anim.fade_out);
            }else if(endX-startX>20){//上一张
                index = index-1>=0?--index:texts.length-1;
                textSwitcher.setInAnimation(this,android.R.anim.fade_in);
                textSwitcher.setInAnimation(this,android.R.anim.fade_out);
            }
            textSwitcher.setText(texts[index]);
        }

        return true;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity_textsw"
    android:padding="16dp">

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

屏幕间切换ViewFlipper

该类有几个动画相关函数:

setInAnimation:进入屏幕使用的动画,可以接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为context对象和定义Animation的resourceID。

setOutAnimation:设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。

showNext:调用该函数来显示FrameLayout里面的下一个View。

showPrevious:调用该函数来显示FrameLayout里面的上一个View。

in_leftright.xml——从左到右进入屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>

out_leftright.xml——从左到右出去屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>
 

in_rightleft.xml——从右到左进入屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

out_rightleft.xml——从右到左出去屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>
 

java代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;

public class MainActivity_vf extends AppCompatActivity {
    private ViewFlipper viewFlipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vf);
        viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
    }

    float statX,endX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        if(action==MotionEvent.ACTION_DOWN){
            statX = event.getX();
        }else if(action==MotionEvent.ACTION_UP){
            endX = event.getX();
            if(statX>endX){//next
                viewFlipper.setInAnimation(this,R.anim.in_rightleft);
                viewFlipper.setOutAnimation(this,R.anim.out_rightleft);
                viewFlipper.showNext();
            }else if(endX>statX){//pre
                viewFlipper.setInAnimation(this,R.anim.in_leftright);
                viewFlipper.setOutAnimation(this,R.anim.out_leftright);
                viewFlipper.showPrevious();
            }
        }

        return super.onTouchEvent(event);
    }
}

 

ViewFlipperViewSwitcher的使用:屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。 通过查看OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个动画相关的函数: l setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Contextint,分别为Context对象定义Animation的resourceID。 setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。 showNext: 调用该函数来显示FrameLayout里面的下一个View。 showPrevious:调用该函数来显示FrameLayout里面的上一个View。 一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipperViewSwitcher。ViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数: isFlipping: 用来判断View切换是否正在进行 setFilpInterval:设置View之间切换的时间间隔 startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行 stopFlipping: 停止View切换 ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片文本切换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值