【问题标题】:how to fill image fullscreen in android如何在android中全屏填充图像
【发布时间】:2015-03-07 07:22:27
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageview"
        android:scaleType="centerCrop"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imagecancel"
        android:layout_marginBottom="10dp"
        android:background="#80000000">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imagecaption"
        android:hint="Enter a description"
        android:textColorHint="#80ffffff"
        android:textColor="#ffffff"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"/>
    </RelativeLayout>
    <ImageButton
        android:contentDescription="@string/imagecancel"
        android:id="@+id/imagecancel"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:scaleType="fitStart"
        android:background="@android:color/transparent"
        android:src="@drawable/cancel"/>
    <ImageButton
        android:contentDescription="@string/imagesave"
        android:id="@+id/imagesave"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:scaleType="fitEnd"
        android:background="@android:color/transparent"
        android:src="@drawable/ok"/>

</RelativeLayout>

Java

private void previewCapturedImage() {



        try {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            ExifInterface exif = null;
            int orientation = 0;//Since API Level 5
            try {
                exif = new ExifInterface(fileUri.getPath());
                orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

            Log.i("file path",exifOrientation);

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());

            previewimage.setImageBitmap(bitmap);
            switch(orientation) {

                case ExifInterface.ORIENTATION_ROTATE_270:
                    Log.i("RotateBitmap","270");
                    RotateBitmap(bitmap, 270);
                    previewimage.setRotation(270);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    Log.i("RotateBitmap","90");
                    RotateBitmap(bitmap, 90);
                    previewimage.setRotation(90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    Log.i("RotateBitmap","180");
                    RotateBitmap(bitmap, 180);
                    previewimage.setRotation(180);
                    break;
            }







        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    public static Bitmap RotateBitmap(Bitmap source, float angle)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        previewimage.setImageBitmap(source);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

这里的图像填充在小屏幕手机中正常。但在大屏幕手机图像显示不正确。它显示距离顶部 2 厘米和底部 2 厘米的间隙。图片来自手机摄像头拍照。我想全屏填充图像。如何解决这个问题。

【问题讨论】:

  • 在你的图像视图中使用 android:scaleType="fitXY"

标签: android android-layout android-camera


【解决方案1】:

试试这个

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageview"
        android:scaleType="fitXY"/>

【讨论】:

    【解决方案2】:

    问题的根源在于当前预览区域的边比与所获得图片的边比之间的差异。您的预览区域在不同设备上会有所不同(即使在不同方向的同一设备上 - 由“软按钮”位置引起),并且您从相机获得的图像很少与当前预览具有相同的边比。

    无需任何代码即可回答您的问题。只需在一张纸上画一个矩形(非正方形)和一个正方形,然后尝试将一个放入另一个。

    矩形(宽或高)代表您的手机,正方形代表您从相机拍摄的照片(它大多不是正方形,但我在这里使用正方形以使其更清晰演示)。

    如果您采用这 2 种形状并尝试将其中一种“适合”到另一种中,您将得到 3 种不同的场景:

    1. 您可以通过拉伸/挤压将正方形调整为矩形。这绝对不是拍的结果(第一,它不再是方形了,第二,我们奶奶的脸会太胖或太瘦:-)
    2. 您将整个正方形放入矩形中,您会看到 2 个未填充区域(如果您将其居中) - 这就是您所抱怨的情况。也称为“适合”或“信箱”;
    3. 您用正方形的一部分填充整个矩形,正方形的两侧将溢出矩形。如果您不介意失去一些形象,这个案例看起来您已经实现了您的目标。这种情况有时被称为“平移和扫描”。

    那么,这个咆哮对你有什么帮助?如果您坚持填满全屏(并丢失溢出的图像部分),则在获取图像后调整 SurfaceView 区域,使用它的宽度/高度来计算比率。

    如果您真的坚持要查看一些代码,it's done here(请参阅 setLayoutParams),但请注意,我所指的示例更复杂,并且涉及自定义相机处理。但总体思路是一样的。

    祝你好运

    【讨论】:

      【解决方案3】:

      如果需要,可以在xml中设置为RelativeLayout背景。

      android:background="@drawable/_image"
      

      或设置dynamically

      【讨论】:

        【解决方案4】:

        更改刻度类型

           <ImageView
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:id="@+id/imageview"
         android:scaleType="fitXY"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-25
          • 1970-01-01
          • 2014-03-03
          • 1970-01-01
          相关资源
          最近更新 更多