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

1.6k views Asked by At

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

3

There are 3 answers

5
AskNilesh On BEST ANSWER

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

public void requestLayout ()

  • Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.

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);
    }


}
0
Kingsley Mitchell On

You need to use constraints, put it in a ConstraintLayout and then set the edges to 0, or set the Constraint to how much you want it to not shift.

https://medium.com/@eugenebrusov/using-of-constraintlayout-to-set-your-imageview-in-16-9-ratio-9979d53ba976

1
Chetan Joshi On

You can use Parent Layout (LinearLayout or FramLayout)to apply your padding on and put your ImageView in it so its work according your requirements.

OR

You can use ScaleType property so it can be manage in a smooth way.