安卓videoview視訊縮放
第一步重寫videoview,新建MyVideoView,至於為啥,網上有很多解釋,大概是原始碼中的給登出掉了,需要重寫把縮放功能開啟
public class MyVideoView extends VideoView { public MyVideoView(Context context) { super(context); } public MyVideoView(Context context, AttributeSet attrs) { super(context, attrs); } @Overridepublic void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); setMeasuredDimension(width, height); } }
第二步,編寫xml,把MyVideoView放到LinearLayout或者Relativlayout中,以便程式碼中動態修改myvideoview的大小
<com.sammer.xiangaotie.util.MyVideoView android:id="@+id/my_video_view" android:layout_width="500px" android:layout_height="500px" android:layout_gravity="center"/>第三步,MainActivity中引用,並進行縮放
1. FrameLayout下動態設定子控制元件居中,動態用JAVA程式碼要這樣實現:(位置,要在其他的語句中進行設定)
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp.gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);
2. RelativeLayout下動態設定子控制元件居中:
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btn1.setLayoutParams(lp);
3.LinearLayout下動態設定子空間大小,居中。
LinearLayout.LayoutParams rl = new LinearLayout.LayoutParams(300,300); rl.gravity = Gravity.CENTER_HORIZONTAL; videoView.setLayoutParams(rl);
今天遇到的其他問題
1模擬器中spinner無法正常顯示。
答案:把Activity修改成AppCompatActivity成功
2AppCompatActivity下requestWindowFeature(Window.FEATURE_NO_TITLE);無效解決辦法
答案:修改AndroidManifest中的application下的android:theme選項
<application android:allowBackup="true" android:icon="@mipmap/xiangaotielogo" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>