androidandroid-layoutandroid-imageviewandroid-photoview

How to move Image to position x, y coordinate with current scale and padding


I am using Photoview Library in my project. I need to setPadding in Imageview with Zoom.

When I use with left and top padding, It working Perfectly. It also move image from left and top as I wish.

mPhotoView.setPadding(150, 150, 0, 0);

image with left padding

But, When I apply right padding and bottom padding , it apply padding but not automatically move from current place.

mPhotoView.setPadding(0, 0, 150, 150);     //Issue here.

Image with right padding


Solution

  • You need to use requestLayout() after setting padding in your mPhotoView

    public void requestLayout ()

    SAMPLE CODE

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:onClick="ClickMe3"
            android:text="Remove Padding " />
    
        <com.github.chrisbanes.photoview.PhotoView
            android:id="@+id/photo_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:onClick="ClickMe"
            android:text="left and top " />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:onClick="ClickMe2"
            android:text="right and bottom " />
    
    
    </RelativeLayout>
    

    Activity Code

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    
    import com.github.chrisbanes.photoview.PhotoView;
    
    public class MainActivity extends AppCompatActivity {
    
        PhotoView mPhotoView;
        boolean flag = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mPhotoView = (PhotoView) findViewById(R.id.photo_view);
            mPhotoView.setImageResource(R.drawable.dishu);
    
        }
    
        public void ClickMe(View view) {
    
            mPhotoView.setPadding(150, 150, 0, 0);
            mPhotoView.requestLayout();
    
        }
    
        public void ClickMe2(View view) {
    
            mPhotoView.setPadding(0, 0, 150, 150);
            mPhotoView.requestLayout();
        }
    
        public void ClickMe3(View view) {
    
            mPhotoView.setPadding(0, 0, 0, 0);
            mPhotoView.requestLayout();
    
        }
    
    
    }
    

    Please check the output of the above code https://www.youtube.com/watch?v=828XI6ydNdY

    UPDATE

    as per your below comment

    My ImageView with FItXY scaletype. When I apply your code , Not working. same result. It can't move automatic as left padding.

    Actually Your padding Right and Bottom is working after setting padding you need to scroll your mPhotoView

    Like below code

    public class MainActivity extends AppCompatActivity {
    
        PhotoView mPhotoView;
        RelativeLayout rootView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            rootView =  findViewById(R.id.rootView);
            mPhotoView = (PhotoView) findViewById(R.id.photo_view);
    
            mPhotoView.setImageResource(R.drawable.dishu);
        }
    
        public void Left_Top(View view) {
            mPhotoView.setPadding(150, 150, 0, 0);
            mPhotoView.requestLayout();
        }
    
        public void Right_Bottom(View view) {
            mPhotoView.setPadding(0, 0, 150, 150);
            mPhotoView.requestLayout();
            // after setting padding scroll your mPhotoView to bottom
            mPhotoView.scrollTo(150,150);
        }
    
        public void ClickMe3(View view) {
            mPhotoView.setPadding(0, 0, 0, 0);
            mPhotoView.requestLayout();
            mPhotoView.scrollTo(0,0);
        }
    
    
    }