0
我已經設置了兩行水平滑動動畫的四個圖像瀏覽,頂部圖像滑動到右側,底部向左滑動,我很想知道爲什麼我的圖像眨眼當他們動畫時,我會向任何有想法的人提供任何見解,謝謝!難以理解爲什麼我的圖像視圖閃爍
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);
}
}
不看你的代碼:你使用雙緩衝? –
我不熟悉doublebuffering – user1091368
後快速谷歌搜索我假設你建議它可能抵消閃爍 – user1091368