如何设置图片的大小和位置?

发布于 2024-11-05 12:43:38 字数 2063 浏览 0 评论 0原文

我正在尝试设置屏幕上图像(image2)的大小和位置,以便无论屏幕大小如何,它相对于背景图像(image1)都处于相同的位置和大小。为此,我尝试在 Activity 类的 onCreate 方法中更改 image2 的布局参数:

    public class Game_main extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);



        ImageView image2= (ImageView) findViewById(R.id.seed1);
        ImageView image1= (ImageView) findViewById(R.id.board_rev1);


        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
        params.leftMargin = (image1.getWidth())/2;
        params.topMargin = (image1.getHeight())/4;
        rl.addView(image2, params);
        setContentView(R.layout.gamerev);


}


}

但是当我的应用程序转到此 Activity 时,我收到错误:说应用程序意外停止。然后我必须强制关闭该应用程序。为什么会出现这种情况呢?我有什么遗漏的吗?或者有更好的方法来做到这一点吗?

logcat中的错误是:

E/AndroidRuntime(296): java.lang.RuntimeException: 无法启动活动Co mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc 但我不明白

为什么任何内容都会为空,因为所有 id 都存在于我的 xml 文件中:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/rlGame"
  >
    <ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
    <ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>

</RelativeLayout>

谢谢

I am trying to set the size and position of an image (image2) on the screen so that it is in the same position and size relative to the background image (image1) it is on no matter the screen size. To do this, I try to alter the layout parameters of image2 in my onCreate method of my Activity class:

    public class Game_main extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);



        ImageView image2= (ImageView) findViewById(R.id.seed1);
        ImageView image1= (ImageView) findViewById(R.id.board_rev1);


        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
        params.leftMargin = (image1.getWidth())/2;
        params.topMargin = (image1.getHeight())/4;
        rl.addView(image2, params);
        setContentView(R.layout.gamerev);


}


}

But when my app goes to this activity, I get an error that says the application has stopped unexpectedly. Then I have to force close the app. Why would this happen? Is there something I am missing? or is there a better way to do this?

The error in the logcat is:

E/AndroidRuntime( 296): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc
eption

But I dont see why anything would be null because all the ids exist in my xml file:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/rlGame"
  >
    <ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
    <ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>

</RelativeLayout>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-11-12 12:43:38

您是否放置了 try catch 块并尝试调试它?似乎有什么东西返回null..

Did you put a try catch block and try to debug it ? It seems to be something returns null..

落在眉间の轻吻 2024-11-12 12:43:38

我认为问题是 imageview 的大小

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);

,也许它返回 0 所以这个问题发生了..试试吧

I think problem is size of imageview

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);

maybe it return 0 so this problem is occur..try it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文