android中ViewSwitcher的使用



先看类图

类 ViewSwitcher

java.lang.Object
  继承者 android.view.View
      继承者 android.view.ViewGroup
          继承者 android.widget.FrameLayout
              继承者 android.widget.ViewAnimator
                  继承者 android.widget.ViewSwitcher
所有已实现的接口:
Drawable.Callback, KeyEvent.Callback, ViewManager, ViewParent
直接已知子类:
ImageSwitcher, TextSwitcher
可以看到ImageSwitcher实现了viewSwitcher,所以可以通过viewSwitcher实现ImageSwitcher的功能。




package com.example.fristlogin;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ViewSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements
OnTouchListener {
	
	private ViewSwitcher viewswitcher;
	private LayoutInflater inflate;
	private int[] drawables;
	private ImageView imageview;
	private float startX;
	private float endX;
	private float moveX;
	private int current=0;
	private RadioGroup rd;
	private RadioButton radio1;
	
	private RadioButton radio2;
	private RadioButton radio3;
	private RadioButton radio4;
	private List<RadioButton> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawables=new int[]{R.drawable.pic1,
        		R.drawable.pic2,R.drawable.pic3,
        		R.drawable.pic4};
        viewswitcher=(ViewSwitcher) findViewById(R.id.viewSwitcher1);
        viewswitcher.setOnTouchListener(this);
        inflate=LayoutInflater.from(getApplicationContext());
        
        list=new ArrayList<RadioButton>();
        rd=(RadioGroup) findViewById(R.id.radio);
        radio1=(RadioButton) findViewById(R.id.radio1);
        radio2=(RadioButton) findViewById(R.id.radio2);
        radio3=(RadioButton) findViewById(R.id.radio3);
        radio4=(RadioButton) findViewById(R.id.radio4);
        list.add(radio1);
        list.add(radio2);
        list.add(radio3);
        list.add(radio4);
       
        viewswitcher.setFactory(new ViewFactory() {
			
			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				View view=inflate.inflate(R.layout.item, null);
				return view;
			}
		});
       
        View view=viewswitcher.getNextView();
		((ImageView)view).setImageDrawable(getResources().
				getDrawable(drawables[current]));
		viewswitcher.showNext();
		list.get(3).setChecked(true);
	
        
    }





	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN:
			startX=event.getX();
			Log.d("move", "手指按下了!");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.d("move", "手指移动了!");
			break;
		case MotionEvent.ACTION_UP:
			Log.d("move", "手指按收起了!");
			endX=event.getX();
			moveX=endX-startX;
			if(moveX>240){
				if(current>0){
					
					current--;
					View view=viewswitcher.getNextView();
					list.get(current).setChecked(true);
					((ImageView)view).setImageDrawable(getResources().
							getDrawable(drawables[current]));
					viewswitcher.showNext();
				}
			}
			if(moveX<-240){
				if(current<3){
					
					current++;
					list.get(current).setChecked(true);
					View view=viewswitcher.getNextView();
					((ImageView)view).setImageDrawable(getResources().
							getDrawable(drawables[current]));
					
					viewswitcher.showNext();
				}
			}
			break;
		}
		return true;
	}
    
	
}

 

        在此过程中,出现了一个小插曲。当我我想通过切换图片时,改变radioButton的状态,我刚开始想改变选中状态,肯定是setselected(true),结果发现不管怎样都不能选中,最后通过查看radioButton的所有方法才发现应该是setCheck(true),在此听别提出,希望大家以后不要范相同错误;

      ViewSwitcher里面特别需要注意的是ViewFactory的makeView的方法,该方法返回的是Switcher之后的View。也可以通过不但的填充布局,然后通过viewSwitcher.addView()方法不断添加view,并通过viewSwither.showNext()或者viewSwitcher.showPrevious()切换显示的View。


布局文件如下:

<FrameLayout 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:layout_gravity="bottom"
   
    tools:context=".MainActivity" >

    <ViewSwitcher
        android:id="@+id/viewSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ViewSwitcher>
      
        <RadioGroup 
            android:id="@+id/radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           
            android:gravity="center"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            >
            <RadioButton
              android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             />
            <RadioButton
              android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             />
            <RadioButton
              android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             />
            <RadioButton
               android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             />
        </RadioGroup>

</FrameLayout>

ViewSwitcher的子布局如下:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:scaleType="fitXY" >
    

</ImageView>
通过设置imageview的scaleType=“fitXY”使图片充满整个父控件。 android:gravity="center"设置的是内容的布局,而android:layout_gravity="bottom"设置的是子布局在父布局中的布局。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值