gpt4 book ai didi

android - 使用 Picasso 在 Activity 之间共享过渡

转载 作者:行者123 更新时间:2023-11-30 05:05:05 31 4
gpt4 key购买 nike

我在使用 Picasso 异步加载图像时遇到共享过渡问题。如果我使用带有可绘制对象的简单 ImageView,它会工作得很好。

但是,当我使用 Picasso 时,返回的动画无法正确缩放图像。

您可以更清楚地看到我在我的 github 存储库中描述的问题: https://github.com/brunoperezm/SharedTransitionsAsync

调用和被调用 Activity 的代码,也可以在我的 repo 上看到,如下所示:

MainActivity.java

package com.example.sharedtransitionsasync;

import ...

public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// getWindow().setSharedElementEnterTransition(new DetailsTransition());
getWindow().setSharedElementReturnTransition(new DetailsTransition());


imageView = findViewById(R.id.my_image_view1);
Picasso.get()
.load("https://i.redd.it/jb1okqafolg21.jpg")
.fit()
.into(imageView, new Callback() {
@Override
public void onSuccess () {
startPostponedEnterTransition();
}

@Override
public void onError (Exception e) {
startPostponedEnterTransition();
}
});

}

public void gotoActivity2 (View view) {
Intent intent = new Intent(this, Main2Activity.class);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageView, "profile");
startActivity(intent, optionsCompat.toBundle());
}

public class DetailsTransition extends TransitionSet {
public DetailsTransition() {
setOrdering(ORDERING_TOGETHER);
addTransition(new ChangeBounds()).
addTransition(new ChangeTransform()).
addTransition(new ChangeImageTransform());
}
}

}

Main2Activity.java

package com.example.sharedtransitionsasync;

import ...

public class Main2Activity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);


getWindow().setSharedElementReturnTransition(new DetailsTransition());


postponeEnterTransition();

imageView = findViewById(R.id.my_image_view2);
Picasso.get()
.load("https://i.redd.it/jb1okqafolg21.jpg")
.fit()
.centerCrop()
.into(imageView, new Callback() {
@Override
public void onSuccess () {
scheduleStartPostponedTransition(imageView);
}

@Override
public void onError (Exception e) {

}
});
}

private void scheduleStartPostponedTransition(final View sharedElement) {
sharedElement.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw () {
sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
});
}

@Override
public void onBackPressed () {
super.onBackPressed();
supportFinishAfterTransition();
}
public class DetailsTransition extends TransitionSet {
public DetailsTransition() {
setOrdering(ORDERING_TOGETHER);
addTransition(new ChangeBounds()).
addTransition(new ChangeTransform()).
addTransition(new ChangeImageTransform()).
setDuration(5000);
}
}

}

最佳答案

在你的两个 xml 中,在 ImageView 上使用这个 scaleType

android:scaleType="centerCrop"

然后在 picasso 调用这两个 Activity 时,删除 .fit() 和 .centerCrop()

现在在你的 git 仓库上工作得很好

关于android - 使用 Picasso 在 Activity 之间共享过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715056/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com