1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package de.danoeh.antennapod.view;
/*
* Copyright (C) Google Inc. All rights reserved.
*
* 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.
*/
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
public class AspectRatioVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
private float mAvailableWidth = -1;
private float mAvailableHeight = -1;
public AspectRatioVideoView(Context context) {
this(context, null);
}
public AspectRatioVideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AspectRatioVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mVideoWidth = 0;
mVideoHeight = 0;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mVideoWidth <= 0 || mVideoHeight <= 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
if (mAvailableWidth < 0 || mAvailableHeight < 0) {
mAvailableWidth = getWidth();
mAvailableHeight = getHeight();
}
float heightRatio = (float) mVideoHeight / mAvailableHeight;
float widthRatio = (float) mVideoWidth / mAvailableWidth;
int scaledHeight;
int scaledWidth;
if (heightRatio > widthRatio) {
scaledHeight = (int) Math.ceil((float) mVideoHeight
/ heightRatio);
scaledWidth = (int) Math.ceil((float) mVideoWidth
/ heightRatio);
} else {
scaledHeight = (int) Math.ceil((float) mVideoHeight
/ widthRatio);
scaledWidth = (int) Math.ceil((float) mVideoWidth
/ widthRatio);
}
setMeasuredDimension(scaledWidth, scaledHeight);
}
/**
* Source code originally from:
* http://clseto.mysinablog.com/index.php?op=ViewArticle&articleId=2992625
*
* @param videoWidth
* @param videoHeight
*/
public void setVideoSize(int videoWidth, int videoHeight) {
// Set the new video size
mVideoWidth = videoWidth;
mVideoHeight = videoHeight;
/*
* If this isn't set the video is stretched across the
* SurfaceHolders display surface (i.e. the SurfaceHolder
* as the same size and the video is drawn to fit this
* display area). We want the size to be the video size
* and allow the aspectratio to handle how the surface is shown
*/
getHolder().setFixedSize(videoWidth, videoHeight);
requestLayout();
invalidate();
}
/**
* Sets the maximum size that the view might expand to
* @param width
* @param height
*/
public void setAvailableSize(float width, float height) {
mAvailableWidth = width;
mAvailableHeight = height;
requestLayout();
}
}
|