Android Open Source - Media-Pack Range Selector






From Project

Back to project page Media-Pack.

License

The source code is released under:

Apache License

If you think the Android project Media-Pack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//
//               INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2013-2014 Intel Corporation. All Rights Reserved.
///*from  w  w w  .j  a  v  a 2s .  c om*/

package com.intel.inde.mp.samples.controls;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class RangeSelector extends View
{
    public static final int HandleSize = 48;
    public static final int BarHeight = 8;

    public interface RangeSelectorEvents
    {
        public void onStartPositionChanged(int position);

        public void onEndPositionChanged(int position);
    }

    private Handle[] mHandles;
    private Handle mHandleToMove;

    private Paint mBarPaint;
    private Paint mHandlePaint;
    private Paint mHandleHaloPaint;

    private int mWidth;
    private int mHeight;

    private Rect mBarRect;

    RangeSelectorEvents mEventsListener;

    public RangeSelector(Context context)
    {
        super(context);

        init(null, 0);
    }

    public RangeSelector(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        init(attrs, 0);
    }

    public RangeSelector(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        init(attrs, defStyle);
    }

    public void setEventsListener(RangeSelectorEvents listener) {
        mEventsListener = listener;
    }

    public int getStartPosition()
    {
        return mHandles[0].getPosition();
    }

    public void setStartPosition(int position)
    {
        mHandles[0].setPosition(position);

        invalidate();
    }

    public int getEndPosition()
    {
        return mHandles[1].getPosition();
    }

    public void setEndPosition(int position)
    {
        mHandles[1].setPosition(position);

        invalidate();
    }

    private void init(AttributeSet attrs, int defStyle)
    {
        setFocusable(true);

        mBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBarPaint.setColor(Color.GRAY);

        mHandleHaloPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHandleHaloPaint.setColor(Color.BLACK);
        mHandleHaloPaint.setAlpha(0x50);

        mHandlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHandlePaint.setColor(0xff8285E2);

        mHandles = new Handle[2];

        mHandles[0] = new Handle();
        mHandles[0].setId(0);
        mHandles[0].setPosition(0);

        mHandles[1] = new Handle();
        mHandles[1].setId(1);
        mHandles[1].setPosition(100);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        int cy = mBarRect.top + (mBarRect.bottom - mBarRect.top) / 2;
        int cx;

        canvas.drawRect(mBarRect, mBarPaint);

        for (Handle handle : mHandles)
        {
            cx = positionToX(handle.getPosition());

            canvas.drawCircle(cx, cy, HandleSize / 2, mHandleHaloPaint);
            canvas.drawCircle(cx, cy, HandleSize / 4, mHandlePaint);
        }

        canvas.drawRect(positionToX(mHandles[0].getPosition()), mBarRect.top, positionToX(mHandles[1].getPosition()), mBarRect.bottom, mHandlePaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        mWidth = w;
        mHeight = h;

        mBarRect = new Rect();
        mBarRect.left = HandleSize / 2;
        mBarRect.right = mWidth - HandleSize / 2;
        mBarRect.top = mHeight / 2 - BarHeight / 2;
        mBarRect.bottom = mBarRect.top + BarHeight / 2;
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();

        int x = (int) event.getX();
        int y = (int) event.getY();

        int cy = mBarRect.top + (mBarRect.bottom - mBarRect.top) / 2;
        int cx;

        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
            {
                mHandleToMove = null;

                for (Handle handle : mHandles)
                {
                    cx = positionToX(handle.getPosition());

                    if (pointInsideRect(cx - HandleSize, cx + HandleSize, cy - HandleSize, cy + HandleSize, x, y))
                    {
                        mHandleToMove = handle;

                        break;
                    }
                }

                // Hit test on handles failed
                // let's pick handle what is more
                // close to touch point
                if(mHandleToMove == null)
                {
                    if(Math.abs(x - positionToX(mHandles[0].getPosition())) < Math.abs(x - positionToX(mHandles[1].getPosition())))
                    {
                        mHandleToMove = mHandles[0];
                    }
                    else
                    {
                        mHandleToMove = mHandles[1];
                    }
                }
            }
            break;

            //Touch drag with the knob
            case MotionEvent.ACTION_MOVE:
            {
                if (mHandleToMove != null)
                {
                    int position = x * 100 / mWidth;

                    if (position > 0 && position < 100)
                    {
                        if (mHandleToMove.getId() == 0)
                        {
                            if ((x + HandleSize * 2) > positionToX(mHandles[1].getPosition()))
                            {
                                position = -1;
                            }
                        }
                        else if (mHandleToMove.getId() == 1)
                        {
                            if ((x - HandleSize * 2) < positionToX(mHandles[0].getPosition()))
                            {
                                position = -1;
                            }
                        }
                    }
                    else
                    {
                        position = -1;
                    }

                    if (position != -1)
                    {
                        mHandleToMove.setPosition(position);

                        if (mEventsListener != null)
                        {
                            if (mHandleToMove.getId() == 0)
                            {
                                mEventsListener.onStartPositionChanged(position);
                            }
                            else if (mHandleToMove.getId() == 1)
                            {
                                mEventsListener.onEndPositionChanged(position);
                            }
                        }
                    }
                }
            }
            break;

            case MotionEvent.ACTION_UP:
            {
                mHandleToMove = null;
            }
            break;
        }

        invalidate();

        return true;
    }

    private int positionToX(int position)
    {
        int barWidth = mBarRect.width();

        return (int) ((position * barWidth / 100) + mBarRect.left);
    }

    private boolean pointInsideRect(int l, int r, int t, int b, int pointX, int pointY)
    {
        if (pointX > l && pointX < r && pointY > t && pointY < b)
        {
            return true;
        }

        return false;
    }

    private class Handle {
        int mPosition;

        int mId;

        public void setId(int id) {
            mId = id;
        }

        public int getId() {
            return mId;
        }

        public void setPosition(int position) {
            mPosition = position;
        }

        public int getPosition() {
            return mPosition;
        }
    }
}




