这个库在「华夏万象」Android 版开发过程中产出,仅用做查阅参考、不适宜工业应用。
目录
- MarqueeDrawable 跑马灯进度条 drawable
- StarrySky 星空 drawable
- ChinaMapView 中国地图带手势版本
- MenuItemView 带底部菜单的View
- ParallaxRelativeLayout 纵向的视差滚动布局
- SegmentProgressBar 分段、可拖动的进度条
- VerticalTextView 纵向 TextView
- ShapedImageView 可管制宽高比的 ImageView
- com.antiless.huaxia.widget.gesture 一个易于扩大的手势检测框架
用法
1. MarqueeDrawable
<code class="kotlin">/** * 跑马灯进度条 drawable * @param width 冀望宽度 Pixel * @param height 冀望高度 Pixel * @param perWidth 跑马灯每段宽度 Pixel */ class MarqueeDrawable(val width: Int, val height: Int, val perWidth: Int) : Drawable(), Animatable val marqueeDrawable = MarqueeDrawable(width, height, perWidth) ImageView.setImageDrawable(marqueeDrawable) marqueeDrawable.progress = 50
2. StarrySky
星空 Drawable
<code class="kotlin">/** * 星空 drawable * @param widthPixels 宽度 * @param heightPixels 高度 */ class StarrySky(val widthPixels: Int, val heightPixels: Int) : Drawable(), Animatable val starrySky = StarrySky(widthPixels, heightPixels).apply { for (i in 0 until 50) { // 增加 50 个随机地位的星星 addRandomStar() } // 监听星星跑出范畴 setOnStarOutListener { removeStar(it) addRandomStar() } } ImageView.setImageDrawable(starrySky) // 开始静止 starrySky.start() // 进行静止 starrySky.stop()
- ChinaMapView
中国地图带手势版本
这个控件基于ChinaMapView
退出了双指操作手势,双指基于第 9 项的手势检测框架
<code class="xml"><com.antiless.huaxia.widget.chinamap.ChinaMapView android:id="@+id/itemMap" android:layout_width="match_parent" android:layout_height="match_parent"/>
<code class="java">/** * 增加不可选区域 */ void addUnSelectableArea(Area area) /** * 设置选中监听 */ void setOnProvinceSelectedListener(OnProvinceSelectedListener pOnProvinceSelectedListener) /** * 设置空白处双击监听 */ void setOnProvinceDoubleClickListener(OnProvinceDoubleClickListener onDoubleClickListener) /** * 设置区域绘制 paint.style 是否为 Paint.Style.FILL */ void setPaintColor(Area pArea, int color, boolean isFull)
- MenuItemView
带底部菜单的View, 手势左滑展现底部菜单
<code class="xml"><com.antiless.huaxia.widget.MenuItemView android:id="@+id/menuItemView" android:layout_width="match_parent" android:layout_height="wrap_content" app:backViewId="@id/itemMenu" app:coverViewId="@id/itemCover" app:dragEnable="true"> <View android:id="@+id/itemMenu"/> <View android:id="@+id/itemOver"/> </com.antiless.huaxia.widget.MenuItemView>
<code class="kotlin">fun showBackView() fun resetBackView() fun isBackShowed(): Boolean
- ParallaxRelativeLayout
纵向的视差滚动布局
只有一个参数layout_parallax_speed
,值为1时失常速度滚动,值 0~1 时小于失常速度滚动,>1 时大于失常速度滚动
<code class="xml"><ScrollView> <com.antiless.huaxia.widget.ParallaxRelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View app:layout_parallax_speed="0.6"/> <View app:layout_parallax_speed="1"/> <View app:layout_parallax_speed="1.6"/> </com.antiless.huaxia.widget.ParallaxRelativeLayout> </ScrollView>
- SegmentProgressBar
分段、可拖动的进度条
<code class="xml"><com.antiless.huaxia.widget.SegmentProgressBar android:id="@+id/segmentProgressBar" android:layout_width="match_parent" android:layout_height="wrap_content" />
<code class="kotlin">maxSegment 最大值 minSegment 最小值 currentSegment 以后值 showBubble() 显示气泡 hideBubble() 暗藏气泡 bubbleAnimating 气泡是否在动画中
- VerticalTextView
纵向 TextView
<code class="xml"><com.antiless.huaxia.widget.VerticalTextView android:text="xxx" android:textSize="xxx" android:textColor="xxx" android:height="textStyle" android:letterSpace="height" android:textStyle="letterSpace" /> <declare-styleable name="VerticalTextView"> <attr name="text"/> <attr name="textSize"/> <attr name="textColor"/> <attr name="height" format="dimension"/> <attr name="letterSpace" format="float"/> <attr name="textStyle"/> </declare-styleable>
- ShapedImageView
可管制宽高比的 ImageView
<code class="xml"><com.antiless.huaxia.widget.ShapedImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" app:base="widthBased" app:heightWeight="1" app:radius="4dp" app:widthWeight="1" /> <declare-styleable name="ShapedImageView"> <attr name="widthWeight" format="integer"/> <attr name="heightWeight" format="integer"/> <attr name="radius" format="dimension"/> <attr name="strokeColor" format="color"/> <attr name="strokeWidth" format="dimension"/> <attr name="base"> <enum name="heightBased" value="0" /> <enum name="widthBased" value="1" /> </attr> </declare-styleable>
- com.antiless.huaxia.widget.gesture
一个易于扩大的手势检测框架
应用办法很简略:
<code class="java">DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); GesturePointersUtility utility = new GesturePointersUtility(dm); transformSystem = new TransformSystem(dm, utility); TransformNode node = new TransformNode(this, transformSystem); // 内置了单指滑动、双指pinch、双指拖动、双指twist手势辨认 // 开发者可自定义手势辨认逻辑,注册到TransformSystem中 // 而后实现BaseTransformationController, 将手势辨认的后果反馈到指定的对象上 // 最初在 TransformNode 中注册本人实现的 Controller
1. 实现 controller
Controller 用于将手势行为的后果利用到 View 上
ScaleController 是将pinch手势的后果用来缩放
<code class="kotlin">class ScaleController(transformNode: BaseTransformNode, recognizer: PinchGestureRecognizer) : BaseTransformationController<PinchGesture>(transformNode, recognizer) { override fun canStartTransformation(gesture: PinchGesture): Boolean { return true } var lastRatio = 1f override fun onContinueTransformation(gesture: PinchGesture) { if (transformNode.view is ChinaMapView) { val pinchRatio = gesture.gap / gesture.startGap val startCenterPoint = gesture.startPosition1.center(gesture.startPosition2) val scale = pinchRatio / lastRatio lastRatio = pinchRatio transformNode.view.scale(scale, startCenterPoint.x, startCenterPoint.y) } } override fun onEndTransformation(gesture: PinchGesture) { lastRatio = 1f } }
2. 注册 controller
申明须要应用的 controller
该例中:
双指 pinch 缩放
双指滑动进行拖动
双指 twist 进行立体旋转
单指滑动旋转 3d 视角
<code class="kotlin">class TransformNode(view: View, transformSystem: TransformSystem) : BaseTransformNode(view, transformSystem) { private val scaleController: ScaleController = ScaleController(this, transformSystem.pinchGestureRecognizer) private val dragController: DragController = DragController(this, transformSystem.doubleFingerMoveGestureRecognizer) private val rotateController: RotateController = RotateController(this, transformSystem.twistGestureRecognizer) private val visualController: VisualController = VisualController(this, transformSystem.swipeGestureRecognizer) init { addTransformationController(dragController) addTransformationController(scaleController) addTransformationController(rotateController) addTransformationController(visualController) } }
3. 自定义你的手势识别器
- 实现
BaseGesture
和BaseGestureRecognizer
- 应用
TransformSystem.addGestureRecognizer()
注册YourRecognizer
- 实现
BaseTransformationController<YourGesture>(TransformNode, YourRecognizer)
- 应用
TransformNode.addTransformationController
注册YourController
4. 在须要应用手势辨认的中央调用
<code class="java">DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); GesturePointersUtility utility = new GesturePointersUtility(dm); transformSystem = new TransformSystem(dm, utility); TransformNode node = new TransformNode(this, transformSystem);
欢迎关注搞代码gaodaima网的主页
Github源码
Github主页
我的博客