1 Star 0 Fork 14

wingceltis-c / Tunlview

forked from wzyitspider / Tunlview 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

##一、前言 最近公司需要,需要一个视频播放条控件,可以将控件与时间对应起来,也可以对控件进行移动和缩放。(PS:公司是做摄像头的)移动到某个位置就播放该位置对应时间戳的视频录像,当然,并非任何时间点都有录像文件,所有录像的时间段用蓝色覆盖,代表该段时间可以进行录像回放,不在该蓝色区域内代表不能回放。最终完成效果如下

整体效果截图

这里写图片描述

缩放效果截图

缩放效果

滑动效果截图

滑动效果

##二、代码实现 我们需要一个自定义View,重写其onLayout方法,获取其宽高

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    mWidth = getWidth();
    mHeight = getHeight();
}

然后重写onDraw方法:分别绘制刻度,中间的红线(代表当前刻度尺滑动到的时间点),还有蓝色块(代表有录像的区域)。这里的思路是,我们把整个控件分为左右两块区域,由中间往两边绘制刻度。其主要代码:

  for (int i = 0; drawCount < width * 2; i++) {
        xPosition = (mWidth / 2 + mMove) + i * mDensity * (mLineDivider);
        if (((mValue + i)) % largeScaleValue == 0) {
            canvas.drawLine(xPosition, 0, xPosition, mDensity * ITEM_MAX_HEIGHT, linePaint);
            if ((((mValue + i)) / largeScaleValue % useString.length) < 0) {
                canvas.drawText(useString[useString.length + (((mValue + i)) / largeScaleValue % useString.length)], countLeftStart(mValue + i, xPosition, textWidth), getHeight() - textWidth, textPaint);
            } else {
                canvas.drawText(useString[((mValue + i)) / largeScaleValue % useString.length], countLeftStart(mValue + i, xPosition, textWidth), getHeight() - textWidth, textPaint);
            }

        } else {
            if ((mValue + i) % smallScaleValue == 0) {
                canvas.drawLine(xPosition, 0, xPosition, mDensity * ITEM_MIN_HEIGHT, linePaint);
            }
        }

        xPosition = (mWidth / 2 + mMove) - i * mDensity * (mLineDivider);
        if (( (mValue - i)) % largeScaleValue == 0) {
            canvas.drawLine(xPosition, 0, xPosition, mDensity * ITEM_MAX_HEIGHT, linePaint);
            if ((( (mValue - i)) / largeScaleValue % useString.length) < 0) {
                canvas.drawText(useString[useString.length + (((mValue - i)) / largeScaleValue % useString.length)], countLeftStart(mValue - i, xPosition, textWidth), getHeight() - textWidth, textPaint);
            } else {
                canvas.drawText(useString[( (mValue - i)) / largeScaleValue % useString.length], countLeftStart(mValue - i, xPosition, textWidth), getHeight() - textWidth, textPaint);
            }
        } else {
            if ( (mValue - i) % smallScaleValue == 0) {
                canvas.drawLine(xPosition, 0, xPosition, mDensity * ITEM_MIN_HEIGHT, linePaint);
            }
        }
        drawCount += mDensity * (mLineDivider);

    }

绘制中间的红线代码就很简单了,找到视图的中间,直接绘制

/**
 * 画中间的红色指示线
 *
 * @param canvas
 */
private void drawMiddleLine(Canvas canvas) {
    int indexWidth = 5;
    String color = "#66999999";
    Paint redPaint = new Paint();
    redPaint.setStrokeWidth(indexWidth);
    redPaint.setColor(Color.RED);
    canvas.drawLine(mWidth / 2, 0, mWidth / 2, mHeight, redPaint);
}

