你的位置:首页 > 操作系统

[操作系统]自定义图片轮播框架。也是作为依赖库使用的哦


图片的轮播,我相信大家都在熟悉不过了 ,对吧。今天在这里发表一下自己的见解。。。

也是从继承LineaLayout开始。。。

话不多说 。上代码咯  

package com.weight.adscrollviewlib;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.weight.adscrollviewlib.entity.Ad;

public class AdScrollLayout extends LinearLayout {
ArrayList<Integer> bitMapData = new ArrayList<Integer>();
ArrayList<View> bitValue = new ArrayList<View>();
ArrayList<ImageView> imgData = new ArrayList<ImageView>();
ViewPager myViewPage;
LinearLayout myImgState;// 状态的显示容器
Timer timer;
TimerTask task;
int index = 0; // 当前页面的具体的滑动的位置
int stateImgNormal;
int stateImgSelect;

public AdScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();

}

public AdScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}

public AdScrollLayout(Context context) {
super(context);
initView();
}

/**
*
* @param content
* :图片的内容
* @param normalBitMap
* :正常情况下显示的图片
* @param selectBitMap
* :选择情况下显示的图片
*/
public void addImageAndBitMapState(ArrayList<Ad> content, int normalBitMap,
int selectBitMap) {
stateImgNormal = normalBitMap;
stateImgSelect = selectBitMap;
LayoutInflater inflater = LayoutInflater.from(getContext());
for (int i = 0; i < content.size(); i++) {
ImageView imageView = null;
if (i == 0) {
imageView = new ImageView(getContext());
imageView.setBackgroundResource(selectBitMap);
} else {
imageView = new ImageView(getContext());
imageView.setBackgroundResource(normalBitMap);
}
myImgState.setGravity(Gravity.CENTER);
LayoutParams params = new LinearLayout.LayoutParams(8, 8);
params.leftMargin = 10;
myImgState.addView(imageView, params);
imgData.add(imageView);

}
// 下面的这一个主要是对数据重构
for (int i = 0; i < content.size(); i++) {
View view = inflater.inflate(R.layout.ad_scroll_item, null);
view.findViewById(R.id.adImg).setBackgroundResource(
content.get(i).getImg());
bitValue.add(view);
}
myViewPage.setAdapter(new MyAdapter());
setPageFromTime(3000);
}

/**
*
* @Title: initView
* @Description: 初始化布局页面
* @param
* @return
* @throws
*/
private void initView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.ad_scroll, this);
findView(view);
myViewPage.setOnPageChangeListener(new MyPageChangeLister());
}

/**
* 页面的滑动监听
*
* @author Administrator
*
*/
private class MyPageChangeLister implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageSelected(int id) {
index = id;
// Log.e("arg0", id + "");

hand();
}
}

/**
* ViewPager的适配器
*
* @author Administrator
*
*/
private class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return bitValue.size();
}

@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}

@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(bitValue.get(position));
}

@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(bitValue.get(position));
return bitValue.get(position);
}
}

/**
*
* @Title: findView
* @Description: 找控件
* @param @param view
* @return
* @throws
*/
private void findView(View view) {
myViewPage = (ViewPager) view.findViewById(R.id.myViewPage);
myImgState = (LinearLayout) view.findViewById(R.id.myImgState);
}

// 需要一个方法:通过时间来动态的使用ViewPage最终实现页面的一个切换功能

public void setPageFromTime(int delayTime) {
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
// 业务逻辑
hand();
index++;
if (index == imgData.size()) {
index = 0;
}
}
};
timer.schedule(task, delayTime, delayTime);
}

/**
*
* @Title: hand
* @Description: handle发送数据
* @param
* @return
* @throws
*/
protected void hand() {
Bundle bundle = new Bundle();
bundle.putInt("index", index);
Message msg = new Message();
msg.setData(bundle);
msg.what = 100;
handler.sendMessage(msg);
}

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 100:
int index = msg.getData().getInt("index");
initPic(index);
break;
}

}
};

/**
*
* @Title: initPic
* @Description: 定时的更新页面
* @param @param index2:当前页面应该更新的一个位置
* @return
* @throws
*/
protected void initPic(int index2) {
myViewPage.setCurrentItem(index2);
changeImage(index2);
}

/**
* 传递一个id过来
*
* @param id
*/
public void changeImage(int id) {
for (int i = 0; i < imgData.size(); i++) {
if (id == i) {
imgData.get(i).setBackgroundResource(stateImgSelect);
} else {
imgData.get(i).setBackgroundResource(stateImgNormal);
}
}
}

public ViewPager getViewPage() {
return myViewPage;
}

}

这就是自定义的代码 ,其实很简单的 。。 

做做好事。。  实代码全部给你们

package com.weight.adscrollviewlib.entity;

/**
* 广告这个实体
*
* @author Administrator
*
*/
public class Ad {
private int img; // 广告的内容图片

public Ad(int img) {
this.img = img;
}

public Ad() {
}

public int getImg() {
return img;
}

public void setImg(int img) {
this.img = img;
}
}

然后 这部分就好了  要源码的伙伴们。。 可以找我哦。。。

源码地址:链接:http://pan.baidu.com/s/1nvoyQbJ  密码:y375

 

使用它 要作为依赖库的哟。。