2017-12-21 5 views
1

Ich versuche, ein Video als Hintergrund in meiner Anwendung zu setzen. Allerdings, wenn ich das Video einfüge, steigt die Größe, sehr seltsam, im Laufe der Zeit und ist nicht oben. Weiß jemand wie ich das beheben kann? Lassen Sie es in einem Rechts Verhältnis, nur, dass es wie Seiten geschnitten wird ... etwas von der ArtVideohintergrund

Mein Code:

Activity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/home_container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <icon.com.videobackground.VideoBackground 
     android:id="@+id/introVideoView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent"> 

     <EditText 
      android:id="@+id/edit" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:hint="Enter Email" 
      android:padding="10dp" 
      android:gravity="center" 
      android:background="@null" /> 

     <EditText 
      android:id="@+id/pass" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/edit" 
      android:hint="Enter Password" 
      android:padding="10dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center" 
      android:background="@null" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Login" 
      android:textColor="@android:color/holo_orange_dark" 
      android:padding="10dp" 
      android:layout_marginTop="5dp" 
      android:textSize="25sp" 
      android:background="@android:color/transparent" 
      android:layout_below="@+id/pass"/> 

    </RelativeLayout> 
</RelativeLayout> 

VideoBackground.java

public class VideoBackground extends SurfaceView implements SurfaceHolder.Callback { 

    private static final String TAG = "INTRO_VIDEO_CALLBACK"; 
    private MediaPlayer mp; 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public VideoBackground(Context context, AttributeSet attrs, 
          int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(); 
    } 

    public VideoBackground(Context context, AttributeSet attrs, 
          int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    public VideoBackground(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public VideoBackground(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     mp = new MediaPlayer(); 
     getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.video_bgg); // your intro video file placed in raw folder named as intro.mp4 
     try { 
      mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), 
        afd.getDeclaredLength()); 
      mp.prepare(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     android.view.ViewGroup.LayoutParams lp = getLayoutParams(); 

     int screenHeight = getHeight(); 
     int screenWidth = getWidth(); 

     // this plays in full screen video 
     lp.height = screenHeight; 
     lp.width = screenWidth; 

     setLayoutParams(lp); 
     mp.setDisplay(getHolder()); 
     mp.setLooping(false); 
     mp.start(); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
           int height) { 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     mp.stop(); 
    } 
} 

Image this paste raw

+0

Wie pro mein Verständnis Sie können Detail erhalten [Dieser Link] (https://stackoverflow.com/questions/8830111/integrating-video- file-in-android-app-als-app-background) –

+0

Mögliches Duplikat von [Video-Datei in Android-App als App-Hintergrund integrieren] (https://stackoverflow.com/questions/8830111/integrating-video-file-in- android-app-as-app-background) –

Antwort

0

Make Seprate Klasse VideoBackground und fügen Sie diesen Code

public class VideoBackground extends SurfaceView implements SurfaceHolder.Callback { 

private static final String TAG = "INTRO_VIDEO_CALLBACK"; 
private MediaPlayer mp; 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public VideoBackground(Context context, AttributeSet attrs, 
         int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(); 
} 

public VideoBackground(Context context, AttributeSet attrs, 
         int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 

public VideoBackground(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public VideoBackground(Context context) { 
    super(context); 
    init(); 
} 

private void init() { 
    mp = new MediaPlayer(); 
    getHolder().addCallback(this); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sample); // your intro video file placed in raw folder named as intro.mp4 
    try { 
     mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), 
       afd.getDeclaredLength()); 
     mp.prepare(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    android.view.ViewGroup.LayoutParams lp = getLayoutParams(); 

    int screenHeight = getHeight(); 
    int screenWidth = getWidth(); 

    // this plays in full screen video 
    lp.height = screenHeight; 
    lp.width = screenWidth; 

    setLayoutParams(lp); 
    mp.setDisplay(getHolder()); 
    mp.setLooping(false); 
    mp.start(); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
          int height) { 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    mp.stop(); 
    } 
    } 

und Ihre XML wäre wie activity_main

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/home_container" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<icon.com.videobackground.VideoBackground 
    android:id="@+id/introVideoView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent"> 

    <EditText 
     android:id="@+id/edit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:hint="Enter Email" 
     android:padding="10dp" 
     android:gravity="center" 
     android:background="@null" /> 

    <EditText 
     android:id="@+id/pass" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/edit" 
     android:hint="Enter Password" 
     android:padding="10dp" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:background="@null" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:textColor="@android:color/holo_orange_dark" 
     android:padding="10dp" 
     android:layout_marginTop="5dp" 
     android:textSize="25sp" 
     android:background="@android:color/transparent" 
     android:layout_below="@+id/pass"/> 

</RelativeLayout> 

Ihre Aktivität: MainActivity

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

}

+0

Danke, habe ich eine Frage, eine separate Klasse erstellen oder direkt in meiner MainActivity verwenden? oder call ist diese klasse in meinem na create in main? –

+0

warten Ich aktualisiere meinen Code gerade nach ADN Verwenden Sie diese Klasse –

+0

Vielen Dank, ich werde testen und Ihnen zurückgeben, ich bin im Dienst, sobald ich nach Hause komme, werde ich testen! Vielen Dank –

Verwandte Themen