zoukankan      html  css  js  c++  java
  • 安卓学习-界面-ui-ViewSwitcher、ImageSwitcher和TextSwitcher

    3个都差不多,一个是图片,一个是文字

    点击上一张、下一张或者向左、向右滑动

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上一张" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下一张" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ViewSwitcher
                android:id="@+id/viewSwitcher1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
    
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </ViewSwitcher>
        </LinearLayout>
    
    </LinearLayout>
    View Code

    2个动画

    slide_in_right.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
        <!-- 设置从右边拖进来的动画  
        android:duration指定动画持续时间  -->  
        <translate  
            android:fromXDelta="100%p"  
            android:toXDelta="0"  
            android:duration="@android:integer/config_mediumAnimTime" />  
    </set>
    View Code

    slide_out_left.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
        <!-- 设置从左边拖出去的动画   
        android:duration指定动画持续时间 -->  
        <translate  
            android:fromXDelta="0"  
            android:toXDelta="-100%p"  
            android:duration="@android:integer/config_mediumAnimTime" />  
    </set>  
    View Code

    MainActivity.java

    public class MainActivity extends Activity{
    
        //定义图片资源
        int[] pics=new int[]{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3};
        //用来判断手指是向左滑动还是向右
        float x1;
        float x2;
        
        ViewSwitcher viewSwitcher1;
        ImageView imageView1;
        //图片索引
        int index=0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            viewSwitcher1=(ViewSwitcher)findViewById(R.id.viewSwitcher1);
            Button btn1=(Button)findViewById(R.id.button1);
            Button btn2=(Button)findViewById(R.id.button2);
            imageView1=(ImageView)findViewById(R.id.imageView1);
            
            //默认第一张图片
            imageView1.setImageResource(pics[0]);
            //上一张
            btn1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    pre();
                }
            });
            // 下一张
            btn2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    next();
                }
            });
    
            //识别手势
            viewSwitcher1.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    //手指按下时的坐标
                    if(event.getAction()==MotionEvent.ACTION_DOWN){
                        x1=event.getX();
                    }
                    //手指松开时的坐标
                    else if(event.getAction()==MotionEvent.ACTION_UP){
                        x2=event.getX();
                        //向左滑动
                        if(x1-x2>0){
                            Log.v("", "向左滑动");
                            next();
                            
                        }
                        //向右滑动
                        else if(x2-x1>0){
                            Log.v("", "向右滑动");
                            pre();
                        }
                    }
                    return true;
                }
            });
      
        }
        
        
        void next(){
            // 为ViewSwitcher的组件显示过程设置动画  
            viewSwitcher1.setInAnimation(this, R.anim.slide_in_right); 
            // 为ViewSwitcher的组件隐藏过程设置动画  
            viewSwitcher1.setOutAnimation(this, R.anim.slide_out_left);  
            if(index==2) index=0;
            else index=index+1;
            imageView1.setImageResource(pics[index]);
            viewSwitcher1.showNext();
    
        }
        
       void pre(){
            //为ViewSwitcher的组件显示过程设置动画  
            viewSwitcher1.setInAnimation(this, android.R.anim.slide_in_left);  
            // 为ViewSwitcher的组件隐藏过程设置动画  
            viewSwitcher1.setOutAnimation(this, android.R.anim.slide_out_right);  
            if(index==0) index=2;
            else index=index-1;
            imageView1.setImageResource(pics[index]);
            viewSwitcher1.showPrevious();
            
        }
        
    
    }
    View Code
  • 相关阅读:
    hadoop中namenode发生故障的处理方法
    开启虚拟机所报的错误:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary fil
    Hbase的安装与部署(集群版)
    分别用反射、编程接口的方式创建DataFrame
    用Mapreduce求共同好友
    SparkSteaming中直连与receiver两种方式的区别
    privot函数使用
    Ajax无刷新显示
    使用ScriptManager服务器控件前后台数据交互
    数据库知识
  • 原文地址:https://www.cnblogs.com/weijj/p/3961719.html
Copyright © 2011-2022 走看看