ImageView对象超出了屏幕范围

时间:2014-06-26 14:26:44

标签: java android android-layout

我有一个带有播放按钮的StartActivity,当按下它时会将你带到MainActivty,其中有9个ImageView对象被设置为位图img“aliencreature”,你将在下面的xml文件中看到。 imageview对象将在启动时设置为随机位置我使用显示指标并随机执行此操作但是我的第一个问题是它们有时会超出界限。并非所有9个图像视图都显示在屏幕上,有时会显示半个图像视图,即使所有图像视图都显示1或2个图像视图由于某种原因变小。我已经附上了我的.java和xml文件,请告诉我你是否需要我在我的问题中更加详细。

<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);

1 个答案:

答案 0 :(得分:0)

如果您在项目的文件/值/维度中检查您的值,您会发现以下

 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"

不为空。因此,由于您的主布局中的RelativeLayout定义,您会发现可绘制区域小于整个屏幕所以

    params.leftMargin =  r.nextInt(width-48)  ; // 48 is the width of my bitmap img
    params.topMargin =  r.nextInt(height-50)

赢得不够。 我的建议是删除RelativeLayout中的填充定义,这样你的图像将永远在屏幕上。

其次,在ImageView定义中删除

   tools:ignore="ContentDescription"

经过这些修正后,一切都会好的;)