Android视频旋转并全屏展示

来源:互联网 发布:淘宝试衣间 编辑:程序博客网 时间:2024/04/27 20:57

视频旋转主要靠RotateLayout这个类实现,把VideoView放置于RotateLayout中,调用相关方法即可实现视频旋转全屏效果,有兴趣的可以谷歌(百度)下RotateLayout,

RotateLayout源码如下:

package com.julyapp.julyonline.opensource.rotate;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Rect;import android.graphics.RectF;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.ViewParent;import com.julyapp.julyonline.R;public class RotateLayout extends ViewGroup {public RotateLayout(Context context) {this(context, null);}public RotateLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RotateLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs);final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateLayout);final int angleFromAttrs = a.getInt(R.styleable.RotateLayout_angle, 0);angle = fixAngle(angleFromAttrs);a.recycle();setWillNotDraw(false);}/** * Returns current angle of this layout */public int getAngle() {return angle;}/** * Sets current angle of this layout. * If angle is not multiple of 90 it will be reduced to the multiple of 90. * For example 89 will be reduced to 0, 91 will be reduced to 90. */public void setAngle(int angle) {int fixedAngle = fixAngle(angle);if (this.angle != fixedAngle) {this.angle = fixedAngle;angleChanged = true;requestLayout();}}/** * Returns */public View getView() {if (getChildCount() > 0) {return getChildAt(0);} else {return null;}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {final View view = getView();if (view != null) {if (Math.abs(angle % 180) == 90) {//noinspection SuspiciousNameCombinationmeasureChild(view, heightMeasureSpec, widthMeasureSpec);setMeasuredDimension(resolveSize(view.getMeasuredHeight(), widthMeasureSpec),resolveSize(view.getMeasuredWidth(), heightMeasureSpec));} else {measureChild(view, widthMeasureSpec, heightMeasureSpec);setMeasuredDimension(resolveSize(view.getMeasuredWidth(), widthMeasureSpec),resolveSize(view.getMeasuredHeight(), heightMeasureSpec));}} else {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {if (angleChanged || changed) {final RectF layoutRect = tempRectF1;final RectF layoutRectRotated = tempRectF2;layoutRect.set(0, 0, r - l, b - t);rotateMatrix.setRotate(angle, layoutRect.centerX(), layoutRect.centerY());rotateMatrix.mapRect(layoutRectRotated, layoutRect);layoutRectRotated.round(viewRectRotated);angleChanged = false;}final View view = getView();if (view != null) {view.layout(viewRectRotated.left, viewRectRotated.top, viewRectRotated.right, viewRectRotated.bottom);}}@Overrideprotected void dispatchDraw(Canvas canvas) {canvas.save();canvas.rotate(-angle, getWidth() / 2f, getHeight() / 2f);super.dispatchDraw(canvas);canvas.restore();}@Overridepublic ViewParent invalidateChildInParent(int[] location, Rect dirty) {invalidate();return super.invalidateChildInParent(location, dirty);}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {viewTouchPoint[0] = event.getX();viewTouchPoint[1] = event.getY();rotateMatrix.mapPoints(childTouchPoint, viewTouchPoint);event.setLocation(childTouchPoint[0], childTouchPoint[1]);boolean result = super.dispatchTouchEvent(event);event.setLocation(viewTouchPoint[0], viewTouchPoint[1]);return result;}/** * Takes any angle, makes it valid one for this view. * This means multiple of 90. */private static int fixAngle(int angle) {return (angle / 90) * 90;}private int angle;private final Matrix rotateMatrix = new Matrix();private final Rect viewRectRotated = new Rect();private final RectF tempRectF1 = new RectF();private final RectF tempRectF2 = new RectF();private final float[] viewTouchPoint = new float[2];private final float[] childTouchPoint = new float[2];private boolean angleChanged = true;}


AndroidMainfest.xml相应Activity中配置:


 <activity            android:name=".activity.VideoPlayActivity"            android:configChanges="orientation|keyboardHidden|screenSize"            android:screenOrientation="portrait"            android:windowSoftInputMode="adjustPan" />


Activity中VideoView与RotateLayout关系如下:


    <RelativeLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"><span style="white-space:pre"></span><!--rotatelayout-->                <com.julyapp.julyonline.opensource.rotate.RotateLayout                    android:id="@+id/rotate_videoplay"                    android:layout_width="match_parent"                    android:layout_height="@dimen/video_height"                    app:angle="0">                    <FrameLayout                        android:layout_width="match_parent"                        android:layout_height="match_parent">                        <RelativeLayout                            android:layout_width="match_parent"                            android:layout_height="match_parent"><span style="white-space:pre"></span><!--Videoview-->                            <com.baidu.cyberplayer.core.BVideoView                                android:id="@+id/videoview"                                android:layout_width="match_parent"                                android:layout_height="match_parent"></com.baidu.cyberplayer.core.BVideoView>                        </RelativeLayout>                        <LinearLayout                            android:id="@+id/vp_ccontrolbar_top"                            android:layout_width="match_parent"                            android:layout_height="@dimen/video_control_height"                            android:layout_gravity="top"                            android:background="#B1000000"                            android:orientation="horizontal"                            android:visibility="invisible">                            <FrameLayout                                android:id="@+id/container_controlbar_top_iv"                                android:layout_width="wrap_content"                                android:layout_height="match_parent">                                <ImageView                                    android:id="@+id/iv_top_back"                                    android:layout_width="50dp"                                    android:layout_height="50dp"                                    android:layout_gravity="center"                                    android:scaleType="centerInside"                                    android:src="@mipmap/ic_back_toolbar" />                            </FrameLayout>                            <TextView                                android:id="@+id/tv_controlbar_top_title"                                android:layout_width="wrap_content"                                android:layout_height="wrap_content"                                android:layout_gravity="center_vertical"                                android:layout_marginLeft="20dp"                                android:textColor="#ffffff"                                android:textSize="15sp" />                        </LinearLayout>                        <LinearLayout                            android:id="@+id/vp_controlbar_bottom"                            android:layout_width="match_parent"                            android:layout_height="@dimen/video_control_height"                            android:layout_gravity="bottom"                            android:background="#B1000000"                            android:orientation="horizontal"                            android:visibility="invisible">                            <ImageView                                android:id="@+id/iv_start_or_stop"                                android:layout_width="50dp"                                android:layout_height="50dp"                                android:layout_gravity="center_vertical"                                android:scaleType="centerInside"                                android:src="@mipmap/video_pause_btn" />                            <RelativeLayout                                android:layout_width="0dp"                                android:layout_height="match_parent"                                android:layout_gravity="center_vertical"                                android:layout_weight="1">                                <SeekBar                                    android:id="@+id/seekbar"                                    style="@style/Widget.SeekBar.Normal"                                    android:layout_width="match_parent"                                    android:layout_height="wrap_content"                                    android:layout_centerVertical="true"                                    android:progressDrawable="@drawable/seekbar_style" />                                <TextView                                    android:id="@+id/tv_time"                                    android:layout_width="wrap_content"                                    android:layout_height="wrap_content"                                    android:layout_alignRight="@+id/seekbar"                                    android:layout_below="@+id/seekbar"                                    android:layout_marginRight="16dp"                                    android:layout_marginTop="-4dp"                                    android:textColor="#b4b4b4"                                    android:textSize="12sp" />                            </RelativeLayout>                            <ImageView                                android:id="@+id/iv_fullscreen"                                android:layout_width="50dp"                                android:layout_height="50dp"                                android:layout_gravity="center_vertical"                                android:scaleType="centerInside"                                android:src="@mipmap/video_play_fullscreen" />                        </LinearLayout>                        <ImageView                            android:id="@+id/iv_background"                            android:layout_width="match_parent"                            android:layout_height="match_parent"                            android:scaleType="fitXY" />                        <ImageView                            android:id="@+id/iv_center_start"                            android:layout_width="50dp"                            android:layout_height="50dp"                            android:layout_gravity="center"                            android:src="@mipmap/video_play_center" />                    </FrameLayout>                </com.julyapp.julyonline.opensource.rotate.RotateLayout>            </RelativeLayout>


Activity中代码段如下,fullScreenLayoutParams为rotatelayout(这里RotateLayout直接父层是一个RelativeLayout)全屏状态的布局属性

imageViewFullScreen.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!isFullScreen) {                    //全屏                    getSupportActionBar().hide();                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);                    fullScreenLayoutParams =                            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);                    fullScreenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);                    fullScreenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);                    fullScreenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);                    fullScreenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);                    fullScreenLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);                    rotateLayout.setLayoutParams(fullScreenLayoutParams);                    videoView.setLayoutParams(fullScreenLayoutParams);                    rotateLayout.startLayoutAnimation();                    isControlBarShow = true;                    controlBarTop.setVisibility(View.VISIBLE);                } else {                    //取消全屏                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) toolbar.getLayoutParams();                    layoutParams.topMargin = 0;                    toolbar.setLayoutParams(layoutParams);                    getSupportActionBar().show();                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);                    getWindow().clearFlags(1024);                    rotateLayout.setLayoutParams(videoLayoutParams);                    videoView.setLayoutParams(videoLayoutParams);                    rotateLayout.startLayoutAnimation();                    controlBarTop.setVisibility(View.INVISIBLE);                    controlBarBottom.setVisibility(View.VISIBLE);                    isControlBarShow = true;                }                isFullScreen = !isFullScreen;            }        });

同时别忘了重写onConfigurationChanged方法:


 @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        } else {            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);        }    }

这样就实现了视频的旋转功能。


0 0