2012-11-19 61 views
0

我的問題有2個部分。在播放視頻時調整視圖的大小和位置

  1. 如何(任何教程都是完美的)我可以在Android視圖中播放不是全屏的視頻嗎?
  2. 我可以在播放視頻時調整視圖的位置並更改其位置:
+0

到目前爲止您嘗試過了什麼?現在你的問題完全可以通過谷歌搜索來回答。 – karlphillip

回答

2

對於這種視頻操作,你絕對應該看看TextureView

此視圖可用於通過MediaPlayer呈現視頻,並且您可以對其應用任何轉換。

下面是如何用它來播放視頻(有一個愚蠢的縮放動畫)一個簡單的例子:

public class TestActivity extends Activity implements SurfaceTextureListener { 

private static final String VIDEO_URL = "http://www.808.dk/pics/video/gizmo.mp4"; 

private MediaPlayer player; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    player = MediaPlayer.create(this, Uri.parse(VIDEO_URL)); 

    setContentView(R.layout.main); 

    TextureView videoView = (TextureView) findViewById(R.id.video); 
    videoView.setSurfaceTextureListener(this); 


    // Scaling 
    Animation scaling = new ScaleAnimation(0.2f, 1.0f, 0.2f, 1.0f); 
    scaling.setDuration(2000); 
    videoView.startAnimation(scaling); 
} 

@Override 
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 
    player.setSurface(new Surface(surface)); 
    player.start(); 
} 

@Override 
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } 

@Override 
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } 

@Override 
public void onSurfaceTextureUpdated(SurfaceTexture surface) { } 
} 

用下面的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextureView 
     android:id="@+id/video" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

注意,這TextureView僅適用於API級別> = 14.

+0

不支持庫包括TextureView,因爲我想用的是Android 2.2 –

+0

我不知道,你必須檢查出來 – fiddler

+0

hi @fiddler,我可以知道如何調整視頻大小以適應調整後的TextureView ,請幫助...提前致謝 – bala