ImageSwitch和TextSwitch

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center_horizontal">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </ImageSwitcher>

    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一张" >
        </Button>

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" >
        </Button>
    </LinearLayout>

</LinearLayout>


 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">ImageTextSwitcher</string>
    <string name="photo_1">图片1</string>
    <string name="photo_2">图片2</string>
    <string name="photo_3">图片3</string>
    <string name="photo_4">图片4</string>
    <string name="photo_5">图片5</string>
    <string name="photo_6">图片6</string>
    <string name="photo_7">图片7</string>
</resources>


ActivityMain

package com.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class ActivityMain extends Activity {
	
	private ImageSwitcher mImageSwitcher;
	private TextSwitcher mTextSwitcher;
	//当前显示的索引
	private int current;
	//图片资源
    private Integer[] mImageIds = {
            R.drawable.photo_1, R.drawable.photo_2, R.drawable.photo_3,
            R.drawable.photo_4, R.drawable.photo_5, R.drawable.photo_6,
            R.drawable.photo_7};
    //文本资源
    private Integer[] mTextIds = {
            R.string.photo_1, R.string.photo_2, R.string.photo_3,
            R.string.photo_4, R.string.photo_5, R.string.photo_6,
            R.string.photo_7};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取ImageSwitcher对象
        mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        //设置ViewFactory
        mImageSwitcher.setFactory(new ViewFactory (){
			@Override
			public View makeView() {
				//ImageSwitcher需要返回ImageView对象
		        ImageView i = new ImageView(ActivityMain.this);
		        i.setBackgroundColor(0xFF000000);
		        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
		                LayoutParams.FILL_PARENT));
		        return i;
			}
        	
        });
        //设置动画淡然淡出效果
        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        //默认值
        mImageSwitcher.setImageResource(mImageIds[0]);
        current = 0;
        
        //TextSwitcher例子
        mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        mTextSwitcher.setFactory(new ViewFactory (){

			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				//ImageSwitcher需要返回ImageView对象
				//TextSwitcher需要返回TextView对象
		        TextView i = new TextView(ActivityMain.this);
		        i.setTextSize(25);
		        return i;
			}
        	
        });
        mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left));
        mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right));
        mTextSwitcher.setText(this.getResources().getText(mTextIds[0]));
        //按钮定义
        Button previousBtn = (Button) findViewById(R.id.previous);
        Button nextBtn = (Button) findViewById(R.id.next);
        
        previousBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(current>0){
					current--;
					//设置ImageSwitcher要显示的图片
					mImageSwitcher.setImageResource(mImageIds[current]);
					mTextSwitcher.setText(ActivityMain.this.getResources().getText(mTextIds[current]));
				}
			}
        	
        });
        
        nextBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(current<mImageIds.length-1){
				  current++;
				  mImageSwitcher.setImageResource(mImageIds[current]);
				  mTextSwitcher.setText(ActivityMain.this.getResources().getText(mTextIds[current]));
				}
			}
        	
        });
       
    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用Gallery和ImageSwitcher来实现可左右循环滑动的图片浏览器。 首先,在布局文件中添加一个Gallery和一个ImageSwitcher: ``` <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_vertical" /> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/gallery" /> ``` 然后,在Java代码中,需要为Gallery设置一个Adapter,并为ImageSwitcher设置一个ViewFactory。代码如下: ``` public class MainActivity extends AppCompatActivity { private Gallery mGallery; private ImageSwitcher mImageSwitcher; private int[] mImageIds = new int[]{R.drawable.image1, R.drawable.image2, R.drawable.image3}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGallery = (Gallery) findViewById(R.id.gallery); mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); mGallery.setAdapter(new ImageAdapter(this)); mImageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }); mGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mImageSwitcher.setImageResource(mImageIds[position % mImageIds.length]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context context) { mContext = context; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new Gallery.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mImageIds[position % mImageIds.length]); return imageView; } } } ``` 这里的关键是设置Gallery的Adapter为一个无限循环的Adapter,以及在Gallery的OnItemSelectedListener中更新ImageSwitcher的图片资源。同时,ImageAdapter负责为Gallery提供每个Item的View。 这样,我们就可以实现一个左右循环滑动的图片浏览器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值