Java Source Code List

com.intel.inde.mp.android.graphics.EglUtil.java
com.intel.inde.mp.android.graphics.FrameBuffer.java
com.intel.inde.mp.android.graphics.FullFrameTexture.java
com.intel.inde.mp.android.graphics.ShaderProgram.java
com.intel.inde.mp.android.graphics.VideoEffect.java
com.intel.inde.mp.effects.AudioEffect.java
com.intel.inde.mp.effects.AudioReader.java
com.intel.inde.mp.effects.GrayScaleEffect.java
com.intel.inde.mp.effects.InverseEffect.java
com.intel.inde.mp.effects.JpegSubstituteEffect.java
com.intel.inde.mp.effects.OverlayEffect.java
com.intel.inde.mp.effects.RotateEffect.java
com.intel.inde.mp.effects.SepiaEffect.java
com.intel.inde.mp.effects.SubstituteAudioEffect.java
com.intel.inde.mp.effects.TextOverlayEffect.java
com.intel.inde.mp.samples.ActivityWithTimeline.java
com.intel.inde.mp.samples.CameraCapturerActivity.java
com.intel.inde.mp.samples.CameraStreamerActivity.java
com.intel.inde.mp.samples.ComposerAudioEffectActivity.java
com.intel.inde.mp.samples.ComposerAudioEffectCoreActivity.java
com.intel.inde.mp.samples.ComposerCutActivity.java
com.intel.inde.mp.samples.ComposerCutCoreActivity.java
com.intel.inde.mp.samples.ComposerJoinActivity.java
com.intel.inde.mp.samples.ComposerJoinCoreActivity.java
com.intel.inde.mp.samples.ComposerMediaFileInfoActivity.java
com.intel.inde.mp.samples.ComposerMediaFileInfoCoreActivity.java
com.intel.inde.mp.samples.ComposerTranscodeActivity.java
com.intel.inde.mp.samples.ComposerTranscodeCoreActivity.java
com.intel.inde.mp.samples.ComposerVideoEffectActivity.java
com.intel.inde.mp.samples.ComposerVideoEffectCoreActivity.java
com.intel.inde.mp.samples.DemoListAdapter.java
com.intel.inde.mp.samples.DemoListItem.java
com.intel.inde.mp.samples.ExpandableSamplesListAdapter.java
com.intel.inde.mp.samples.FPSCounter.java
com.intel.inde.mp.samples.Format.java
com.intel.inde.mp.samples.GameCapturing.java
com.intel.inde.mp.samples.GameRenderer.java
com.intel.inde.mp.samples.GameStreaming.java
com.intel.inde.mp.samples.MediaStreamerActivity.java
com.intel.inde.mp.samples.MediaStreamerCoreActivity.java
com.intel.inde.mp.samples.RecognitionActivity.java
com.intel.inde.mp.samples.SamplesMainActivity.java
com.intel.inde.mp.samples.VideoCapture.java
com.intel.inde.mp.samples.VideoPlayerActivity.java
com.intel.inde.mp.samples.VideoStreamPlayerActivity.java
com.intel.inde.mp.samples.controls.CameraCaptureSettingsPopup.java
com.intel.inde.mp.samples.controls.GameGLSurfaceView.java
com.intel.inde.mp.samples.controls.PlaybackToolbar.java
com.intel.inde.mp.samples.controls.PopupMessage.java
com.intel.inde.mp.samples.controls.Popup.java
com.intel.inde.mp.samples.controls.RangeSelector.java
com.intel.inde.mp.samples.controls.TimelineItem.java
com.intel.inde.mp.samples.controls.TranscodeSurfaceView.java
com.intel.inde.mp.samples.controls.VideoPlayer.java