我想设置VideoView
尺寸,我无法弄清楚我做错了什么。我运行此代码时看不到尺寸变化
package com.nati_1.ImageGallerySwitching_v8;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.VideoView;
import java.util.Arrays;
public class VideoActivity extends AppCompatActivity {
ImageButton floatButtonPlay;
ImageButton floatButtonPause;
ImageButton floatButtonBack;
private Integer videos[];
private Integer videos1[]={R.raw.vid4_1, R.raw.vid5_1_6m, R.raw.vid3_3m, R.raw.vid2_2_9m};
private Integer videos2[]={R.raw.vts_0, R.raw.vts_1, R.raw.vts_5, R.raw.vts_6};
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
floatButtonPlay=(ImageButton) findViewById(R.id.playButton);
floatButtonPause=(ImageButton) findViewById(R.id.pauseButton);
floatButtonPlay.setVisibility(View.INVISIBLE);
floatButtonBack=(ImageButton) findViewById(R.id.backButton);
final MyVideoView video1=(MyVideoView)findViewById(R.id.MyVideoView);
videos= Arrays.copyOf(videos1,videos1.length);
switch (MainActivity.AlbumChosed){
case 1:
videos= Arrays.copyOf(videos1,videos1.length);
break;
case 2:
videos= Arrays.copyOf(videos2,videos2.length);
break;
default:
}
if (MainActivity.VidNum==MainActivity.vidPosition[0] ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
video1.setVideoSize(70,70);
video1.setVideoPath("android.resource://" + getPackageName() + "/" + videos[0]);
}
else if
........
........
video1.start();
video1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
floatButtonPause.setVisibility(View.INVISIBLE);
floatButtonPlay.setVisibility(View.VISIBLE);
floatButtonBack.setVisibility(View.VISIBLE);
}
});
floatButtonPlay.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
floatButtonPlay.setVisibility(View.INVISIBLE);
floatButtonPause.setVisibility(View.VISIBLE);
video1.start();
}
} );
floatButtonPause.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
floatButtonPause.setVisibility(View.INVISIBLE);
....
if(video1.isPlaying()){
video1.pause();
}
}
} );
floatButtonBack.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
finish();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
}
});
}
public class MyVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyVideoView(Context context) {
super(context);
}
public void setVideoSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Log.i("@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if (mVideoWidth * height > width * mVideoHeight) {
// Log.i("@@@", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
} else if (mVideoWidth * height < width * mVideoHeight) {
// Log.i("@@@", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else {
// Log.i("@@@", "aspect ratio is correct: " +
// width+"/"+height+"="+
// mVideoWidth+"/"+mVideoHeight);
}
}
// Log.i("@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}
}
我的XML是下一个
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
tools:context="com.nati_1.ImageGallerySwitching_v8.VideoActivity">
<com.nati_1.ImageGallerySwitching_v8.VideoActivity.MyVideoView
android:id="@+id/MyVideoView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
app:layout_constraintVertical_bias="0.428"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp" />
<!-- "play" was taken from https://www.flaticon.com/ -->
<!-- android:src="@mipmap/pause128"-->
<ImageButton
android:id="@+id/playButton"
....
....
我得到致命的异常
01-18 21:31:28.053 28639-28639/com.nati_1.ImageGallerySwitching_v6 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nati_1.ImageGallerySwitching_v6, PID: 28639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nati_1.ImageGallerySwitching_v6/com.nati_1.ImageGallerySwitching_v8.VideoActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.nati_1.ImageGallerySwitching_v8.VideoActivity.MyVideoView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.nati_1.ImageGallerySwitching_v8.VideoActivity.MyVideoView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.nati_1.ImageGallerySwitching_v8.VideoActivity.MyVideoView
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.nati_1.ImageGallerySwitching_v8.VideoActivity.MyVideoView" on path: DexPathList[[zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/base.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.nati_1.ImageGallerySwitching_v6-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.nati_1.ImageGallerySwitching_v6-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:609)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
先谢谢