




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】banner廣告及viewpager的小圓點指示器
一、主界面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是否為同一個View
@Override
publicbooleanisViewFromObject(Viewview,Objectobject){
returnview==object;
}
//控制當某個View不需要的時候的回收處理
@Override
publicvoiddestroyItem(ViewGroupcontainer,intposition,Objectobject){
container.removeView((View)object);
}
//實例化Item:即得到每個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;
}}三、小圓點指示器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的小圓點指示器
*/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;//實心圓的半徑
}
//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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 石樓縣2025年數(shù)學四下期末監(jiān)測試題含解析
- 江蘇省宿遷市泗洪縣2025屆四年級數(shù)學第二學期期末聯(lián)考試題含解析
- 江蘇省常州市武進區(qū)禮嘉中學2025屆高三5月模擬(三模)語文試題含解析
- 酒泉市金塔縣2025年數(shù)學三下期末聯(lián)考模擬試題含解析
- 個人購房合同樣本
- 智慧農(nóng)業(yè)技術(shù)-提升農(nóng)村人居環(huán)境質(zhì)量
- 大型超市收銀員勞動合同書
- 遼寧省遼陽市燈塔市2019-2020學年八年級上學期期末物理試題【含答案】
- 勞動合同續(xù)簽意向協(xié)議書范本
- 個人產(chǎn)品分銷合同
- IrreguLar-Verbs-不規(guī)則動詞表
- 蔬菜水果配送投標方案(技術(shù)方案)
- (2023版)《結(jié)腸鏡檢查腸道準備專家共識意見》解讀課件
- 學院物資倉庫管理員崗位職責
- 砼工程技術(shù)交底
- 煤礦污水處理站制度(完整版)資料
- 光伏儲能一體化充電站設(shè)計方案
- 康復(fù)醫(yī)學科全新筆記匯總
- 劍橋英語PET真題校園版
- 土方開挖及基坑支護工程安全監(jiān)理實施細則
- 土木工程施工現(xiàn)場安全控制措施
評論
0/150
提交評論