【问题标题】:How to align image to the bottom of the screen如何将图像与屏幕底部对齐
【发布时间】:2015-09-18 19:48:11
【问题描述】:

我想将我的图像与屏幕底部对齐。作为我的应用程序条件,我必须将图像的 scaleType 设置为 matrix 。当我将 scaleType 应用于矩阵时,我真的不知道为什么我的图像在顶部。这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_capture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom">


    <FrameLayout
        android:id="@+id/fl_camera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom">

        <ImageView
            android:id="@+id/img_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="content_desc_overlay"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher"

            />

        <ImageView
            android:id="@+id/iv_overlay"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|center"
            android:adjustViewBounds="true"
            android:gravity="center_vertical"
            android:scaleType="matrix"

            android:src="@drawable/suit_1"

            />

        <RelativeLayout
            android:id="@+id/overlay_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"

            android:adjustViewBounds="true"

            ></RelativeLayout>


    </FrameLayout>

</FrameLayout>

现在我在 ImageView id=iv_overlay 中设置图像,它应该位于 xml 中每个视图的顶部并且正在工作,iv_overlay Imageview 位于顶部但设置在屏幕的左上角.

它应该在屏幕底部的位置。所以请告诉我如何通过设置 scaleType 来实现这一点,我知道如果我删除 scaleType 那么它很容易与底部对齐。但由于某些原因,我必须使用 scaletype =Matrix。

编辑: 想看图的,这里是演示图,分辨率是 479*414 ,但我使用的是下面给出的这张图片是一个演示图片,很快就会从设计师那里得到更好的透明背景图片

请帮忙。

【问题讨论】:

  • 你能附上一张图片,这样我就可以确切地知道你想要什么
  • 你想要什么?你想看看我想如何在视图中对齐图像吗?
  • 显示布局图片
  • 让我附上 ,,,,,,

标签: android imageview


【解决方案1】:

将您的图像视图放在相对布局中。并使用 alignparrentbottom=true

【讨论】:

  • 使用它 android:gravity="bottom" 而不是 android:layout_gravity="bottom"
  • 它不工作?你有没有实现过 scaleType 到矩阵?
  • 让我给你的答案打分
【解决方案2】:

你的问题是

android:layout_height="match_parent"
android:layout_width="match_parent"

将这两个都更改为“wrap_content”,您的问题就解决了

【讨论】:

  • 如果我把它变成一个包装内容,那么图像只能移动到不需要整个屏幕的布局
  • 仅在 iv_overlay 中更改此设置
  • 是的,我说的正是 ivOverlay
【解决方案3】:

对于那些在TOuchListnere 上工作并想要旋转、缩放和拖动以及使用矩阵作为缩放类型并面临类似问题的人,正如我所做的那样,这里是通过java 代码的完整解决方案

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= 16) {
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                Drawable drawable = imageView.getDrawable();
                Rect rectDrawable = drawable.getBounds();
                float leftOffset = (imageView.getMeasuredWidth() - rectDrawable.width()) / 2f;
                float topOffset = (imageView.getMeasuredHeight() - rectDrawable.height()) / 1f;

//                Matrix matrix = imageView.getImageMatrix();
                 matrix = imageView.getImageMatrix();
                matrix.postTranslate(leftOffset, topOffset);
                imageView.setImageMatrix(matrix);
                imageView.invalidate();
            }
        });

它会像魅力一样工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    相关资源
    最近更新 更多