2012-06-14 40 views
8

Tôi đang cố gắng thiết lập kích thước của Android VideoView theo cách động. Tôi đã xem xét trên StackOverflow cũng như internet; và giải pháp tốt nhất tôi tìm được là từ here. Tôi đã thực hiện triển khai của mình bên dưới:Đổi kích thước VideoView

public class ResizableVideoView extends VideoView { 

    public ResizableVideoView(Context c) { 
     super(c); 
    } 

    private int mVideoWidth = 100; 
    private int mVideoHeight = 100; 

    public void changeVideoSize(int width, int height) { 
     mVideoWidth = width;  
     mVideoHeight = height; 

     // not sure whether it is useful or not but safe to do so 
     getHolder().setFixedSize(width, height); 

     forceLayout(); 
     invalidate(); // very important, so that onMeasure will be triggered 
    } 

    public void onMeasure(int specwidth, int specheight) { 
     Log.i("onMeasure","On Measure has been called"); 
     setMeasuredDimension(mVideoWidth, mVideoHeight); 
    } 

    public void onDraw(Canvas c) { 
     super.onDraw(c); 
     Log.i("onDraw","Drawing..."); 
    } 
} 

Video thay đổi kích thước chính xác trên trình giả lập Android cũng như trên Motorola Droid X; nhưng trên Motorola Droid, VideoView thay đổi kích thước nhưng video phát trong VideoView không thay đổi kích thước. Trên Motorola Droid, nếu VideoView được đặt ở kích thước lớn hơn video đang phát, một nền đen xuất hiện trong VideoView với video đang phát ở góc trên cùng bên trái của VideoView trên nền đen.

Làm cách nào để thay đổi kích thước một VideoView chính xác trong Android?

Cảm ơn, Vance

Trả lời

3

thực hiện tôi làm việc như thế này:

RelativeLayout.LayoutParams videoviewlp = new RelativeLayout.LayoutParams(newwidth, newheight); 
    videoviewlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    videoviewlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
    videoview.setLayoutParams(videoviewlp); 
    videoview.invalidate(); 

Bằng cách vô hiệu các videoview, bạn sẽ buộc nó để vẽ lại toàn bộ videoview sử dụng LayoutParams mới (và kích thước mới).

+0

Tôi kiểm tra nó. nhưng nó cắt video thay vì thay đổi kích cỡ! Bạn còn ý kiến ​​nào không? – boomz

+1

@boomz Cuối cùng tôi đã tạo một triển khai vào mùa hè năm ngoái. Xem qua lớp [VideoPlayer] (https://github.com/Vance-Turner/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/VideoPlayer.java) từ dự án App Inventor (mã thay đổi kích thước, tính đến cuối mùa hè, là công việc của tôi). –

Các vấn đề liên quan