为什么显示两个布局之间的黑色背景 [英] why display black background between two layouts

查看:201
本文介绍了为什么显示两个布局之间的黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有电台应用程序流,当打开这个程序,首先它的开放飞溅的布局是这样的:

I have radio app stream, and when open this app, first its open splash layout like this :

splash.xml

splash.xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/loading" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.67" />

</LinearLayout>

和类是这样的:

StartPoint.java

StartPoint.java

public class StartPoint extends Activity{

ProgressBar progressBar;
private int progressBarStatus = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    progressBar = (ProgressBar)findViewById(R.id.progressBar1);


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
                while(progressBarStatus < 5000){
                    StartPoint.this.runOnUiThread(new Runnable(){
                        public void run()
                        {
                            progressBar.setProgress(progressBarStatus);
                            progressBarStatus += 1000;
                        }
                    });

                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openMainList = new Intent(StartPoint.this, 
                                           com.example.kam.MainActivity.class);
                startActivity(openMainList);
            }
        }
    };
    timer.start();
}

@Override
protected void onStop(){
   super.onStop();
   finish();
}

}

完成后这个类,它的呼叫MainActivity.java类,它的显示:

this class when finished, its call MainActivity.java class, and its show :

MainActivity.java

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
    // Define ..............................................................
    private static ProgressDialog progressDialog;
    public MediaPlayer mp;
    boolean isPrepared = false;
    Button PlayBtn, ExitBtn, PauseBtn, RefreshBtn;
    String MEDIA_PATH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressDialog = ProgressDialog.show(MainActivity.this, "", 
                                                  "Buffering Radio...", true);
        progressDialog.setCancelable(false);
        // Declare
        // ..............................................................
        mp = new MediaPlayer();
        PlayBtn = (Button) findViewById(R.id.btnPlay);
        PlayBtn.setOnClickListener(this);
        PauseBtn = (Button) findViewById(R.id.btnPause);
        PauseBtn.setOnClickListener(this);
        RefreshBtn = (Button) findViewById(R.id.btnRefresh);
        RefreshBtn.setOnClickListener(this);
        ExitBtn = (Button) findViewById(R.id.btnExit);
        ExitBtn.setOnClickListener(this);
        MEDIA_PATH = "http://radio.arabhosters.com:8015/";

        // Volume Control
        // ..............................................................
        final AudioManager leftAm = (AudioManager)
                                      getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = leftAm.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = leftAm.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar) findViewById(R.id.volumebar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener
                                      (new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged
                                (SeekBar arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub
            leftAm.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
             }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == PlayBtn) {
            startradio();
        }
        else if (v == PauseBtn) {
            pauseradio();
        }
        else if (v == ExitBtn) {
            exitradio();
        }
        else if (v == RefreshBtn) {
            try {
                refreshradio();
            }
            catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void onCompletion(MediaPlayer mediaPlayer) {
        synchronized (this) {
            isPrepared = false;
        }
    }

    protected void onResume() {
        super.onResume();

        try {
            mp.setDataSource(MEDIA_PATH);
        }
        catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        }
        catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // also consider mp.prepareAsync().
            // defult start stream when start App.
        mp.start();
        mp.setVolume(100, 100);
        progressDialog.dismiss();
    }

    // method for play stream after stop it.
    public void startradio() {
        try {
            if (mp.isPlaying()) {
                return;
            }
            mp.start();
            progressDialog.dismiss();
        }
        catch (IllegalStateException ex) {
            ex.printStackTrace();
        }
    }

    // method for Refresh stream.
    public void refreshradio() throws IllegalArgumentException, 
                                       SecurityException, IOException {
        try {
            if (mp.isPlaying()) {
                return;
            }
            mp.reset();
            mp.setDataSource(MEDIA_PATH);
            mp.prepare();
            mp.start();
            progressDialog.dismiss();
        }
        catch (IllegalStateException ex) {
            ex.printStackTrace();
        }
    }

    // method for pause stream.
    public void pauseradio() {
        mp.pause();
    }

    // method for check is radio paly or not stream
    public boolean isPlaying() {
        return mp.isPlaying();
    }

    // method for Looping audio if your record it - Soon :)
    public boolean isLooping() {
        return mp.isLooping();
    }

    // method for Looping audio if your record it - Soon :)
    public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

    // method for volume
    public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

    // method for stop stream.
    public void stopradio() {
        if (mp.isPlaying()) {
            mp.stop();
        }
        mp.release();
    }

    // method for exit.
    public void exitradio() {
        finish();
        System.exit(0);
    }

    // method for back to main menu "Home".
    public void backtomenu() {
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

和布局是这样的:

activity_main.xml中

activity_main.xml

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
         >

        <LinearLayout
            android:layout_weight="6" 
            android:orientation="vertical" 
            android:layout_alignParentTop="true" 
            android:layout_alignParentLeft="true" 
            android:layout_width="fill_parent" 
            android:layout_height="0dip" >

        </LinearLayout>

        <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_alignParentLeft="true"
            android:layout_weight="0.62"
            android:background="@drawable/iconbgrepate"
            android:gravity="center"
            android:orientation="horizontal"
            android:tileMode="repeat"
            android:weightSum="5" 
            android:alpha=".75">

        <SeekBar
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/volumebar"
            android:max="100"
            android:paddingBottom="10dip"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_alignParentLeft="true"
            android:layout_weight="0.62"
            android:background="@drawable/iconbgrepate"
            android:gravity="center"
            android:orientation="horizontal"
            android:tileMode="repeat"
            android:weightSum="5" >

           <Button
               android:id="@+id/btnRefresh"
               android:layout_width="32dp"
               android:layout_height="match_parent"
               android:layout_margin="5dp"
               android:layout_weight="0.10"
               android:background="@drawable/refreshbutton" />

           <View android:layout_weight="1" 
               android:layout_width="0dip" 
               android:layout_height="0dip" />

            <Button
                android:id="@+id/btnPause"
                android:layout_width="32dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="0.05"
                android:background="@drawable/pausebutton" />

            <View android:layout_weight="1" 
                android:layout_width="0dip" 
                android:layout_height="0dip" />

            <Button
                android:id="@+id/btnPlay"
                android:layout_width="32dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="0.05"
                android:background="@drawable/playbutton" />

            <View android:layout_weight="1" 
                android:layout_width="0dip" 
                android:layout_height="0dip" />

            <Button
                android:id="@+id/btnExit"
                android:layout_width="32dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="0.12"
                android:background="@drawable/exitbutton" />

        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

为什么当反从 splash.xml activity_main.xml中显示黑色背景几个分和显示应用程序。

why when trans from splash.xml to activity_main.xml display black background few min and display app.

推荐答案

尝试

 Thread timer = new Thread(){
    public void run(){
        try{

            while(progressBarStatus < 5000){
                StartPoint.this.runOnUiThread(new Runnable(){
                    public void run()
                    {
                        progressBar.setProgress(progressBarStatus);
                        progressBarStatus += 1000;
                    }
                });
                    sleep(5000);
            }

这篇关于为什么显示两个布局之间的黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