本文实例讲述了AndroID开发实现自定义水平滚动的容器。分享给大家供大家参考,具体如下:
public class horizontalscrollview extends VIEwGroup { //手势 private GestureDetector mGestureDetector; private HorizontalScroller mScroller; private int curID; //快速滑动 private boolean isFlying; //--回调函数------------------------------------- private Onchangelistener mListener; public voID setonchangelistener(Onchangelistener Listener) { if (Listener != null) { mListener = Listener; } } public interface Onchangelistener{ voID move2dest(int curID); } public horizontalscrollview(Context context) { this(context,null); } public horizontalscrollview(Context context,AttributeSet attrs) { this(context,attrs,0); } public horizontalscrollview(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); mScroller = new HorizontalScroller(); isFlying = false; initGesture(context); } @OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { // 模向移动, for (int i = 0; i < getChildCount(); i++) { VIEw vIEw = getChildAt(i); //给水平方向的每个vIEw定位 vIEw.layout(i * getWIDth(),getWIDth() + i * getWIDth(),getHeight()); } } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { for (int i = 0; i < getChildCount(); i++) { VIEw vIEw = getChildAt(i); vIEw.measure(wIDthMeasureSpec,heightmeasureSpec); } super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { mGestureDetector.ontouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (!isFlying) { move2dest(); } isFlying = false; break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; default: break; } return true; } public voID move2dest() { // int destID = (getScrollX() + getWIDth() / 2) / getWIDth(); move2dest(destID); } public voID move2dest(int destID) { curID = destID; if (destID > getChildCount() - 1) { destID = getChildCount() - 1; } if (mListener != null) { mListener.move2dest(curID); } int distance = (int) (destID * getWIDth() - getScrollX()); // scrollBy(distance,0); mScroller.startScroll(getScrollX(),getScrollY(),distance,0); invalIDate(); } /** * invalIDate()此方法会触发下面的方法 */ @OverrIDe public voID computeScroll() { // 如果存在偏移,就不断刷新 if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(),0); invalIDate(); } super.computeScroll(); } private voID initGesture(Context context) { mGestureDetector = new GestureDetector(context,new OnGestureListener() { @OverrIDe public boolean onSingleTapUp(MotionEvent e) { return false; } @OverrIDe public voID onShowPress(MotionEvent e) { } @OverrIDe public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) { scrollBy((int) distanceX,0); return false; } @OverrIDe public voID onLongPress(MotionEvent e) { } @OverrIDe /** * 快速滑动时 */ public boolean onFling(MotionEvent e1,float veLocityX,float veLocityY) { isFlying = true; if (curID > 0 && veLocityX > 0) {// 表示向左移 move2dest(curID - 1); } else if (curID < getChildCount() && veLocityX < 0) { move2dest(curID + 1);// 向右 } else { move2dest();// 移到原位 } return false; } @OverrIDe public boolean onDown(MotionEvent e) { return false; } }); }}
/** * 位移计算工具类 * * @author chenlin * */public class HorizontalScroller { private int startX; private int startY; private int distanceX; private int distanceY; private int currentX; private int currentY; private long startTime; private long duration = 1000L; private boolean isFinish; /** * * @param scrollX * x坐标 * @param scrollY * y坐标 * @param distanceX * X方向移动的距离 * @param distanceY * y方向移动的距离 */ public voID startScroll(int scrollX,int scrollY,int distanceX,int distanceY) { startX = scrollX; startY = scrollY; this.distanceX = distanceX; this.distanceY = distanceY; isFinish = false; startTime = SystemClock.uptimeMillis(); } /** * 计算偏移量, * * @return true 还在移动 false:移动已经停止 */ public boolean computeScrollOffset() { if (isFinish) { return false; } long timePassed = SystemClock.uptimeMillis() - startTime; if (timePassed < duration) { currentX = (int) (startX + distanceX * timePassed / duration); currentY = (int) (startY + distanceY * timePassed / duration); System.out.println("currentX:::" + currentX); } else if (timePassed >= duration) { currentX = startX + distanceX; currentY = startY + distanceY; isFinish = true; } return true; } public int getCurrX() { return currentX; } public voID setCurrentX(int currentX) { this.currentX = currentX; } public int getCurrentY() { return currentY; } public voID setCurrentY(int currentY) { this.currentY = currentY; }}
使用方法
public class ScrollActivity extends Activity implements OnCheckedchangelistener,Onchangelistener { private int[] IDs = { R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6 }; private horizontalscrollview mVIEw; private linearLayout mLayout; private RadioGroup mGroup; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { requestwindowFeature(Window.FEATURE_NO_Title); super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_myscrollvIEw); init(); } private voID init() { mLayout = (linearLayout) findVIEwByID(R.ID.body_layout); mGroup = (RadioGroup) findVIEwByID(R.ID.radiogroup); mVIEw = new horizontalscrollview(this); for (int i = 0; i < IDs.length; i++) { ImageVIEw imageVIEw = new ImageVIEw(this); imageVIEw.setBackgroundResource(IDs[i]); mVIEw.addVIEw(imageVIEw); } mLayout.addVIEw(mVIEw); // 随便添加一个vIEw VIEw vIEw = getLayoutInflater().inflate(R.layout.activity_progressvIEw,null); mVIEw.addVIEw(vIEw,3); for (int i = 0; i < mVIEw.getChildCount(); i++) { Radiobutton radiobutton = new Radiobutton(this); // 设置ID的编号 radiobutton.setID(i); mGroup.setorIEntation(linearLayout.HORIZONTAL); mGroup.addVIEw(radiobutton); if (i == 0) { radiobutton.setChecked(true); } } mGroup.setonCheckedchangelistener(this); mVIEw.setonchangelistener(this); } @OverrIDe public voID onCheckedChanged(RadioGroup group,int checkedID) { mVIEw.move2dest(checkedID); } @OverrIDe public voID move2dest(int curID) { Radiobutton radiobutton = (Radiobutton) mGroup.getChildAt(curID); radiobutton.setChecked(true); }}
布局文件
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <RadioGroup androID:ID="@+ID/radiogroup" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > </RadioGroup> <linearLayout androID:ID="@+ID/body_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > </linearLayout></linearLayout>
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity *** 作技巧总结》、《Android资源 *** 作技巧汇总》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android开发实现自定义水平滚动的容器示例全部内容,希望文章能够帮你解决Android开发实现自定义水平滚动的容器示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)