在两个活动之间创建加载屏幕

时间:2017-11-13 16:46:57

标签: android android-asynctask

我有两项活动(activity_1和activity_2)。我在第二个活动中有一个CustomPagerAdapter,因此需要15秒才能启动。 问题是,当我尝试启动第二个活动时,它会显示黑屏大约15秒(加载所有方法和数据所需的时间)。我想向用户展示一个进度条,甚至只是一个图像或一个动画 -  除了简单的黑屏外什么都没有。 我发现了很多像这样的问题,但没有人使用寻呼机适配器,这对我来说很难。

这是第二项活动:

public class hiphopvol1 extends Activity {
MediaPlayer[] array = new MediaPlayer[66],array2 = new MediaPlayer[70];
SoundPool[] soundPool=new SoundPool[66],soundPool2=new SoundPool[70];
int[] sm=new int[66],sm2=new int[70];

public void backtoback(View v) {
    Intent readMore = new Intent(v.getContext(), realmusic.class);
    v.getContext().startActivity(readMore);
    for(int i=0;i<66;i++){
        if(array[i].isPlaying())
        array[i].stop();
        array[i].reset();
        array[i].release();
        soundPool[i].stop(sm[i]);
        soundPool[i].release();
    }
    for(int i=0;i<70;i++){
        if(array2[i].isPlaying())
        array2[i].stop();
        array2[i].reset();
        array2[i].release();
        soundPool2[i].stop(sm2[i]);
        soundPool2[i].release();
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slider_hiphop);
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager2);
    viewPager.setAdapter(new CustomPagerAdapter3());
}
public class CustomPagerAdapter3 extends PagerAdapter {
//    private SoundPool[] soundPool=new SoundPool[66],soundPool2=new SoundPool[70];
//    private int[] sm=new int[66],sm2=new int[70];
    private int[] streamid=new int[66],streamid2=new int[70];
    boolean loaded = false;private float volume;
    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }
    @Override
    public void destroyItem(View view, int value, Object object) {

        ((ViewPager) view).removeView((View) object);
    }

    @Override
    public Parcelable saveState() {

        return null;
    }
    @Override
    public Object instantiateItem(final View collection, int position) {
        final  Button[] buttons=new Button[66],buttons2=new Button[70];
        final  CheckBox[] checkboxes=new CheckBox[66],checkboxes2=new CheckBox[70];
       // final  MediaPlayer[] array = new MediaPlayer[66],array2 = new MediaPlayer[70];
        for(int s=0;s<66;s++){array[s]= new MediaPlayer();}for(int s=0;s<70;s++){array2[s]= new MediaPlayer();}

        LayoutInflater layoutInflater  = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int v=0;
        final  AssetFileDescriptor[] descripto =new AssetFileDescriptor[66],descripto2 =new AssetFileDescriptor[70];
        View view;
        LinearLayout container,container2;
        AudioManager audioManager = (AudioManager) collection.getContext().getSystemService(collection.getContext().AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        volume = actualVolume / maxVolume;
        switch (position) {
            case 0:
            default:
                view=layoutInflater.inflate(R.layout.hhv1_s1,null);
                container = (LinearLayout)view.findViewById(R.id.container);
                for(int s=0;s<66;s++) {
                    soundPool[s] = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
                    soundPool[s].setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                        @Override
                        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                            loaded = true;
                        }
                    });
                    try {
                        if(s<30)
                            descripto[s]= collection.getContext().getAssets().openFd("hhv1/hhv1drums/" +(s+1)+".ogg");
                        else if(s>=30 && s <44)
                            descripto[s]= collection.getContext().getAssets().openFd("hhv1/hhv1bass/" +(s+1)+".ogg");
                        else if(s>=44 && s<49)
                            descripto[s]= collection.getContext().getAssets().openFd("hhv1/hhv1guitar/" +(s+1)+".ogg");
                        else descripto[s]= collection.getContext().getAssets().openFd("hhv1/hhv1keys/" +(s+1)+".ogg");
                    } catch (IOException e) {}
                    sm[s] = soundPool[s].load(descripto[s],1);
                }
                for(int k=1;k<133;k+=2){
                    final View addView = layoutInflater.inflate(R.layout.button, null);
                    addView.setPadding(0,0,0,30);
                    CheckBox textOut = (CheckBox) addView.findViewById(R.id.cb);
                    Button buttonRemove = (Button)addView.findViewById(R.id.b);
                    textOut.setId((k+1));buttonRemove.setId(k);
                    container.addView(addView);
                    buttons[v]=((Button) view.findViewById(buttonRemove.getId()));
                    checkboxes[v] = ((CheckBox) view.findViewById(textOut.getId()));
                    if(v<30) buttons[v].setText("Drum "+(v+1));
                    else if(v>=30 && v <44)buttons[v].setText("Bass "+(v+1));
                    else if(v>=44 && v<49)buttons[v].setText("Guitar "+(v+1));
                    else buttons[v].setText("Keys "+(v+1));
                    buttons[v].setId(k);
                    buttons[v].setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(final View v){
                            if (array[(v.getId()-1)/2].isPlaying()) {
                                array[(v.getId()-1)/2].pause();
                                array[(v.getId()-1)/2].seekTo(0);
                                array[(v.getId()-1)/2].reset();
                                buttons[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect3);
                                return;}
                            array[(v.getId()-1)/2].reset();
                            try {
                                array[(v.getId()-1)/2].setDataSource(descripto[(v.getId()-1)/2].getFileDescriptor(), descripto[(v.getId()-1)/2].getStartOffset(), descripto[(v.getId()-1)/2].getLength());
                                array[(v.getId()-1)/2].prepare();
                            } catch (IOException e) {}
                            array[(v.getId()-1)/2].setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                public void onCompletion(MediaPlayer mp) {
                                    mp.reset();
                                    buttons[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect3);
                                };});
                            array[(v.getId()-1)/2].start();
                            buttons[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect4);
                        }
                    });
                    checkboxes[v].setId((k+1));
                    checkboxes[v].setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(final View v){
                            if (checkboxes[(v.getId()/2)-1].isChecked()) {
                                if (loaded) {
                                    if(array[(v.getId()/2)-1].isPlaying()){
                                        array[(v.getId()/2)-1].pause();
                                        array[(v.getId()/2)-1].seekTo(0);
                                        array[(v.getId()/2)-1].reset();
                                        buttons[(v.getId()/2)-1].setBackgroundResource(R.drawable.myrect3);}
                                    streamid[(v.getId()/2)-1] = soundPool[(v.getId()/2)-1].play(sm[(v.getId()/2)-1], volume, volume, 1, 100, 1f);
                                    v.setBackgroundResource(R.drawable.myrect4);
                                }
                            } else {
                                soundPool[(v.getId()/2)-1].stop(streamid[(v.getId()/2)-1]);
                                soundPool[(v.getId()/2)-1].setLoop(streamid[(v.getId()/2)-1],0);
                                v.setBackgroundResource(R.drawable.myrect3);
                            }
                        }
                    });
                    v++;}
                break;

            case 1:
                view = layoutInflater.inflate(R.layout.hhv1_s2, null);
                container2 = (LinearLayout)view.findViewById(R.id.container);
                for(int s=0;s<70;s++) { soundPool2[s] = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
                    soundPool2[s].setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                        @Override
                        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                            loaded = true;
                        }
                    });}
                for (int i=0;i<70;i++){
                    try {
                        if(i<19)
                            descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1synth/" +(i+1)+".ogg");
                        else if(i>=19 && i <27)
                            descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1strings/" +(i+1)+".ogg");
                        else if(i>=27 && i<45)
                            descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1sequence/" +(i+1)+".ogg");
                        else if(i>=45 && i<54)
                            descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1pad/" +(i+1)+".ogg");
                        else if(i>=54 && i<56)
                            descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1brass/" +(i+1)+".ogg");
                        else descripto2[i]= collection.getContext().getAssets().openFd("hhv1/hhv1fx/" +(i+1)+".ogg");
                    } catch (IOException e) {}
                    sm2[i] = soundPool2[i].load(descripto2[i],1);}

                for(int k=1;k<141;k+=2){
                    final View addView2 = layoutInflater.inflate(R.layout.button, null);
                    addView2.setPadding(0,0,0,30);
                    CheckBox textOut = (CheckBox) addView2.findViewById(R.id.cb);
                    Button buttonRemove = (Button)addView2.findViewById(R.id.b);
                    textOut.setId((k+1));buttonRemove.setId(k);
                    container2.addView(addView2);
                    buttons2[v]=((Button) view.findViewById(buttonRemove.getId()));
                    checkboxes2[v] = ((CheckBox) view.findViewById(textOut.getId()));
                    v++;}
                v=0;
                for(int j=1;j<141;j+=2){
                    if(v<19) buttons2[v].setText("Synth "+(v+1));
                    else if(v>=19 && v <27)buttons2[v].setText("Strings "+(v+1));
                    else if(v>=27 && v<45)buttons2[v].setText("Sequence "+(v+1));
                    else if(v>=45 && v<54)buttons2[v].setText("Pad "+(v+1));
                    else if(v>=54 && v<56)buttons2[v].setText("Brass "+(v+1));
                    else buttons2[v].setText("Fx "+(v+1));
                    buttons2[v].setId(j);
                    buttons2[v].setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(final View v){
                            if (array2[(v.getId()-1)/2].isPlaying()) {
                                array2[(v.getId()-1)/2].pause();
                                array2[(v.getId()-1)/2].seekTo(0);
                                array2[(v.getId()-1)/2].reset();
                                buttons2[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect3);
                                return;}
                            array2[(v.getId()-1)/2].reset();
                            try {
                                array2[(v.getId()-1)/2].setDataSource(descripto2[(v.getId()-1)/2].getFileDescriptor(), descripto2[(v.getId()-1)/2].getStartOffset(), descripto2[(v.getId()-1)/2].getLength());
                                array2[(v.getId()-1)/2].prepare();
                            } catch (IOException e) {}
                            array2[(v.getId()-1)/2].setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                public void onCompletion(MediaPlayer mp) {
                                    mp.reset();
                                    buttons2[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect3);
                                };});
                            array2[(v.getId()-1)/2].start();
                            buttons2[(v.getId()-1)/2].setBackgroundResource(R.drawable.myrect4);
                        }
                    });
                    checkboxes2[v].setId((j+1));
                    checkboxes2[v].setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                            if (checkboxes2[(v.getId()/2)-1].isChecked()) {
                                if (loaded) {
                                    if(array2[(v.getId()/2)-1].isPlaying()){
                                        array2[(v.getId()/2)-1].pause();
                                        array2[(v.getId()/2)-1].seekTo(0);
                                        array2[(v.getId()/2)-1].reset();
                                        buttons2[(v.getId()/2)-1].setBackgroundResource(R.drawable.myrect3);}
                                    streamid2[(v.getId()/2)-1] = soundPool2[(v.getId()/2)-1].play(sm2[(v.getId()/2)-1], volume, volume, 1, 100, 1f);
                                    v.setBackgroundResource(R.drawable.myrect4);
                                }
                            } else {
                                soundPool2[(v.getId()/2)-1].stop(streamid2[(v.getId()/2)-1]);
                                soundPool2[(v.getId()/2)-1].setLoop(streamid2[(v.getId()/2)-1],0);
                                v.setBackgroundResource(R.drawable.myrect3);
                            }}});v++;}
                break;


        }
        ((ViewPager) collection).addView(view);
        return view;
    }
}

}

0 个答案:

没有答案