绘制代表有录像视频的蓝色区域

    public void drawShadow(Canvas canvas){
    float startXPosition ;
    float endXPosition ;
    // TODO: 2017/10/24  画阴影面积
    for (int j = 0; j < list.size(); j++) {
        RecordInfo info = list.get(j);
        int startvalue = info.getStartValue();
        int endvalue = info.getEndValue();

        //获取当前得value
        AXLog.e("wzytest","getNowValue:"+getNowValue() +"mvalue:"+mValue);

        startXPosition = mWidth/2 - (mValue-startvalue) * mDensity * (mLineDivider);
        endXPosition = mWidth/2 + (endvalue-mValue) * mDensity * (mLineDivider);

        canvas.drawRect(startXPosition,0,endXPosition,mHeight,shadowPaint);
    }
}

我们也要搞清楚时间与刻度之间的转换

/**
 * 获取时间戳所对应的mvalue
 *
 * @param time 录像时间段time
 * @return
 */
private int getmValue(long time) {
    /**
     *  从00:00:00开始设置时间
     */
    calendar1.set(Calendar.HOUR_OF_DAY, 0);
    calendar1.set(Calendar.MINUTE, 0);
    calendar1.set(Calendar.SECOND, 0);
    long l1 = calendar1.getTimeInMillis();
    return (int) (time - l1) / (1000 * valueToSencond);
}
/**
 * 获取mValue 所对应时间
 * @param mValue
 * @return
 */
public static String getTime(float mValue) {
    // TODO: 2017/10/23  超过24小时 和 少于0小时的处理

    int day = (int) (mValue *  valueToSencond / (3600 * 24));  // 天数
    int hour = (int) ((mValue *  valueToSencond - (60 * 60 * 24) * day) / 3600);
    int minute = (int) (mValue * valueToSencond - 3600 * hour - (60 * 60 * 24) * day) / 60;
    int second = (int) mValue * valueToSencond - hour * 3600 - minute * 60 - (60 * 60 * 24) * day;

    AXLog.e("wzytest", "hour:" + hour + " minute:" + minute + " second:" + second + " day:" + day);
    Calendar calendar1 = Calendar.getInstance();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     *  从00:00:00开始设置时间
     */
    calendar1.add(Calendar.DATE, day);
    calendar1.set(Calendar.HOUR_OF_DAY, hour);
    calendar1.set(Calendar.MINUTE, minute);
    calendar1.set(Calendar.SECOND, second);

    String moveDate = sdf1.format(calendar1.getTime());
    return moveDate;
}

当手指滑动和缩放所进行的处理

case MotionEvent.ACTION_MOVE:
            if (event.getPointerCount() == 1) {
                mMove = xPosition - mlastX;
                xMove = (int) (mMove / (mLineDivider * mDensity));
                mValue -= xMove;
                notifyValueChange();
                invalidate();
            }
            break;

