版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】banner廣告及viewpager的小圓點(diǎn)指示器
一、主界面packagecom.example.t;importandroid.app.Activity;importandroid.os.Bundle;importandroid.support.v4.view.ViewPager;importandroid.support.v4.view.ViewPager.OnPageChangeListener;publicclassMainActivityextendsActivityimplementsOnPageChangeListener{
privateCirclePageIndicatorindicator;
privateViewPagerviewpager;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
privatevoidinitview(){
viewpager=(ViewPager)findViewById(R.id.viewpager);
indicator=(CirclePageIndicator)findViewById(R.id.indicator);
indicator.setStrokeWidth(0);
indicator.setPageCircleColor(0xFFFFFFFF);
viewpager.setAdapter(newMyadapter(this));
indicator.setViewPager(viewpager);
indicator.setOnPageChangeListener(this);
}
@Override
publicvoidonPageScrollStateChanged(intarg0){
}
@Override
publicvoidonPageScrolled(intarg0,floatarg1,intarg2){
}
@Override
publicvoidonPageSelected(intarg0){
}}二、廣告適配器packagecom.example.t;importandroid.content.Context;importandroid.support.v4.view.PagerAdapter;importandroid.support.v4.view.ViewPager.LayoutParams;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.ImageView;importandroid.widget.ImageView.ScaleType;publicclassMyadapterextendsPagerAdapter{
privateContextcontext;
privateint[]p_w_picpaths;
publicMyadapter(Contextcontext){
this.context=context;
p_w_picpaths=newint[]{R.drawable.default_img_mid,R.drawable.default_img_mid,R.drawable.default_img_mid};
}
@Override
publicintgetCount(){
returnp_w_picpaths.length;
}
//判斷View和Object是否為同一個(gè)View
@Override
publicbooleanisViewFromObject(Viewview,Objectobject){
returnview==object;
}
//控制當(dāng)某個(gè)View不需要的時(shí)候的回收處理
@Override
publicvoiddestroyItem(ViewGroupcontainer,intposition,Objectobject){
container.removeView((View)object);
}
//實(shí)例化Item:即得到每個(gè)View
@Override
publicObjectinstantiateItem(ViewGroupcontainer,intposition){
ImageViewiv=newImageView(context);
iv.setScaleType(ScaleType.CENTER_CROP);
android.view.ViewGroup.LayoutParamsparams=newandroid.view.ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
iv.setLayoutParams(params);
iv.setImageResource(p_w_picpaths[position]);
container.addView(iv);
returniv;
}}三、小圓點(diǎn)指示器packagecom.example.t;importstaticandroid.graphics.Paint.ANTI_ALIAS_FLAG;importstaticandroid.widget.LinearLayout.HORIZONTAL;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.Paint.Style;importandroid.os.Parcel;importandroid.os.Parcelable;importandroid.support.v4.view.MotionEventCompat;importandroid.support.v4.view.ViewConfigurationCompat;importandroid.support.v4.view.ViewPager;importandroid.support.v4.view.ViewPager.OnPageChangeListener;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewConfiguration;/**
*@describeviewpager的小圓點(diǎn)指示器
*/publicclassCirclePageIndicatorextendsViewimplementsOnPageChangeListener{
privatestaticfinalintINVALID_POINTER=-1;
privatefloatmRadius;
privatefinalPaintmPaintPageFill=newPaint(ANTI_ALIAS_FLAG);
privatefinalPaintmPaintStroke=newPaint(ANTI_ALIAS_FLAG);
privatefinalPaintmPaintFill=newPaint(ANTI_ALIAS_FLAG);
privateViewPagermViewPager;
privateViewPager.OnPageChangeListenermListener;
privateintmCurrentPage;
privateintmSnapPage;
privatefloatmPageOffset;
privateintmScrollState;
privateintmOrientation;
privatebooleanmCentered;
privatebooleanmSnap;
privateintmTouchSlop;
privatefloatmLastMotionX=-1;
privateintmActivePointerId=INVALID_POINTER;
privatebooleanmIsDragging;
privateContextcontext;
publicCirclePageIndicator(Contextcontext){
super(context);
this.context=context;
init(context);
}
publicCirclePageIndicator(Contextcontext,AttributeSetattrs){
super(context,attrs);
init(context);
}
privatevoidinit(Contextcontext){
mCentered=true;
mOrientation=0;
mPaintPageFill.setStyle(Style.FILL);
mPaintPageFill.setColor(0x00000000);
mPaintStroke.setStyle(Style.STROKE);
mPaintStroke.setColor(0xFFFF6600);
mPaintStroke.setStrokeWidth(getResources().getDimension(R.dimen.indicator_stroke_width));
mPaintFill.setStyle(Style.FILL);
mPaintFill.setColor(0xFFFF6600);
mRadius=getResources().getDimension(R.dimen.indicator_radius);
mSnap=false;
finalViewConfigurationconfiguration=ViewConfiguration.get(context);
mTouchSlop=ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
}
publicvoidsetRadius(intdpValue){
floatscale=context.getResources().getDisplayMetrics().density;
mRadius=(int)(dpValue*scale+0.5f);
invalidate();
}
publicfloatfraction;
privatefloatparseRadiusIfNeed(floatpageFillRadius){
returnfraction==0?pageFillRadius:pageFillRadius+pageFillRadius/fraction;
}
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
if(mViewPager==null){
return;
}
finalintcount=mViewPager.getAdapter().getCount();
if(count==0){
return;
}
if(mCurrentPage>=count){
setCurrentItem(count-1);
return;
}
intlongSize;
intlongPaddingBefore;
intlongPaddingAfter;
intshortPaddingBefore;
if(mOrientation==HORIZONTAL){
longSize=getWidth();
longPaddingBefore=getPaddingLeft();
longPaddingAfter=getPaddingRight();
shortPaddingBefore=getPaddingTop();
}else{
longSize=getHeight();
longPaddingBefore=getPaddingTop();
longPaddingAfter=getPaddingBottom();
shortPaddingBefore=getPaddingLeft();
}
finalfloatthreeRadius=mRadius*5;
finalfloatshortOffset=shortPaddingBefore+mRadius;
floatlongOffset=longPaddingBefore+mRadius;
if(mCentered){
longOffset+=((longSize-longPaddingBefore-longPaddingAfter)/2.0f)-((count*threeRadius)/2.0f);
}
floatdX;
floatdY;
floatpageFillRadius=mRadius;
if(mPaintStroke.getStrokeWidth()>0){
pageFillRadius-=mPaintStroke.getStrokeWidth()*1.5f;//實(shí)心圓的半徑
}
//Drawstrokedcircles
for(intiLoop=0;iLoop<count;iLoop++){
floatdrawLong=longOffset+(iLoop*threeRadius);
if(mOrientation==HORIZONTAL){
dX=drawLong;
dY=shortOffset;
}else{
dX=shortOffset;
dY=drawLong;
}
//Onlypaintfillifnotcompletelytransparent
if(mPaintPageFill.getAlpha()>0){
canvas.drawCircle(dX,dY,pageFillRadius,mPaintPageFill);
}
//Onlypaintstrokeifastrokewidthwasnon-zero
if(pageFillRadius!=mRadius){
canvas.drawCircle(dX,dY,mRadius,mPaintStroke);
}
}
//Drawthefilledcircleaccordingtothecurrentscroll
floatcx=(mSnap?mSnapPage:mCurrentPage)*threeRadius;
if(!mSnap){
cx+=mPageOffset*threeRadius;
}
if(mOrientation==HORIZONTAL){
dX=longOffset+cx;
dY=shortOffset;
}else{
dX=shortOffset;
dY=longOffset+cx;
}
canvas.drawCircle(dX,dY,parseRadiusIfNeed(pageFillRadius),mPaintFill);
}
publicbooleanonTouchEvent(android.view.MotionEventev){
if(super.onTouchEvent(ev)){
returntrue;
}
if((mViewPager==null)||(mViewPager.getAdapter().getCount()==0)){
returnfalse;
}
finalintaction=ev.getAction()&MotionEventCompat.ACTION_MASK;
switch(action){
caseMotionEvent.ACTION_DOWN:
mActivePointerId=MotionEventCompat.getPointerId(ev,0);
mLastMotionX=ev.getX();
break;
caseMotionEvent.ACTION_MOVE:{
finalintactivePointerIndex=MotionEventCompat.findPointerIndex(ev,mActivePointerId);
finalfloatx=MotionEventCompat.getX(ev,activePointerIndex);
finalfloatdeltaX=x-mLastMotionX;
if(!mIsDragging){
if(Math.abs(deltaX)>mTouchSlop){
mIsDragging=true;
}
}
if(mIsDragging){
mLastMotionX=x;
if(mViewPager.isFakeDragging()||mViewPager.beginFakeDrag()){
mViewPager.fakeDragBy(deltaX);
}
}
break;
}
caseMotionEvent.ACTION_CANCEL:
caseMotionEvent.ACTION_UP:
if(!mIsDragging){
finalintcount=mViewPager.getAdapter().getCount();
finalintwidth=getWidth();
finalfloathalfWidth=width/2f;
finalfloatsixthWidth=width/6f;
if((mCurrentPage>0)&&(ev.getX()<halfWidth-sixthWidth)){
if(action!=MotionEvent.ACTION_CANCEL){
mViewPager.setCurrentItem(mCurrent1);
}
returntrue;
}elseif((mCurrentPage<count-1)&&(ev.getX()>halfWidth+sixthWidth)){
if(action!=MotionEvent.ACTION_CANCEL){
mViewPager.setCurrentItem(mCurrentPage+1);
}
returntrue;
}
}
mIsDragging=false;
mActivePointerId=INVALID_POINTER;
if(mViewPager.isFakeDragging())
mViewPager.endFakeDrag();
break;
caseMotionEventCompat.ACTION_POINTER_DOWN:{
finalintindex=MotionEventCompat.getActionIndex(ev);
mLastMotionX=MotionEventCompat.getX(ev,index);
mActivePointerId=MotionEventCompat.getPointerId(ev,index);
break;
}
caseMotionEventCompat.ACTION_POINTER_UP:
finalintpointerIndex=MotionEventCompat.getActionIndex(ev);
finalintpointerId=MotionEventCompat.getPointerId(ev,pointerIndex);
if(pointerId==mActivePointerId){
finalintnewPointerIndex=pointerIndex==0?1:0;
mActivePointerId=MotionEventCompat.getPointerId(ev,newPointerIndex);
}
mLastMotionX=MotionEventCompat.getX(ev,MotionEventCompat.findPointerIndex(ev,mActivePointerId));
break;
}
returntrue;
}
publicvoidsetViewPager(ViewPagerview){
if(mViewPager==view){
return;
}
if(mViewPager!=null){
mViewPager.setOnPageChangeListener(null);
}
if(view.getAdapter()==null){
thrownewIllegalStateException("ViewPagerdoesnothaveadapterinstance.");
}
mViewPager=view;
mViewPager.setOnPageChangeListener(this);
invalidate();
}
publicvoidsetViewPager(ViewPagerview,intinitialPosition){
setViewPager(view);
setCurrentItem(initialPosition);
}
publicvoidsetCurrentItem(intitem){
if(mViewPager==null){
thrownewIllegalStateException("ViewPagerhasnotbeenbound.");
}
mViewPager.setCurrentItem(item);
mCurrentPage=item;
invalidate();
}
publicvoidnotifyDataSetChanged(){
invalidate();
}
@Override
publicvoidonPageScrollStateChanged(intstate){
mScrollState=state;
if(mListener!=null){
mListener.onPageScrollStateChanged(state);
}
}
@Override
publicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels){
mCurrentPage=position;
mPageOffset=positionOffset;
invalidate();
if(mListener!=null){
mListener.onPageScrolled(position,positionOffset,positionOffsetPixels);
}
}
@Override
publicvoidonPageSelected(intposition){
if(mSnap||mScrollState==ViewPager.SCROLL_STATE_IDLE){
mCurrentPage=position;
mSnapPage=position;
invalidate();
}
if(mListener!=null){
mListener.onPageSelected(position);
}
}
publicvoidsetOnPageChangeListener(ViewPager.OnPageChangeListenerlistener){
mListener=listener;
}
/**
*設(shè)置圓邊框的顏色
*/
publicvoidsetStrokeColor(intcolor){
try{
mPaintStroke.setColor(color);
}catch(Exceptione){
mPaintStroke.setColor(0xFFFF6600);
e.printStackTrace();
}
}
/**
*設(shè)置中心圓的顏色
*
*@paramcolor
*/
publicvoidsetCircleColor(intcolor){
try{
mPaintFill.setColor(color);
}catch(Exceptione){
mPaintFill.setColor(0xFFFF6600);
e.printStackTrace();
}
}
publicvoidsetPageCircleColor(intcolor){
try{
mPaintPageFill.setColor(color);
}catch(Exceptione){
mPaintPageFill.setColor(0x00000000);
e.printStackTrace();
}
}
publicvoidsetStrokeWidth(floatwidth){
mPaintStroke.setStrokeWidth(width);
}
/*
*(non-Javadoc)
*
*@seeandroid.view.View#onMeasure(int,int)
*/
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
if(mOrientation==HORIZONTAL){
setMeasuredDimension(measureLong(widthMeasureSpec),measureShort(heightMeasureSpec));
}else{
setMeasuredDimension(measureShort(widthMeasureSpec),measureLong(heightMeasureSpec));
}
}
/**
*Determinesthewidthofthisview
*
*@parammeasureSpec
*
AmeasureSpecpackedintoanint
*@returnThewidthoftheview,honoringconstraintsfrommeasureSpec
*/
privateintmeasureLong(intmeasureSpec){
intresult;
intspecMode=MeasureSpec.getMode(measureSpec);
intspecSize=MeasureSpec.getSize(measureSpec);
if((specMode==MeasureSpec.EXACTLY)||(mViewPager==null)){
//Weweretoldhowbigtobe
result=specSize;
}else{
//Calculatethewidthaccordingtheviewscount
finalintcount=mViewPager.getAdapter().getCount();
result=(int)(getPaddingLeft()+getPaddingRight()+(count*2*mRadius)+(count-1)*mRadius*4+1);
//RespectAT_MOSTvalueifthatwaswhatiscalledforbymeasureSpec
if(specMode==MeasureSpec.AT_MOST){
result=Math.min(result,specSize);
}
}
returnresult;
}
/**
*Determinestheheightofthisview
*
*@parammeasureSpec
*
AmeasureSpecpackedintoanint
*@returnTheheightoftheview,honoringconstraintsfrommeasureSpec
*/
privateintmeasureShort(intmeasureSpec){
intresult;
intspecMode=MeasureSpec.getMode(measureSpec);
intspecSize=MeasureSpec.getSize(measureSpec);
if(specMode==MeasureSpec.EXACTLY){
//Weweretoldhowbigtobe
result=specSize;
}else{
//Measuretheheight
result=(int)(2*mRadius+getPaddingTop()+getPaddingBottom()+1);
//RespectAT_MOSTvalueifthatwaswhatiscalledforbymeasureSpec
if(specMode==MeasureSpec.AT_MOST){
result=Math.min(result,specSize);
}
}
returnresult;
}
@Override
publicvoidonRestoreInstanceState(Parcelablestate){
SavedStatesavedState=(SavedState)state;
super.onRestoreInstanceState(savedState.getSuperState());
mCurrentPage=savedState.currentPage;
mSnapPage=savedState.currentPage;
requestLayout();
}
@Override
publicParcelableonSaveInstanceState()
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 美工高級(jí)課程設(shè)計(jì)
- 紙質(zhì)文物修復(fù)課程設(shè)計(jì)
- 2024年貨物買賣合同(國際貿(mào)易版)
- 8與9的互動(dòng):分合樂趣多
- 2024房地產(chǎn)開發(fā)代理合作的協(xié)議書
- 2025版物流快遞產(chǎn)業(yè)股權(quán)轉(zhuǎn)讓協(xié)議2篇
- 二零二五年度新能源風(fēng)力發(fā)電場建設(shè)勞務(wù)合同示范2篇
- 2024年跨界對接:全權(quán)代理中介合同3篇
- 2025年度網(wǎng)絡(luò)安全風(fēng)險(xiǎn)評估與管理合同范本3篇
- 二零二五年工廠空調(diào)系統(tǒng)維修保養(yǎng)及改造合同3篇
- 2023年軟件主管年終業(yè)務(wù)工作總結(jié)
- 你比我猜成語
- 2023版設(shè)備管理體系標(biāo)準(zhǔn)
- 外來人員車輛入廠安全須知
- 01S201室外消火栓安裝圖集
- 浪潮入職培訓(xùn)考試題目
- B2C網(wǎng)絡(luò)商店物流服務(wù)質(zhì)量及其與顧客忠誠的關(guān)系研究的中期報(bào)告
- 年同等學(xué)歷考研申碩綜合科復(fù)習(xí)指導(dǎo)(教育心理學(xué))課件
- 線段角動(dòng)點(diǎn)問題
- 劍橋英語PET真題校園版
- 【課件】沉心靜氣打贏最后一仗 課件-2022-2023學(xué)年高中主題班會(huì)
評論
0/150
提交評論