Autore Topic: Layout per video tipo youtube  (Letto 310 volte)

Offline gabrygaggio19

  • Nuovo arrivato
  • *
  • Post: 1
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    OnePlus One
  • Sistema operativo:
    Mac OS X 10.9
Layout per video tipo youtube
« il: 10 Settembre 2014, 11:38:06 CEST »
0
Ciao a tutti,
mi sto cimentando in un app (la mia prima app seria) per la visualizzazione dei video di un canale su youtube. Quello che volevo fare era la possibilità di rimpicciolire il player in stile youtube in modo da poter cercare un altro video mentre ne sto guardando uno.
Ho trovato alcune guide che spiegano il funzionamento di viewDragHelper ma non fanno proprio al caso mio, cioè manca qualcosa.
Ad esempio non posso chiudere il video facendo uno slide o non mi permette di cliccare sui comandi del video.

Voi avete qualche idea?

Questo è il codice che ho scritto per ora:

Codice (Java): [Seleziona]
public class VideoLayout extends ViewGroup {

    private final ViewDragHelper viewDragHelper;

    private View videoView;
    private View descriptionView;

    public static final int NOT_LOADED = 0;
    public static final int LOADED = 1;
    public static final int CLOSING = 2;

    private int videoState = 0;

    private float initialMotionX;
    private float initialMotionY;

    private int dragRange;
    private int layoutTop;
    private float dragOffset;
    private int dragHorizontalRange;
    private int layoutRight;
    private float dragHorizontalOffset;


    public VideoLayout(Context context) {
        this(context, null);
    }

    public VideoLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public VideoLayout(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        viewDragHelper = ViewDragHelper.create(this, 1f, new DragHelperCallback());
    }

    @Override
    protected void onFinishInflate() {
        videoView = findViewById(R.id.video_view);
        descriptionView = findViewById(R.id.desc_view);
    }

    public void setVideoView(View videoView) {
        this.videoView = videoView;
    }

    public void setVideoState(int state) {
        videoState = state;
    }

    public void maximize() {
        smoothSlideTo(0f);
    }

    public boolean smoothSlideTo(float slideOffset) {
        final int topBound = getPaddingTop();
        int y = (int) (topBound + slideOffset * dragRange);

        if (viewDragHelper.smoothSlideViewTo(videoView, videoView.getLeft(), y)) {
            ViewCompat.postInvalidateOnAnimation(this);
            return true;
        }

        return false;
    }

    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

/*    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);

        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            viewDragHelper.cancel();
            return super.onInterceptTouchEvent(ev);
        }

        final float x = ev.getX();
        final float y = ev.getY();
        boolean interceptTap = false;

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                initialMotionX = x;
                initialMotionY = y;
                interceptTap = viewDragHelper.isViewUnder(videoView, (int) x, (int) y);
                break;

            case MotionEvent.ACTION_MOVE:
                final float adx = Math.abs(x-initialMotionX);
                final float ady = Math.abs(y-initialMotionY);
                final int slop = viewDragHelper.getTouchSlop();
                if (ady < slop && adx > ady) {
                    viewDragHelper.cancel();
                    return false;
                }
        }

        return viewDragHelper.shouldInterceptTouchEvent(ev) || interceptTap;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent ev) {
        viewDragHelper.processTouchEvent(ev);

        final int action = ev.getAction();
        final float x = ev.getX();
        final float y = ev.getY();

        boolean isHeaderViewUnder = viewDragHelper.isViewUnder(videoView, (int) x, (int) y);
        switch (action & MotionEventCompat.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                initialMotionX = x;
                initialMotionY = y;
                break;
            }

            case MotionEvent.ACTION_UP: {
                final float dx = x - initialMotionX;
                final float dy = y - initialMotionY;
                final int slop = viewDragHelper.getTouchSlop();
                if (dx * dx + dy * dy < slop * slop && isHeaderViewUnder) {
                    if (dragOffset == 0) {
                        smoothSlideTo(1f);
                    } else {
                        smoothSlideTo(0f);
                    }
                }
                break;
            }
        }


        return isHeaderViewUnder && isViewHit(videoView, (int) x, (int) y) || isViewHit(descriptionView, (int) x, (int) y);
    }


    private boolean isViewHit(View view, int x, int y) {
        int[] viewLocation = new int[2];
        view.getLocationOnScreen(viewLocation);
        int[] parentLocation = new int[2];
        this.getLocationOnScreen(parentLocation);
        int screenX = parentLocation[0] + x;
        int screenY = parentLocation[1] + y;
        return screenX >= viewLocation[0] && screenX < viewLocation[0] + view.getWidth() &&
                screenY >= viewLocation[1] && screenY < viewLocation[1] + view.getHeight();
    }*/


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        int maxHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
                resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        dragRange = getHeight() - videoView.getHeight();
        dragHorizontalRange = getWidth() - videoView.getWidth();

