第一次写博客,原创效果,不啰嗦,上代码上图。
/**
* 1280*720 密度2;暂不支持适配
* 运行SDK android4.4.2
* @author LXD
*
*/
public class NumberWheel extends SurfaceView implements Callback,Runnable {
private int oldms=500;//不更新UI时线程休眠时间
private int ms=17;//更新UI时线程休眠时间(帧)
private int mvingDistance=6;//更新UI时数字每次滚动距离
private long millisecond=500;//刚创建时延迟运行毫秒数
private boolean isRunnable=false;
private Drawable down=null;
private Drawable middle=null;
private Drawable up=null;
private Drawable background=null;
private SurfaceHolder holder = null;
private int width=0;//控件宽
private int height=0;//控件高
//回调函数
private CallBackListener cbl;
private NumberBean nBean=null;
public NumberWheel(Context context,NumberBean nBean,LayoutParams rl,int millisecond) {
super(context);
// TODO Auto-generated constructor stub
this.nBean=nBean;
this.millisecond=millisecond;
setLayoutParams(rl);
init();
}
//控件初始化
public void init(){
background=getResources().getDrawable(nBean.getBackgroundImage());
ViewGroup.LayoutParams lp=getLayoutParams();
if(null!=lp){
width=lp.width>background.getMinimumWidth()?background.getMinimumWidth():lp.width;
height=lp.height>background.getMinimumHeight()?background.getMinimumHeight():lp.height;
}else{
width=background.getMinimumWidth();
height=background.getMinimumHeight();
}
nBean.doLocations(getContext(), width, height);
millisecond+=System.currentTimeMillis();
holder = getHolder();
holder.addCallback(this); //设置Surface生命周期回调
}
public CallBackListener getCallBackListener() {
return cbl;
}
public void setCallBackListener(CallBackListener cbl) {
this.cbl = cbl;
}
public void restart(float endShowNum){
ms=17;
down=null;
up=null;
middle=null;
nBean.setEndShowNum(endShowNum);
nBean.reset(getContext(), width, height);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
drawStatic(nBean.getStartShowNumString());
isRunnable=true;
if(null!=cbl){
cbl.start();
}
new Thread(this).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
isRunnable=false;
}
//线程运行
@Override
public void run() {
// TODO Auto-generated method stub
while(isRunnable){
try {
if(millisecondheight){//坐标重置到顶端
temp2[2]=temp2[2]-d.getMinimumHeight()*3;
temp2[0]=temp2[0]+3>9?temp2[0]+3-10:temp2[0]+3;
d=getResources().getDrawable(nBean.getNumberImages()[temp2[0]]);
}
int location=0;//所在位置0上1中2下
int y=(height-d.getMinimumHeight())/2;
if(temp2[2]>=(y+d.getMinimumHeight())){
location=2;
}else if(temp2[2]>=y){
location=1;
}else {
location=0;
}
int x=nBean.getEndShowNumString().charAt(i)-48;
boolean isContinue=false;
int z=0;
switch (location){
case 0:
z=temp2[0]-1<0?temp2[0]-1+10:temp2[0]-1;
if(x==z&&temp2[2]+d.getMinimumHeight()>=y&&nBean.endByOrder(j)){
isContinue=true;
d=getResources().getDrawable(nBean.getNumberImages()[z]);
}
break;
case 1:
z=temp2[0];
if(x==z&&temp2[2]>=y&&nBean.endByOrder(j))
isContinue=true;
break;
case 2:
z=temp2[0]+1>9?temp2[0]+1-10:temp2[0]+1;
if(x==z&&temp2[2]-d.getMinimumHeight()>=y&&nBean.endByOrder(j)){
isContinue=true;
d=getResources().getDrawable(nBean.getNumberImages()[z]);
}
break;
}
if(isContinue){
j++;
temp2[0]=z;
temp2[3]=0;
d.setBounds(nBean.getLocations()[i][0],nBean.getLocations()[i][1],nBean.getLocations()[i][0]+d.getMinimumWidth(),nBean.getLocations()[i][1]+d.getMinimumHeight());
d.draw(canvas);
continue;
}
Rect r=new Rect(temp2[1], temp2[2], d.getMinimumWidth()+temp2[1], d.getMinimumHeight()+temp2[2]);
switch (location){
case 0:
up=d;
up.setBounds(r);
up.draw(canvas);
r.top+=d.getMinimumHeight();
r.bottom+=d.getMinimumHeight();
middle=getResources().getDrawable(nBean.getNumberImages()[temp2[0]-1<0?temp2[0]-1+10:temp2[0]-1]);
middle.setBounds(r);
middle.draw(canvas);
if((r.top+d.getMinimumHeight())>=height){
r.top-=d.getMinimumHeight()*2;
r.bottom-=d.getMinimumHeight()*2;
down=getResources().getDrawable(nBean.getNumberImages()[temp2[0]+1>9?temp2[0]+1-10:temp2[0]+1]);
}else{
r.top+=d.getMinimumHeight();
r.bottom+=d.getMinimumHeight();
down=getResources().getDrawable(nBean.getNumberImages()[temp2[0]-2<0?temp2[0]-2+10:temp2[0]-2]);
}
down.setBounds(r);
down.draw(canvas);
break;
case 1:
middle=d;
middle.setBounds(r);
middle.draw(canvas);
r.top-=d.getMinimumHeight();
r.bottom-=d.getMinimumHeight();
up=getResources().getDrawable(nBean.getNumberImages()[temp2[0]+1>9?temp2[0]+1-10:temp2[0]+1]);
up.setBounds(r);
up.draw(canvas);
if((r.top+d.getMinimumHeight()*2)>=height){
r.bottom=r.top;
r.top-=d.getMinimumHeight();
down=getResources().getDrawable(nBean.getNumberImages()[temp2[0]+2>9?temp2[0]+2-10:temp2[0]+2]);
}else{
r.top+=d.getMinimumHeight()*2;
r.bottom+=d.getMinimumHeight()*2;
down=getResources().getDrawable(nBean.getNumberImages()[temp2[0]-1<0?temp2[0]-1+10:temp2[0]-1]);
}
down.setBounds(r);
down.draw(canvas);
break;
case 2:
down=d;
down.setBounds(r);
down.draw(canvas);
r.top-=d.getMinimumHeight();
r.bottom-=d.getMinimumHeight();
middle=getResources().getDrawable(nBean.getNumberImages()[temp2[0]+1>9?temp2[0]+1-10:temp2[0]+1]);
middle.setBounds(r);
middle.draw(canvas);
r.top-=d.getMinimumHeight();
r.bottom-=d.getMinimumHeight();
up=getResources().getDrawable(nBean.getNumberImages()[temp2[0]+2>9?temp2[0]+2-10:temp2[0]+2]);
up.setBounds(r);
up.draw(canvas);
break;
}
temp2[2]+=mvingDistance;
j++;
}
}
holder.unlockCanvasAndPost(canvas);
}
//画初使数值前赋值高度
private void drawStatic(String num){
Canvas canvas=holder.lockCanvas(null);
drawbackground(canvas);
for(int i=0;i47&&c<58){
runningNum[j]=new int[4];
runningNum[j][0]=c-48;
int ec=esns[i]-48;
if(runningNum[j][0]==ec&&endByOrder(j))
runningNum[j][3]=0;
else
runningNum[j][3]=1;
j++;
}
}
}
public void reset(Context context,int width,int height){
init();
doLocations(context, width, height);
}
public int[] getNumberImages() {
return numberImages;
}
public void setNumberImages(int[] numberImages) {
this.numberImages = numberImages;
}
public int[] getNotWheelImages() {
return notWheelImages;
}
public void setNotWheelImages(int[] notWheelImages) {
this.notWheelImages = notWheelImages;
}
public int[][] getLocations() {
return locations;
}
public void setLocations(int[][] locations) {
this.locations = locations;
}
public int getBackgroundImage() {
return backgroundImage;
}
public void setBackgroundImage(int backgroundImage) {
this.backgroundImage = backgroundImage;
}
public float getStartShowNum() {
return startShowNum;
}
public void setStartShowNum(float startShowNum) {
this.startShowNum = startShowNum;
}
public float getEndShowNum() {
return endShowNum;
}
public void setEndShowNum(float endShowNum) {
this.endShowNum = endShowNum;
}
public String getStartShowNumString() {
return startShowNumString;
}
public void setStartShowNumString(String startShowNumString) {
this.startShowNumString = startShowNumString;
}
public String getEndShowNumString() {
return endShowNumString;
}
public void setEndShowNumString(String endShowNumString) {
this.endShowNumString = endShowNumString;
}
public int[][] getRunningNum() {
return runningNum;
}
public void setRunningNum(int[][] runningNum) {
this.runningNum = runningNum;
}
//按顺序结束滚动
private boolean endByOrder(int j){
for (int i = 0; i < j; i++) {
int [] temp=getRunningNum()[i];
if(temp[3]!=0)
return false;
}
return true;
}
//赋值位置坐标
public void doLocations(Context context,int width,int height){
locations=new int[notWheelImages.length][];
for(int i=0,j=0;i
工程下载地址1:http://download.csdn.net/detail/qiongshan0475/8032775(需要另建v7工程)
工程下载地址2:http://download.csdn.net/detail/qiongshan0475/8140303(无错版)