适合ImageView的大小以及对其执行旋转

时间:2017-11-27 16:31:18

标签: android imageview image-rotation

我的活动

            BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap cameraBitmapNull = BitmapFactory.decodeByteArray(data, 0, data.length, options);

            int wid = options.outWidth;
            int hgt = options.outHeight;
            Matrix nm = new Matrix();

            Camera.Size cameraSize = camera.getParameters().getPictureSize();
            float ratio = relativeLayout.getHeight() * 1f / cameraSize.height;
            if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                nm.postRotate(90);
                nm.postTranslate(hgt, 0);
                wid = options.outHeight;
                hgt = options.outWidth;
                ratio = relativeLayout.getWidth() * 1f / cameraSize.height;

            } else {
                wid = options.outWidth;
                hgt = options.outHeight;
                ratio = relativeLayout.getHeight() * 1f / cameraSize.height;
            }
            float ratios = relativeLayout.getHeight() * 1f / cameraSize.height;
            float[] f = new float[9];
            matrix.getValues(f);

            f[0] = f[0] / ratios;
            f[4] = f[4] / ratios;
            f[5] = f[5] / ratios;
            f[2] = f[2] / ratios;
            matrix.setValues(f);

            Bitmap newBitmap = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(newBitmap);

            Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
            imgfool.setImageBitmap(cameraBitmap);
            canvas.drawBitmap(cameraBitmap, nm, null);
        imgfool.setVisibility(View.VISIBLE);
        //imgfool.setScaleType(ImageView.ScaleType.MATRIX);   //required
       // nm.postRotate((float)0);
       // imgfool.setRotation((float) 90);
       // Bitmap rotated = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), nm, true);
        //imgfool.setRotation(90);
        imgfool.setImageBitmap(cameraBitmap);
        cameraSurfaceView.setVisibility(View.GONE);
        takeScreenShot(oopss);

我的XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="55dp"
    android:id="@+id/frame"
  >

    <RelativeLayout
        android:id="@+id/containerImg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/oopss">
        <ImageView
            android:id="@+id/imgfool"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:rotation="90"
            android:scaleType="fitXY"
            />

        <ImageView
            android:id="@+id/logoImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/app_name"
            android:scaleType="matrix"
           />
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/redirect"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:background="#000"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:gravity="bottom"
            android:orientation="horizontal"
            android:visibility="gone">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="10dp"
                android:layout_weight=".33"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/label"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:src="@drawable/glow"
                    android:text="Back"
                    android:textColor="#fff"
                    android:textSize="20sp" />
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

这里我有一个imageview旋转到90度,但在我的xml文件中使用match_parentfitXY时会在上下留下一些空间。我在此搜索了很多但没有找到任何合适的解决方案。所以请帮我解决这个问题。实际上imageview设置了从surfaceview获取的位图图像,但是当我在imageview上设置它时,它会自动旋转。所以我想旋转ImageView以获得要在其上设置的正确图像。

0 个答案:

没有答案