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

本文介绍了如何在Android中创建一个自定义的滚动刻度尺视图,利用ScrollView和LinearLayout结合,通过添加小view并处理手势来实现。提供了SlidingRuler的GitHub源码链接和最终实现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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() {

			@Override
			public 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);

	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值