滑动布局裁剪bug

时间:2016-09-06 07:43:21

标签: android android-layout android-animation

在我的代码中,我实现了滑动布局。我有一个运行视频的首页,而用户交互是在布局上下滑动的情况下完成的。这是代码:

package org.udoo.androidadkdemobidirect;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.VideoView;

import java.io.DataOutputStream;
import java.io.IOException;
//import org.udoo.androidadkdemobidirect.sAdkManager;


public class MainActivity extends Activity{

//  private static final String TAG = "UDOO_AndroidADKFULL";
    private static String mAdkManager=null;
    private VideoView myVideoView;
    private MediaController mediaControls=null;
    private int position = 0;
    private String ppm_val=null;
    private ToggleButton buttonLED;
    private TextView distance;
    private Button res_nextBtn;
    private TextView res_tv;
    private AdkReadTask mAdkReadTask;
    private int myInt = -10;
    private TextView tv;
    private FrameLayout current=null;
    private Animation slide_down;
    private Animation slide_up;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View decorView = getWindow().getDecorView();

        int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        //hideNavigationBar();
        buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLed);
        distance  = (TextView) findViewById(R.id.textViewIntro);
        buttonLED.setVisibility(View.INVISIBLE);
        tv = (TextView) findViewById(R.id.ppm_unique);
        if (mediaControls == null) {
            mediaControls = new MediaController(this);
        }


        slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_down);

        slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_up);

        res_nextBtn = (Button) findViewById(R.id.result_next_btn);
        res_tv = (TextView) findViewById(R.id.result_number_text);

        myVideoView = (VideoView) findViewById(R.id.videoView);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        myVideoView.getHolder().setFixedSize(size.x, size.y);
        tv.setText(""+size.x+":"+size.y);

        final Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
        animation1.setDuration(1000);
        animation1.setStartOffset(000);

        final Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
        animation2.setDuration(1000);
        animation2.setStartOffset(500);

        //animation1 AnimationListener
        animation1.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationEnd(Animation arg0) {
                // start animation2 when animation1 ends (continue)
                distance.startAnimation(animation2);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

        });



        //animation2 AnimationListener
        animation2.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationEnd(Animation arg0) {
                // start animation1 when animation2 ends (repeat)
                distance.startAnimation(animation1);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

        });

        distance.startAnimation(animation1);

        try {
            myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound_758));

        } catch (Exception e) {
        }

