I have a StartActivity which has a play button, when pressed it takes you to the MainActivty where there are 9 ImageView objects which are set to the bitmap img "aliencreature" as you will see in my xml file below. The imageview objects are to be set to random positions on startup i used display metrics and random to do this however my first problem is that they are out of bounds at times. Not all 9 imageviews show in the screen and sometimes half an imageview is shown and even if all do show up 1 or two are imageviews get smaller for some reason. I've attached my .java and xml file please tell me if you need me to be more detailed in my question.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tapoo.MainActivity$PlaceholderFragment"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="22dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="147dp"
android:layout_marginLeft="100dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="108dp"
android:layout_marginLeft="200dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="170dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_marginLeft="108dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="740dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/restartButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/restartbutton"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/countDownTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Seconds remaining: 10"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:layout_marginLeft="188dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/scoreNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:visibility="invisible"
android:textSize="19sp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_marginLeft="188dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:layout_marginTop="350dp"
android:src="@drawable/aliencreature"
tools:ignore="ContentDescription" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
setContentView(R.layout.fragment_main);
counter = 0;
score = (TextView) findViewById(R.id.scoreNumber);
countDownTimer = (TextView) findViewById(R.id.countDownTimer);
view1 = (ImageView) findViewById(R.id.imageView1);
view2 = (ImageView) findViewById(R.id.imageView2);
view3 = (ImageView) findViewById(R.id.imageView3);
view4 = (ImageView) findViewById(R.id.imageView4);
view5 = (ImageView) findViewById(R.id.imageView5);
view6 = (ImageView) findViewById(R.id.imageView6);
view7 = (ImageView) findViewById(R.id.imageView7);
view8 = (ImageView) findViewById(R.id.imageView8);
view9 = (ImageView) findViewById(R.id.imageView9);
allImages = new ArrayList<ImageView>();
allImages.add(view1);
allImages.add(view2);
allImages.add(view3);
allImages.add(view4);
allImages.add(view5);
allImages.add(view6);
allImages.add(view7);
allImages.add(view8);
allImages.add(view9);
for(final ImageView views : allImages){ //allimages is an arraylist of imageviews that contains my 9 imageviews
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) views.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels ;
Random r = new Random();
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50) ; //50 is the height of my bitmap img
views.setLayoutParams(params);
If you check your value in the file /value/dimen of your project you will find that hte following
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
are not null. So due to your RelativeLayout definition in your main layout you will find that the drawable area is smaller than the entire screen so
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50)
won't be sufficient. My suggestion is to remove the padding definition in your RelativeLayout, like this your images will always be on the screen.
Second thing, remove in your ImageView definition
tools:ignore="ContentDescription"
After these corrections everything would be fine;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.