Android videoview, rotating Video orientation, rotation, etc.

Source: Internet
Author: User

Recently, I have been studying operations on video rotation in android programming, but I still haven't completed the goal: to rotate the video 90 degrees in a small area of a small area on a certain page.

 

The methods are as follows:


1. Rotate a view directly, that is, modify the orientation attribute of the view. framelayout does not have the orientation attribute. If linearlayout has this attribute, the orientation can be changed successfully, however, the actual effect is not satisfactory. All the things in layout have been turned around, but the video has not been rotated for 90 times, which is very depressing.

2. Try to use the animation. The actual effect is to rotate the videoview. The videoview is rotated, but the video is still indifferent.

3. Try rotating the entire screen: it is not difficult to achieve this goal. In manifest. xml, add the attribute of activity

Android: configChanges = "orientation" and

Android: screenOrientation = "sensor"

Then add the following in the Code:


Public void onConfigurationChanged (Configuration newConfig ){

Super. onConfigurationChanged (newConfig );

}

Remember, for example
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );

Yes. Otherwise, this method will become invalid.
That's OK. The actual effect is that when the mobile phone is turned from 90 degrees to vertical, the entire interface is turned, and the effect is good! The video is also rotated 90 degrees.

But ...... My demand seems to be: Video rotation only, and the mobile phone does not move.

 

4. A post I saw in stackflow, the problem that my buddy encountered was similar to mine ...... The only difference is that he wants to play the video in full screen ...... Then he doesn't need to consider the issue of rotating other controls together. He just needs to resize the video + solution 3 above and use videoview to play the video, code for that solution:
[Java]
Public class VideoViewCustom extends VideoView {
 
Private int mForceHeight = 0;
Private int mForceWidth = 0;
Public VideoViewCustom (Context context ){
Super (context );
}
 
Public VideoViewCustom (Context context, AttributeSet attrs ){
This (context, attrs, 0 );
} Www.2cto.com
 
Public VideoViewCustom (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
 
Public void setDimensions (int w, int h ){
This. mForceHeight = h;
This. mForceWidth = w;
 
}
 
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Log. I ("@", "onMeasure ");
 
SetMeasuredDimension (mForceWidth, mForceHeight );
}
}

And this Code:

[Java]
@ 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 );
 
QuestionVideo. setDimensions (displayHeight, displayWidth );
QuestionVideo. getHolder (). setFixedSize (displayHeight, displayWidth );
 
} Else {
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FORCE_NOT_FULLSCREEN, WindowManager. LayoutParams. FLAG_FORCE_NOT_FULLSCREEN );
 
QuestionVideo. setDimensions (displayWidth, smallHeight );
QuestionVideo. getHolder (). setFixedSize (displayWidth, smallHeight );
 
}
}

The main task of the two codes is to re-extend the videoview class and automatically resize the video when the screen is rotated. (Use packagename. newclass in xml to ensure that labels in xml can be correctly bound to the class of the Code)
5. What is the latest API available in api level16? Level10, or 2.3.3, cannot be used.

Android. media. effect
Official site in: http://developer.android.com/reference/android/media/effect/package-summary.html
Well, after reading a detailed explanation, it seems that it is only valid for the image and has never said the word "video ......

 

6. Rotate a Layout directly and use this layout in xml.

[Java]
Public class RotateLayout extends FrameLayout {
Private Matrix mForward = new Matrix ();
Private Matrix mReverse = new Matrix ();
Private float [] mTemp = new float [2];
 
Public RotateLayout (Context context ){
Super (context );
}
 
Public RotateLayout (Context context, AttributeSet attrs ){
Super (context, attrs );
 
}
 
 
/* (Non-Javadoc)
* @ See android. widget. FrameLayout # onMeasure (int, int)
*/
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Super. onMeasure (widthMeasureSpec, heightMeasureSpec );
// This didn't work:
// Super. onMeasure (heightMeasureSpec, widthMeasureSpec );
}
 
/* (Non-Javadoc)
* @ See android. widget. FrameLayout # onSizeChanged (int, int)
*/
@ Override
Protected void onSizeChanged (int w, int h, int oldw, int oldh ){
Super. onSizeChanged (w, h, oldw, oldh );
}
 
@ Override
Protected void dispatchDraw (Canvas canvas ){
Canvas. rotate (270, getWidth ()/2, getHeight ()/2 );
// This code will stretch the canvas to accommodate the new screen size. This is not what I want.
// Float scaleX = (float) getHeight ()/getWidth ();
// Float scaleY = (float) getWidth ()/getHeight ();
// Canvas. scale (scaleX, scaleY, getWidth ()/2, getHeight ()/2 );
MForward = canvas. getMatrix ();
MForward. invert (mReverse );
Canvas. save ();
Canvas. setMatrix (mForward); // This is the matrix we need to use for proper positioning of touch events
Super. dispatchDraw (canvas );
Canvas. restore ();
}
 
@ Override
Public boolean dispatchTouchEvent (MotionEvent event ){
Final float [] temp = mTemp;
Temp [0] = event. getX ();
Temp [1] = event. getY ();
 
MReverse. mapPoints (temp );
 
Event. setLocation (temp [0], temp [1]);
Return super. dispatchTouchEvent (event );
}
}
By David beckham2901
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.