//      myVideoView.requestFocus();
        //we also set an setOnPreparedListener in order to know when the video file is ready for playback
        myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {
                // close the progress bar and play the video
                //progressDialog.dismiss();
                //if we have a position on savedInstanceState, the video playback should start from here
                mediaPlayer.setLooping(true);
                myVideoView.seekTo(position);
                if (position == 0) {
                    myVideoView.start();
                } else {
                    //if we come from a resumed activity, video playback will be paused
                    myVideoView.pause();
                }
            }
        });


        ((MyApplication) this.getApplication()).init(this);
        ((MyApplication) this.getApplication()).open();



        if (mAdkReadTask==null) {
            mAdkReadTask = new AdkReadTask();
            mAdkReadTask.execute();
        }
    }

    /************************************************************************************************
     *                                                                                              *
     *                              OnClickEvents                                                   *
     *                                                                                              *
     ************************************************************************************************/

    public void BR_onBuyClick(View view) {
        next((FrameLayout) findViewById(R.id.flavours_inc_layout));
    }


    public void Flavours_onBackClick(View v){
        next((FrameLayout) findViewById(R.id.br_inc_layout));
    }
    public void Flavours_onF1Click(View view) {
        next((FrameLayout) findViewById(R.id.ty_inc_layout));
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        home();
                    }
                },
                5000);
    }
    public void Flavours_onF2Click(View view) {
        next((FrameLayout) findViewById(R.id.ty_inc_layout));
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        home();
                    }
                },
                5000);
    }
    public void Flavours_onF3Click(View view) {
        next((FrameLayout) findViewById(R.id.ty_inc_layout));
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        home();
                    }
                },
                5000);
    }
    private void home() {
        current.startAnimation(slide_down);
        current.setVisibility(View.INVISIBLE);
        current=null;
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        distance.startAnimation(slide_up);
                        distance.setVisibility(View.VISIBLE);
                        final Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
                        animation1.setDuration(1000);
                        animation1.setStartOffset(000);

                        final Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
                        animation2.setDuration(1000);
                        animation2.setStartOffset(500);

                        //animation1 AnimationListener
                        animation1.setAnimationListener(new Animation.AnimationListener(){

                            @Override
                            public void onAnimationEnd(Animation arg0) {
                                // start animation2 when animation1 ends (continue)
                                distance.startAnimation(animation2);
                            }

                            @Override
                            public void onAnimationRepeat(Animation arg0) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onAnimationStart(Animation arg0) {
                                // TODO Auto-generated method stub

                            }

                        });



                        //animation2 AnimationListener
                        animation2.setAnimationListener(new Animation.AnimationListener(){

                            @Override
                            public void onAnimationEnd(Animation arg0) {
                                // start animation1 when animation2 ends (repeat)
                                distance.startAnimation(animation1);
                            }

                            @Override
                            public void onAnimationRepeat(Animation arg0) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onAnimationStart(Animation arg0) {
                                // TODO Auto-generated method stub

                            }

                        });

                        distance.startAnimation(animation1);
                        ((MyApplication) getApplication()).write("1");
                    }
                },
                1000);
    }

    public void BR_onRecycleClick(View view) {
        next((FrameLayout) findViewById(R.id.ready_inc_layout));
    }

    public void proc_onDoneClick(View v){
        next((FrameLayout) findViewById(R.id.delay_inc_layout));

        onDelay();
    }

    public void proc_onCancelClick(View v){
        next((FrameLayout) findViewById(R.id.br_inc_layout));
    }

    private void onDelay() {
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.delay_progressBar);
        progressBar.setProgress(0);
        progressBar.setVisibility(View.VISIBLE);
        ValueAnimator animator = new ValueAnimator();

        animator.setObjectValues(0, 100);
        animator.setDuration(7000);
        final TextView tv = (TextView) findViewById(R.id.delay_textPoints);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                tv.setText("" + animation.getAnimatedValue() +"%");
            }
        });
        animator.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animator) {
                progressBar.setVisibility(View.INVISIBLE);
                ((MyApplication) getApplication()).write("4");
                next((FrameLayout) findViewById(R.id.result_inc_layout));
                ((MyApplication) getApplication()).write("8");
                res_nextBtn.setVisibility(View.INVISIBLE);

            }

        });

        animator.start();
    }

    public void Res_onNextClick(View v){
        next((FrameLayout) findViewById(R.id.points_inc_layout));
        onPoints();
    }

    private void onPoints() {
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.points_progressBar);
        progressBar.setProgress(0);
        progressBar.setVisibility(View.VISIBLE);
        final ValueAnimator animator = new ValueAnimator();
        if (myInt>=0) {
            animator.setObjectValues(0, (myInt) * 5);
        }
        else{
            animator.setObjectValues(0, 1);
        }
        animator.setDuration(2000);

        final TextView tv = (TextView) findViewById(R.id.points_textPoints);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                tv.setText("" + animation.getAnimatedValue());
            }
        });
        animator.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animator) {
                progressBar.setVisibility(View.INVISIBLE);
            }

        });
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        animator.start();
                    }
                },
                1500);

    }

    public void pts_onConfirmClick(View v){
        next((FrameLayout) findViewById(R.id.end_inc_layout));
        ((MyApplication) getApplication()).write("3");
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        home();
                    }
                },
                5000);
    }

    public void Ready_onYesClick(View v){
        ((MyApplication) this.getApplication()).write("2");
        next((FrameLayout) findViewById(R.id.processing_inc_layout));
        onProcessing();
    }

    private void onProcessing() {
        final int[] i = {0};
        final int[] drawablearray = new int[]{R.drawable.processing_butts_3, R.drawable.processing_butts_2, R.drawable.processing_butts_1, R.drawable.processing_butts_0};
        final ImageView backgroundImageView = (ImageView) findViewById(R.id.processing_textButts);

        backgroundImageView.postDelayed(new Runnable() {

            public void run() {
                backgroundImageView.setImageResource(drawablearray[i[0]++ % drawablearray.length]);
                backgroundImageView.postDelayed(this, 1000);
            }
        }, 1500);
    }

    public void Ready_onCancelClick(View v){
        next((FrameLayout) findViewById(R.id.br_inc_layout));
    }


    @Override
    public void onResume() {
        super.onResume();
        ((MyApplication) this.getApplication()).write("1");
    }

    @Override
    public void onPause() {
        super.onPause();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void hideNavigationBar() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            /*
                enable  = enable navigation bar
                disable = disable navigation bar
             */
            os.writeBytes("pm enable com.android.systemui\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            //////////////////////////////////////
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String output="";
    /* 
     * We put the readSerial() method in an AsyncTask to run the 
     * continuous read task out of the UI main thread
     */
    private class AdkReadTask extends AsyncTask<Void, String, String> {

        private boolean running = true;

        public void pause(){
            running = false;
        }
        public void start(){
            running = true;
        }

        @Override
        protected String doInBackground(Void... params) {
            while(running) {
                if (this.isCancelled())
                    break;
                String s = ((MyApplication) getApplication()).read();
                publishProgress(s);
            }

            return output;
        }

        @Override
        protected void onProgressUpdate(String... progress) {
            if(progress[0].charAt(0)=='P') {
                /*
                    TODO:   change text+ppm_val to image+text
                    TODO:   send ppm values to server
                 */
                tv.setText("Air quality: " + progress[0].substring(1, progress[0].length()));
                ppm_val = progress[0].substring(1, progress[0].length());
                ((MyApplication) getApplication()).setPPM(ppm_val);
            }
            else if (progress[0].charAt(0)=='R'){
                /*
                    TODO:   add server verification of RFID
                 */
                    distance.startAnimation(slide_down);
                    distance.setVisibility(View.GONE);
                    next((FrameLayout) findViewById(R.id.br_inc_layout));
            }
            else if (progress[0].charAt(0)=='C'){
                /*
                    TODO:   send data to server
                 */
                res_tv.setText(""+(int) progress[0].charAt(1));
                myInt = (int) progress[0].charAt(1);
                res_nextBtn.setVisibility(View.VISIBLE);
            }
        }
        @Override
        protected void onPostExecute(String result){
        }
    }

    private void next(final FrameLayout fl) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fl.getLayoutParams();
        if (fl == (FrameLayout) findViewById(R.id.flavours_inc_layout)){
            params.height = (int) (size.y*0.6);
        }
        else{
            params.height = (int) (size.y*0.3);
        }
        params.width = size.x;
        int delay_delta = 1000;
        // Set it back.
        fl.setLayoutParams(params);
        if(current!=null){
            current.startAnimation(slide_down);
            current.setVisibility(View.INVISIBLE);
            delay_delta=0;

        }
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        fl.startAnimation(slide_up);
                        fl.setVisibility(View.VISIBLE);
                        current = fl;
                    }
                },
                1200-delay_delta);
    }

}

在我尝试更改滑动高度之前,一切正常。我为一个0.6 * y而不是0.3 * y的布局设置了一个特殊情况。布局未显示在0.3标记以上。它确实起作用,当我点击假想的按钮位置时,它会在向下滑动之前显示。

1 个答案:

答案 0 :(得分:0)

好的,它是偶然解决的。我将所有View.INVISIBLE更改为View.GONE,它就像瑞士手表一样。我猜这里有一些黑魔法,如果有人可以向我解释为什么会这样,我会非常感激(并接受答案)。感谢。