还有最难处理的问题在于,当进行缩放时候,如果刻度尺变得过大或者过小的时候我们应该让刻度尺的精度模式也进行变化,适应页面的变化,不然太拥挤导致字体重叠,太松动导致整个页面可能一个刻度都没有。这个处理比较复杂,整体思路就是刻度尺的间隔会随着手指的缩放进行相应的缩放,当刻度尺大到一定程序或者小到一定程度就对刻度尺的精度进行改变

 else if (event.getPointerCount() == 2) {
                // 有两个手指按在屏幕上移动时,为缩放状态
                centerPointBetweenFingers(event);
                double fingerDis = distanceBetweenFingers(event);

                if (fingerDis > lastFingerDis) {
                    currentStatus = STATUS_ZOOM_OUT;
                } else {
                    currentStatus = STATUS_ZOOM_IN;
                }

                if(Mode==Mode_0&&currentStatus==STATUS_ZOOM_OUT||Mode==Mode_4&&currentStatus==STATUS_ZOOM_IN){
                    AXLog.e("wzytest","Mode_0 时放大 和 Mode_4 时候缩小不做任何处理");
                }else{
                    scaledRatio = (float) (fingerDis / lastFingerDis  );
                    shadowPaint.setStrokeWidth(6*scaledRatio);
                    mLineDivider =  lastItemDivider * scaledRatio; //缩放后一刻度在屏幕上的距离
                }

                if(currentStatus==STATUS_ZOOM_IN&&Mode==Mode_0){
                    if(2*mLineDivider<LINE_DIVIDER){
                        // 复位,转变刻度尺模式
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString1;
                        Mode = Mode_1;
                        valueToSencond = 2*valueToSencond;
                        AXLog.e("wzytest","mvalue:"+mValue);
                        //重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }else if(currentStatus==STATUS_ZOOM_IN&&Mode==Mode_1){
                    if(3*mLineDivider<LINE_DIVIDER){
                        // 复位,转变刻度尺模式
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString2;
                        Mode = Mode_2;
                        valueToSencond = 3*valueToSencond;
                        AXLog.e("wzytest","mvalue:"+mValue);
                        //重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }else if(currentStatus==STATUS_ZOOM_IN&&Mode==Mode_2){
                    if(4*mLineDivider<LINE_DIVIDER){
                        // 复位,转变刻度尺模式
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString3;
                        Mode = Mode_3;
                        valueToSencond = 4*valueToSencond;
                        AXLog.e("wzytest","mvalue:"+mValue);
                        //重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }

                }else if(currentStatus==STATUS_ZOOM_IN&&Mode==Mode_3){
                    AXLog.e("wzytest","跳转到了最后得刻度尺模式");
                    if(2*mLineDivider<LINE_DIVIDER){
                        // 复位,转变刻度尺模式
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString4;
                        Mode = Mode_4;
                        valueToSencond = 2*valueToSencond;
                        // 重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }


                if(currentStatus==STATUS_ZOOM_OUT&&Mode==Mode_4){
                    if(mLineDivider/2>LINE_DIVIDER){
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString3;
                        Mode = Mode_3;
                        valueToSencond = valueToSencond/2;
                        //重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }else if(currentStatus==STATUS_ZOOM_OUT&&Mode==Mode_3){
                    if(mLineDivider/4>LINE_DIVIDER){
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString2;
                        Mode = Mode_2;
                        valueToSencond = valueToSencond/4;
                        AXLog.e("wzytest","mvalue:"+mValue);
                        //重新获取当前value
                        mValue = getNowValue();
                        AXLog.e("wzytest","重新获取的value:"+mValue);
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }else if(currentStatus==STATUS_ZOOM_OUT&&Mode==Mode_2){
                    if(mLineDivider/3>LINE_DIVIDER){
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString1;
                        Mode = Mode_1;
                        valueToSencond = valueToSencond/3;
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }else if(currentStatus==STATUS_ZOOM_OUT&&Mode==Mode_1){
                    if(mLineDivider/2>LINE_DIVIDER){
                        mLineDivider = 2;
                        lastItemDivider = 2;
                        useString = timeString0;
                        Mode = Mode_0;
                        valueToSencond = valueToSencond/2;
                        //重新获取当前value
                        mValue = getNowValue();
                        //重新计算蓝色录像时间轴
                        initData();
                    }
                }

                AXLog.e("wzytest","itemDivider:"+mLineDivider+" lastItemDivider:"+lastItemDivider);
                postInvalidate();
            }

该部分代码比较复杂,建议有兴趣的读者下载源码进行研究。

##三、其它相关 作者开启了一个异步线程定时一秒让录像文件往后增加一秒,这么做的意图只是模拟正在录像的状态,让蓝色的区域跟着时间走动

##四、总结 遇到比较复杂的问题先抽丝剥茧的找出问题的本质,把问题慢慢细化,一步步解决,千里之行始于足下,不积跬步何以至千里,不积小流何以成江河

###源码下载请点这里

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

一个完美的视频播放条控件(刻度尺),可以将控件与时间对应起来,也可以对控件进行移动和缩放,把时间戳和位置对应起来 展开 收起
Android
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/wingceltis-c/Tunlview.git
git@gitee.com:wingceltis-c/Tunlview.git
wingceltis-c
Tunlview
Tunlview
master

搜索帮助