难以理解为什么我的图像视图会闪烁

时间:2016-05-14 00:15:29

标签: java android animation timer

我已经设置了两行水平滑动动画的四个图像视图,顶部图像向右滑动,底部向左滑动,我很想知道为什么我的图像在动画时会眨眼,I& #39; d接受任何有想法的人的见解,谢谢!

public class MainActivity extends Activity {

Display mainDisplay;
ViewGroup mainLayout;
RelativeLayout.LayoutParams layoutPositioner;
RelativeLayout tsubslayout;
Context ourContext;
ImageView subone_Iv, subtwo_Iv, subthree_Iv, subfour_Iv, blur_Iv, pickup_Iv, delivery_Iv, logo_Iv;
TimerTask thisTimerTask;
Timer thisTimer;

float offsetX = 0.0f;
float offsetY = 0.0f;

float subonexpos = -1440;
float suboneypos = 0;

float subtwoxpos = 0;
float subtwoypos = 0;

float subthreexpos = 0;
float subthreeypos = 1280;

float subfourxpos = 1440;
float subfourypos = 1280;

float imgPosOne = 1;
float imgPosTwo = 1;

float timerUntilTitleScreen = 500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//Hides notification bar
    this.setContentView(R.layout.activity_main);//set content view AFTER ABOVE sequence (to avoid crash)

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mainDisplay = getWindowManager().getDefaultDisplay();
    mainLayout = (ViewGroup) findViewById(R.id.id_layout);

    DisplayMetrics m = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(m);
    int windowHeight = m.heightPixels;
    int windowWidth = m.widthPixels;

    offsetX = (windowWidth / 1440.0f);
    offsetY = (windowHeight / 2560.0f);

    ourContext = this;

    transitioningImages();

    logo_Iv = new ImageView(this);
    SetImgPos(0, 0, 1440, 2560, logo_Iv);
    logo_Iv.setImageResource(R.drawable.logo);
    logo_Iv.setLayoutParams(layoutPositioner);
    mainLayout.addView(logo_Iv);

    thisTimerTask = new ThisClass();
    thisTimer = new Timer();
    thisTimer.scheduleAtFixedRate(thisTimerTask, 16, 16);

  }

void transitioningImages()
{

    subone_Iv = new ImageView(this);
    SetImgPos(subonexpos, suboneypos, 1440, 1280, subone_Iv);
    subone_Iv.setVisibility(View.INVISIBLE);
    subone_Iv.setLayoutParams(layoutPositioner);
    mainLayout.addView(subone_Iv);

    subtwo_Iv = new ImageView(this);
    SetImgPos(subtwoxpos, subtwoypos, 1440, 1280, subtwo_Iv);
    subtwo_Iv.setVisibility(View.INVISIBLE);
    subtwo_Iv.setLayoutParams(layoutPositioner);
    mainLayout.addView(subtwo_Iv);

    subthree_Iv = new ImageView(this);
    SetImgPos(subthreexpos, subthreeypos, 1440, 1280, subthree_Iv);
    subthree_Iv.setVisibility(View.INVISIBLE);
    subthree_Iv.setLayoutParams(layoutPositioner);
    mainLayout.addView(subthree_Iv);

    subfour_Iv = new ImageView(this);
    SetImgPos(subfourxpos, subfourypos, 1440, 1280, subfour_Iv);
    subfour_Iv.setVisibility(View.INVISIBLE);
    subfour_Iv.setLayoutParams(layoutPositioner);
    mainLayout.addView(subfour_Iv);

}

void imgAppearanceUpdate()
{
    if(imgPosOne == 1)
    {
        subone_Iv.setBackgroundColor(0xff8A2BE2); //purple
        subtwo_Iv.setBackgroundColor(0xff7FFF00); //lime green
    }

    else if(imgPosOne == 2)
    {
        subone_Iv.setBackgroundColor(0xff7FFF00); //lime green
        subtwo_Iv.setBackgroundColor(0xff8A2BE2); //purple
    }


    if(imgPosTwo == 1)
    {
        subthree_Iv.setBackgroundColor(0xffFF8C00); //orange
        subfour_Iv.setBackgroundColor(0xff00BFFF); //aquablue
    }

    else if(imgPosTwo == 2)
    {
        subthree_Iv.setBackgroundColor(0xff00BFFF); //aquablue
        subfour_Iv.setBackgroundColor(0xffFF8C00); //orange
    }


}

