0
votes

I am developing an android app after image view is rotated image view not fitting the entire screen how can I fit the entire screen so that ImageView covers the entire screen imageafterrotated `

below my MainActivity.java where I have implemented image rotation logic in my java class below my MainActivity.java where I have implemented image rotation logic in my java class

public class MainActivity extends AppCompatActivity {
    float scaleDiff;
    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;
    private float oldDist = 1f;
    private float d = 0f;
    private float newRot = 0f;

    @BindView(R.id.show_image)
    ImageView mImageView;
    private boolean mMoved = false;
    private PhotoViewAttacher mViewAttacher;

    private float mScaleX = 0, mScaleY = 0;

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

        ButterKnife.bind(this);

        mScaleX = mImageView.getScaleX();
        mScaleY = mImageView.getScaleY();

        final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(getScreenWidth(), getScreenHeight());

        mImageView.setLayoutParams(layoutParams);
        mImageView.setOnTouchListener(new View.OnTouchListener() {

            RelativeLayout.LayoutParams params;
            int startWidth;
            int startHeight;
            float dx = 0, dy = 0, x = 0, y = 0;
            float angle = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final ImageView view = (ImageView) v;

                ((BitmapDrawable) view.getDrawable()).setAntiAlias(true);

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        params = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        startWidth = params.width;
                        startHeight = params.height;
                        dx = event.getRawX() - params.leftMargin;
                        dy = event.getRawY() - params.topMargin;
                        mode = DRAG;
                        break;

                    case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist = spacing(event);
                        if (oldDist > 10f) {
                            mode = ZOOM;
                        }
                        d = rotation(event);
                        break;

                    case MotionEvent.ACTION_UP:
                        if (view.getScaleX() <= mScaleX || view.getScaleY() <= mScaleY) {
                            view.setScaleX(mScaleX);
                            view.setScaleY(mScaleY);
                            params.leftMargin = 0;
                            params.topMargin = 0;
                            params.rightMargin = 0;
                            params.bottomMargin = 0;
                        }

                        if (mMoved || view.getWidth() != getScreenWidth() || view.getHeight() != getScreenHeight()) {
                            params.leftMargin = 0;
                            params.topMargin = 0;
                            params.rightMargin = 0;
                            params.bottomMargin = 0;
                            mMoved = false;
                        }

                        view.setScaleType(ImageView.ScaleType.CENTER_CROP);

                        view.setLayoutParams(params);
                        break;

                    case MotionEvent.ACTION_POINTER_UP:
                        mode = NONE;
                        break;

                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {

                            mMoved = true;

                            x = event.getRawX();
                            y = event.getRawY();

                            params.leftMargin = (int) (x - dx);
                            params.topMargin = (int) (y - dy);

                            params.rightMargin = 0;
                            params.bottomMargin = 0;
                            params.rightMargin = params.leftMargin + (5 * params.width);
                            params.bottomMargin = params.topMargin + (10 * params.height);

                            view.setLayoutParams(params);

                        }

                        else if (mode == ZOOM) {
                            if (event.getPointerCount() == 2) {

                                newRot = rotation(event);
                                float r = newRot - d;
                                angle = r;

                                x = event.getRawX();
                                y = event.getRawY();

                                float newDist = spacing(event);
                                if (newDist > 10f) {
                                    float scale = newDist / oldDist * view.getScaleX();
                                    if (scale > 0.6) {
                                        scaleDiff = scale;
                                        view.setScaleX(scale);
                                        view.setScaleY(scale);
                                    }
                                }

                                view.animate().rotationBy(angle).setDuration(0).setInterpolator(new LinearInterpolator()).start();

                                x = event.getRawX();
                                y = event.getRawY();

                                params.leftMargin = (int) ((x - dx) + scaleDiff);
                                params.topMargin = (int) ((y - dy) + scaleDiff);

                                params.rightMargin = 0;
                                params.bottomMargin = 0;
                                params.rightMargin = params.leftMargin + (5 * params.width);
                                params.bottomMargin = params.topMargin + (10 * params.height);

                                view.setLayoutParams(params);
                            }
                        }

                        break;
                }
                return true;
            }
        });
    }

    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private float rotation(MotionEvent event) {
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }

    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }

}

below activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/show_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/abstract_image"
        android:transitionName="image_transition" />
</RelativeLayout>
1

1 Answers

0
votes

Use this in imageView

<ImageView
    android:id="@+id/show_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:src="@drawable/abstract_image"
    android:transitionName="image_transition" />

java file

imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bitmap myImg = BitmapFactory.decodeResource(getResources(), 
 R.drawable.abstract_image);
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, 
 myImg.getWidth(), myImg.getHeight(),
                    matrix, true);
            imageView.setImageBitmap(rotated);
        }
    });

using this code image is rotate properly now you can adjust in your code according to your need