二、设计思路:
1、自定义view,继承vIEwGroup类
2、求出子视图的个数,子视图高度,子视图宽度。假设轮播图的每一张图片大小相等根据第一个子视图的宽度和子视图的个数求出并设置vIEwGroup的宽高。
3、在onLayout方法中动态设置vIEwGroup的布局(绘制)。
4、事件设置:让onIntercepttouchEvent返回true,让ontouchEvent来处理事件,按下、移动、抬起需要做的事情。
5、使用Scroller来完成自动轮播。(当按下没有移动时停止自动轮播,抬起是重新开始轮播)。
6、通过一个接口实现单击事件和参数传递(用一个布尔变量表示是否是单击事件在按下时设置为true,移动时设置为false,在抬起时判断是否需要处理单击事件)。
三、代码自定义的VIEwGroup:
package com.me.androIDstudy2.vIEw;import androID.annotation.Suppresslint;import androID.content.Context;import androID.os.Handler;import androID.os.Message;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Scroller;import androIDx.annotation.NonNull;import com.me.androIDstudy2.jk.imgVIEwGroupLister;import java.util.Timer;import java.util.TimerTask;public class imgVIEwGroup extends VIEwGroup { private int child; //子视图个数 private int chilDWIDth; // 子视图宽 private int childHeight; //子视图高 private int x; private int index = 0; private Scroller scroller; private Timer timer = new Timer(); private TimerTask timerTask ; private boolean isAtuo = true; //默认开启自动轮播 private imgVIEwGroupLister Lister ; private boolean isClick; public imgVIEwGroupLister getLister() { return Lister; } public voID setLister(imgVIEwGroupLister Lister) { this.Lister = Lister; } @Suppresslint("HandlerLeak") private Handler handler = new Handler(){ @OverrIDe public voID handleMessage(@NonNull Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: if (++index >= child ){ //到达最后一张图时,从初开始 index = 0; } scrollTo(chilDWIDth * index,0); break; } } }; public imgVIEwGroup(Context context) { super(context); InitObj(); } public imgVIEwGroup(Context context, AttributeSet attrs) { super(context, attrs); InitObj(); } public imgVIEwGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); InitObj(); } public voID InitObj(){ scroller = new Scroller(getContext()); timerTask = new TimerTask() { @OverrIDe public voID run() { if(isAtuo){ handler.sendEmptyMessage(0); } } }; timer.schedule(timerTask,100,2000); } @OverrIDe public voID computeScroll() { super.computeScroll(); if (scroller.computeScrollOffset()){ scrollTo(scroller.getCurrX(),0); invalIDate(); } } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec, heightmeasureSpec); //1、求出子视图的个数 child = getChildCount(); if (child == 0){ setMeasuredDimension(0,0); }else{ measureChildren(wIDthMeasureSpec,heightmeasureSpec); VIEw vIEw = getChildAt(0); childHeight = vIEw.getMeasuredHeight(); chilDWIDth = vIEw.getMeasureDWIDth(); int wIDth = chilDWIDth * child; int height = childHeight; //设置vIEwGroup的高度和宽度 setMeasuredDimension(wIDth,height); } } @OverrIDe protected voID onLayout(boolean changed, int l, int t, int r, int b) { if (changed){ int left = 0; for (int i = 0; i < child; i++) { VIEw vIEw = getChildAt(i); vIEw.layout(left, 0, left+chilDWIDth, childHeight); left += chilDWIDth; } } } /** * 事件的传递 * 事件拦截 : */ @OverrIDe public boolean onIntercepttouchEvent(MotionEvent ev) { return true; } @OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //按下 isAtuo = false; if (!scroller.isFinished()){ scroller.abortAnimation(); } x = (int) event.getX(); isClick = true; break; case MotionEvent.ACTION_MOVE: //移动 int moveX = (int) event.getX(); int d = moveX - x; scrollBy(-d,0); x = moveX; isClick = false; break; case MotionEvent.ACTION_UP: //抬起 int scrollX = getScrollX(); index = (scrollX + chilDWIDth / 2) /chilDWIDth; //求下标,四舍五入 Log.e("xw",index + ""); if(index < 0){ //最左边 index = 0; }else if(index > child - 1){ //最右边 index = child - 1; } if (isClick){ Lister.clickimg("http://u" + index + ".PHP"); }else{ // scrollTo(index * chilDWIDth,0); int dx = index * chilDWIDth -scrollX; scroller.startScroll(scrollX,0,dx,0); postInvalIDate(); } isAtuo = true; break; default: break; } return true; }}VIEw Code
单击事件的接口:
package com.me.androIDstudy2.jk;public interface imgVIEwGroupLister { voID clickimg(String url);}VIEw Code
active:
package com.me.androIDstudy2;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.ImageVIEw;import androID.Widget.Toast;import com.me.androIDstudy2.jk.imgVIEwGroupLister;import com.me.androIDstudy2.vIEw.imgVIEwGroup;public class MainActivity extends AppCompatActivity implements imgVIEwGroupLister { private imgVIEwGroup imgVIEwGroup ; public voID initVIEw(){ imgVIEwGroup = findVIEwByID(R.ID.img_vIEw_group); } public voID action(){ imgVIEwGroup.setLister(this); int [] IDs = new int[] { R.drawable.xw7, R.drawable.xw4, R.drawable.xw2 }; for (int i = 0; i < IDs.length; i++) { ImageVIEw img = new ImageVIEw(this); img.setBackgroundResource(IDs[i]); imgVIEwGroup.addVIEw(img); } } @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); action(); } @OverrIDe public voID clickimg(String url) { Toast.makeText(this,"url = " + url,Toast.LENGTH_SHORT).show(); }}VIEw Code
总结
以上是内存溢出为你收集整理的自定义view,实现轮播图及点击事件全部内容,希望文章能够帮你解决自定义view,实现轮播图及点击事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)