void imgPosUpdate() {

    if (subonexpos < 0) {
        subonexpos += 6;
        subtwoxpos += 6;

        SetImgPos(subonexpos, suboneypos, 1440, 1280, subone_Iv);
        subone_Iv.setLayoutParams(layoutPositioner);

        SetImgPos(subtwoxpos, subtwoypos, 1440, 1280, subtwo_Iv);
        subtwo_Iv.setLayoutParams(layoutPositioner);
    }

    else if (subonexpos == 0) {
        imgPosOne += 1;
        if (imgPosOne == 3) {
            imgPosOne = 1;
        }

        subonexpos = -1440;
        subtwoxpos = 0;

        SetImgPos(subonexpos, suboneypos, 1440, 1280, subone_Iv);
        subone_Iv.setLayoutParams(layoutPositioner);

        SetImgPos(subtwoxpos, subtwoypos, 1440, 1280, subtwo_Iv);
        subtwo_Iv.setLayoutParams(layoutPositioner);
    }



    if (subthreexpos > -1440) {
        subthreexpos -= 6;
        subfourxpos -= 6;

        SetImgPos(subthreexpos, subthreeypos, 1440, 1280, subthree_Iv);
        subthree_Iv.setLayoutParams(layoutPositioner);

        SetImgPos(subfourxpos, subfourypos, 1440, 1280, subfour_Iv);
        subfour_Iv.setLayoutParams(layoutPositioner);
    }

    else if (subthreexpos == -1440) {
        imgPosTwo += 1;
        if (imgPosTwo == 3) {
            imgPosTwo = 1;
        }

        subthreexpos = 0;
        subfourxpos = 1440;

        SetImgPos(subthreexpos, subthreeypos, 1440, 1280, subthree_Iv);
        subthree_Iv.setLayoutParams(layoutPositioner);

        SetImgPos(subfourxpos, subfourypos, 1440, 1280, subfour_Iv);
        subfour_Iv.setLayoutParams(layoutPositioner);
    }



}

class ThisClass extends TimerTask {

    @Override
    public void run() {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (timerUntilTitleScreen > 0)
                {timerUntilTitleScreen -= 2;}



                if (timerUntilTitleScreen < 20) {
                    logo_Iv.setVisibility(View.INVISIBLE);
                    delivery_Iv.setVisibility(View.VISIBLE);
                    pickup_Iv.setVisibility(View.VISIBLE);
                    subone_Iv.setVisibility(View.VISIBLE);
                    subtwo_Iv.setVisibility(View.VISIBLE);
                    subthree_Iv.setVisibility(View.VISIBLE);
                    subfour_Iv.setVisibility(View.VISIBLE);

                    imgPosUpdate();
                    imgAppearanceUpdate(); //swapping images when they go off screen
                }



            }
        });
    }
}

public void SetImgPos(float x, float y, float width, float height, ImageView theView) {
    layoutPositioner = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    // offsetY/offsetX is the difference in ratio of the current screen
    int y_Margin = (int) (offsetY * y); //changes the y position coordinates to adjust to the ratio of the current screen it is being seen on
    int x_Margin = (int) (offsetX * x); //changes the x position coordinates to adjust to the ratio of the current screen it is being seen on

    layoutPositioner.topMargin = y_Margin; //adjusts where the image's margin begins
    layoutPositioner.leftMargin = x_Margin; //adjusts where the image's margin begins

    layoutPositioner.bottomMargin = (int) offsetY - (y_Margin - theView.getHeight()); //adjusts where the image's margin ends
    layoutPositioner.rightMargin = (int) offsetX - (x_Margin - theView.getWidth()); //adjusts where the image's margin ends

    layoutPositioner.width = (int) (width * offsetX); //adjust the image/layout size to the ratio of the current screen its being seen on
    layoutPositioner.height = (int) (height * offsetY);

}

}

1 个答案:

答案 0 :(得分:0)

在我的SetImgPos函数中偶然发现了解决方案...

layoutPositioner.bottomMargin = (int) offsetY - (y_Margin - theView.getHeight()); 
layoutPositioner.rightMargin = (int) offsetX - (x_Margin - theView.getWidth());

对于我的应用来说显然太多了,并将其更改为...

layoutPositioner.bottomMargin = (int) offsetY - (int) (y_Margin - height); 
layoutPositioner.rightMargin = (int) offsetX - (int) (x_Margin - width);

删除了闪烁的

相关问题