this code work very well but without random heights, i have 2 tubes roof tube and floor tube i am tried to change random heights of tubes but top of roof tube and botom of floor tube genrating in center of screen
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ImageView tube1,tube2;
private int screenW,screenH;
//tube1 x,y
private float tube1X;
private float tube1Y;
//tube2 x,y
private float tube2X;
private float tube2Y;
private Handler handler = new Handler();
private Timer timer = new Timer();
int w = 1200;
int h = 1000;
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(w,h);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tube1 = (ImageView)findViewById(R.id.tube1);
tube2 = (ImageView)findViewById(R.id.tube2);
//get Screen Size:
WindowManager wn = getWindowManager();
Display disply = wn.getDefaultDisplay();
Point size = new Point();
disply.getSize(size);
screenW = size.x;
screenH = size.y;
//move object out of screen
tube1.setX(-80.0f);
tube1.setY(-80.0f);
tube2.setX(-80.0f);
tube2.setY(-80.0f);
//stary the timer
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
changepostub1();
changepostub2();
}
});
}
},0,20);
}// end onCreate
public void changepostub1(){
//tube1
tube1X -=10;
if (tube1.getX()+ tube1.getHeight()<0){
tube1Y = (int)Math.floor(Math.random()*(screenW- tube1.getWidth()));
//tube1.setLayoutParams(layout);
tube1.setRotation(-360);
tube1Y = -10;
tube1X = screenH +100.0f;
}//end if tube1
tube1.setX(tube1X);
tube1.setY(tube1Y);
} //end changepostub1 function
public void changepostub2(){
//tube2
tube2X -=10;
if (tube2.getX()+ tube2.getHeight()< 0){
tube2Y = (int)Math.floor(Math.random()*screenW- tube2.getWidth());
//tube2.setLayoutParams(layout);
tube2Y = +1200;
tube2X = screenH +100.0f;
}//end if tube2
tube2.setX(tube2X);
tube2.setY(tube2Y);
} //end changepostub2 function
}//end class MainActivity
I want hide the top of roof tube out of screen and hide the botom of floor tube out of screen
Aucun commentaire:
Enregistrer un commentaire