简体   繁体   English

屏幕上显示的图像-Android Studio

[英]Image displaying off the screen - Android studio

For some reason when I set the x coordinate to screen_width - image_width, the image is displayed off the screen. 由于某些原因,当我将x坐标设置为screen_width-image_width时,图像显示在屏幕之外。 The same scenario occurs with the y coordinate. y坐标也会发生相同的情况。 Here is my code. 这是我的代码。

public class MainActivity extends AppCompatActivity {

    ImageView image;
    float height;
    float width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        height = displayMetrics.heightPixels;
        width = displayMetrics.widthPixels;

        image.setImageResource(R.drawable.pigeon);
        image.setX(width - image.getMeasuredWidth());

In this case the image, which is a pigeon, is not displayed on the screen. 在这种情况下,作为鸽子的图像不会显示在屏幕上。 And I expect it to be displayed so that the right border of the pigeon touches the right border of the screen. 而且我希望它能显示出来,以便鸽子的右边界接触到屏幕的右边界。

Edit: It's not that I just want to position the image and be done with it. 编辑:不是我只想定位图像并完成它。 I want to be able to move the image to precise coordinates while the app is running, such as when a mouse click occurs. 我希望能够在应用运行时将图像移动到精确坐标,例如单击鼠标时。

One way to align the image right is to use the XML layout. 正确对齐图像的一种方法是使用XML布局。 This is the easiest method. 这是最简单的方法。

Assuming you have an ImageView inside a RelativeLayout you can use the `layout_alignParentRight' attribute as follows: 假设您在RelativeLayout有一个ImageView ,则可以如下使用“ layout_alignParentRight”属性:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_name"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
</RelativeLayout>

The layout_alignParentEnd attribute is an alternative for layout_alignParentRight for right to left language script. layout_alignParentEnd属性是从右到左语言脚本的layout_alignParentRight的替代方法。 For a LinearLayout you can use layout_gravity attribute. 对于LinearLayout ,可以使用layout_gravity属性。 Also, ensure that your ImageView is wrap_content and not match_parent in which case the image will expand to fill the entire ImageView . 另外,请确保您的ImageViewwrap_content而不是match_parent在这种情况下,图像将展开以填充整个ImageView

If you want to do it programmatically, you can do it in the following manner: 如果要以编程方式执行此操作,则可以通过以下方式进行:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        imageView.setImageResource(R.drawable.pigeon);
        RelativeLayout.LayoutParams params = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imageView.setLayoutParams(params);
    }

Here we are aligning the child view of RelativeLayout right by adding the align right rule and then setting that layout parameter for the Image View . 在这里,我们通过添加align right规则,然后为Image View设置该layout参数,来对齐RelativeLayout right的子视图。 This can also be done for LinearLayout using layout_gravity feature. 也可以使用layout_gravity功能对LinearLayout进行layout_gravity

I have tested this code for a sample image. 我已经对该代码进行了示例图像测试。 Hope this solves your problem. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM