您需要requestLayout()
在设置填充后使用mPhotoView
公共无效请求布局()
- 当某些事情发生变化而导致该视图的布局无效时调用它。这将安排视图树的布局传递。当视图层次结构当前处于布局阶段(isInLayout() 时,不应调用此方法。如果正在发生布局,则请求可能会在当前布局阶段结束时(然后布局将再次运行)或在当前框架被绘制并发生下一个布局。
示例代码
<?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>
活动代码
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();
}
}
请检查上述代码的输出https://www.youtube.com/watch?v=828XI6ydNdY
更新
根据您的以下评论
我的带有 FItXY scaletype 的 ImageView。当我应用您的代码时,无法正常工作。同样的结果。它不能像左填充一样自动移动。
实际上,在设置填充后,您的右侧和底部填充正在工作,您需要滚动您的mPhotoView
就像下面的代码
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);
}
}