        videoView.layout(
                0,
                layoutTop,
                r,
                layoutTop + videoView.getMeasuredHeight());

        descriptionView.layout(
                0,
                layoutTop + videoView.getMeasuredHeight(),
                r,
                layoutTop + b);
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View view, int i) {
            return (view == videoView);
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            layoutTop = top;
            dragOffset = (float) top / dragRange;

            videoView.setPivotX(videoView.getWidth());
            videoView.setPivotY(videoView.getHeight());
            videoView.setScaleX(1 - dragOffset / 2);
            videoView.setScaleY(1 - dragOffset / 2);

            descriptionView.setAlpha(1 - dragOffset);

            layoutRight = dx;
            dragHorizontalOffset = (float) dx / dragHorizontalRange;

            videoView.setAlpha(1 - dragHorizontalOffset);

            requestLayout();
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            layoutTop = getPaddingTop();
            layoutRight = getPaddingRight();
            if (videoState != CLOSING) {
                if (yvel > 0 || (yvel == 0 && dragOffset > 0.5)) {
                    layoutTop += dragRange;
                }
                viewDragHelper.settleCapturedViewAt(releasedChild.getLeft(), layoutTop);
            } else {
                if (xvel > 0 || (xvel == 0 && dragHorizontalOffset > 0.5)) {
                    layoutRight -= dragHorizontalRange;
                    videoState = NOT_LOADED;
                }
                viewDragHelper.settleCapturedViewAt(layoutRight, releasedChild.getTop());
                if (videoState == NOT_LOADED) {
                    setVisibility(GONE);
                }
            }
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return dragRange;
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return dragHorizontalRange;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            final int topBound = getPaddingTop();
            final int bottomBound = getHeight() - child.getHeight() - child.getPaddingBottom();

            if (videoState == CLOSING) {
                return bottomBound;
            }

            return Math.min(Math.max(top, topBound), bottomBound);
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (dragOffset == 1f) {
                videoState = CLOSING;

                final int leftBound = getPaddingLeft();
                final int rightBound = getWidth() - child.getWidth() - child.getPaddingRight();

                return Math.min(Math.max(left, leftBound), rightBound);
            }
            videoState = LOADED;
            return super.clampViewPositionHorizontal(child, left, dx);
        }

    }

}

Codice (XML): [Seleziona]
<com.navier.pistoialive.ui.VideoLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context="com.navier.pistoialive.PlayerFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/video_layout"
   android:orientation="vertical">

    <com.google.android.youtube.player.YouTubePlayerView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/video_view"
       />

    <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/desc_view"
       android:padding="16dp"
       android:background="#0000FF">

        <TextView
           android:layout_width="match_parent"
           android:layout_height="48dp"
           android:text="@string/pistoialive_banner"
           android:gravity="center"/>

        <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/share_button"
           android:text="@string/video_share"
           android:layout_marginTop="48dp"
           android:layout_alignParentLeft="true"
           android:layout_alignParentStart="true" />

        <ImageButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/pref_button"
           android:layout_alignTop="@id/share_button"
           android:layout_toRightOf="@id/share_button"
           android:src="#FF0000"
           android:layout_toLeftOf="@+id/yt_button"
           android:layout_toStartOf="@+id/yt_button" />

        <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/yt_button"
           android:text="@string/video_youtube"
           android:layout_alignTop="@id/share_button"
           android:layout_alignParentRight="true"
           android:layout_alignParentEnd="true" />

        <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:layout_below="@id/share_button"
           android:layout_marginTop="48dp" />

    </RelativeLayout>

</com.navier.pistoialive.ui.VideoLayout>

Grazie a tutti in anticipo!