ImageSwitcher 点击小图显示对应的大图

package com.example.imageswitcher;
import android.R.anim;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
</pre><span style="background-color:rgb(255,255,255)">import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;import android.app.Activity;</span><pre name="code" class="java">


public class MainActivity extends Activity implements ViewFactory {
/* 自定义ImagesWitcher */
private ImageSwitcher switcher;
/* 自定义Gallery */
private Gallery gallery;


/*设置大图片*/
private Integer[] big={R.drawable.news_title_images1,R.drawable.news_title_images2,R.drawable.news_title_images3,
R.drawable.news_title_images4,R.drawable.news_title_images5,R.drawable.news_title_images6,
R.drawable.news_title_images7,R.drawable.news_title_images8};
/*设置小图片*/
private Integer[] small={R.drawable.news_title_images1,R.drawable.news_title_images2,R.drawable.news_title_images3,
R.drawable.news_title_images4,R.drawable.news_title_images5,R.drawable.news_title_images6,
R.drawable.news_title_images7,R.drawable.news_title_images8};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 初始化ui
*/
findViews();
/**
* 初始化数据
*/
getIntentData();
/**
* 初始化监听
*/
setListeners();

}


/**
* 初始化数据
*/
private void getIntentData() {
//适配数据
gallery.setAdapter(new Adapter(this,big,small,big.length,null,switcher));
}


/**
* 初始化监听
*/
private void setListeners() {
// 设置图片的滑动效果
// 输入进去的效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this, anim.fade_in));
// 显示出来的效果
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,anim.fade_in));
//设置Gallery 
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switcher.setImageResource(big[arg2 % small.length]);
}
});
//设置位置为第二张图片 它是从0开始的
gallery.setSelection(1);
}


/**
* . 初始化ui
*/
private void findViews() {
switcher = (ImageSwitcher) findViewById(R.id.switcher);
switcher.setFactory(this);
gallery=(Gallery) findViewById(R.id.gallery);


}


@Override
public View makeView() {
ImageView imagesView=new ImageView(this);
//设置背景颜色
imagesView.setBackgroundColor(55000000);
//设置样式
imagesView.setScaleType(ScaleType.FIT_CENTER);
//设置条目
imagesView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
return imagesView;
}


}


适配器

package com.example.imageswitcher;


import java.util.HashMap;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageSwitcher;
import android.widget.ImageView;


public class Adapter extends BaseAdapter {
/* 自定义ImagesWitcher */
private ImageSwitcher switcher;
/* 保存数据 */
private HashMap<Integer, View> map;
private int cout;
private LayoutInflater inflater;
private Context context;
/* 设置大图片 */
private Integer[] big;
/* 设置小图片 */
private Integer[] small;


public Adapter(Context context, Integer[] big, Integer[] small, int cout,
LayoutInflater inflater,ImageSwitcher switcher) {
super();
this.context = context;
this.cout = cout;
this.big = big;
this.small = small;
this.switcher=switcher;
map = new HashMap<Integer, View>(cout);
this.inflater = LayoutInflater.from(context);


}


/**
* 数据长度
*/
@Override
public int getCount() {


return Integer.MAX_VALUE;
}


/**
* 数据所对应的数据
*/
@Override
public Object getItem(int arg0) {


return arg0;
}


/**
* 数据所对应的数据id
*/
@Override
public long getItemId(int arg0) {


return arg0;
}


@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
View view = map.get(arg0 % cout);
ImageView img = null;
if (view == null) {
//初始化ui
view = inflater.inflate(R.layout.gallery_item, null);
img = (ImageView) view.findViewById(R.id.item_gallery_image);
view.setTag(img);
} else {
img = (ImageView) view.getTag();
}
//数据映射
map.put(arg0 % cout, view);
img.setImageResource(big[arg0 % small.length]);
return view;
}


}

///xml

<RelativeLayout 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"
    >
    <com.example.imageswitcher.MyGallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:gravity="center_vertical"
       android:spacing="1dp"
        android:layout_margin="5dp">
    </com.example.imageswitcher.MyGallery>
    
    <ImageSwitcher 
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ImageSwitcher>

</RelativeLayout>

///xml

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


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值