自定义view---滚动的刻度尺(三)

来源:互联网 发布:云计算 刘鹏 pdf 编辑:程序博客网 时间:2024/04/25 21:16

scrollview+ 里linearlayout

使用addview方法添加小view

手势不需要控制

控制过载的长度是第一个添加的view与最后一个添加的view

 

SlidingRuler

github源码地址


效果如下:



主要初始化源码:

private void init(Context context, final int totalCount, Drawable bigUnit,Drawable smallUnit, Drawable mask, Drawable background,float unitSize, int textSize, int betweenCount) {mContext = context;this.totalCount = totalCount;LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);root = new RelativeLayout(context);RelativeLayout.LayoutParams rootParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);root.setLayoutParams(rootParam);addView(root);mScrollView = new HorizontalScrollView(context);mScrollView.setVerticalScrollBarEnabled(false);mScrollView.setHorizontalScrollBarEnabled(false);root.addView(mScrollView);container = new LinearLayout(context);LinearLayout.LayoutParams containerParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);containerParam.gravity = Gravity.CENTER;container.setOrientation(LinearLayout.HORIZONTAL);container.setLayoutParams(containerParam);mScrollView.addView(container);DisplayMetrics displayMetrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);screen_Width = displayMetrics.widthPixels;for (int i = 0; i < totalCount; i++) {TextView text = new TextView(context);text.setTextSize(textSize);text.setGravity(Gravity.CENTER);LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);txtParams.setMargins(0, 0, 0, 0);text.setPadding(0, 0, 0, 0);if (unitSize != 0)text.setWidth(dp2px((int) unitSize));text.setLayoutParams(txtParams);Drawable drawable = null;if (i % betweenCount == 0) {drawable = getResources().getDrawable(R.drawable.bigunit);text.setText(String.valueOf(i));} else {drawable = getResources().getDrawable(R.drawable.smallunit);text.setText("");}drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());text.setCompoundDrawables(null, drawable, null, null);container.addView(text);}ImageView maskPic = new ImageView(context);maskPic.setImageDrawable(mask);RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.CENTER_HORIZONTAL);params.addRule(RelativeLayout.CENTER_IN_PARENT);params.addRule(RelativeLayout.ALIGN_PARENT_TOP);maskPic.setLayoutParams(params);mScrollView.setOnTouchListener(new TouchListenerImpl());mScrollView.post(new Runnable() {@Overridepublic void run() {//获取Linearlayout宽度final float totalLength = (float) mScrollView.getChildAt(0).getWidth();//计算每个添加的小view的宽度final float unitWidth = totalLength / totalCount;//取中央位置的宽度final float halfWidth = (float) mScrollView.getWidth() / 2;//刻度尺中央的值 = 初始中央的值*单元宽度 - view的一半的宽度 + 0.5*单元宽度(即滑动到中央标志的距离)mScrollView.smoothScrollTo((int) (defaultValue * unitWidth- halfWidth + 0.5 * unitWidth),(int) mScrollView.getScrollY());if (mSliderChangedListener != null)mSliderChangedListener.OnChanged(defaultValue);}});root.addView(maskPic);}



0 0
原创粉丝点击