我可以使用 Picasso 下载图像并将其放置在 FrameLayout 的背景中吗?
Can I use Picasso to download an image and place it in the background of a FrameLayout?
我正在尝试使用 Picasso 下载图像,然后将其放在我的 FrameLayout 的背景中。为什么这不起作用?
框架布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coverBackground"
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_gravity="top|center_horizontal">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/transparent_gradient"
android:minHeight="30dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp" />
</FrameLayout>
封面图片下载方法:
private void downloadCoverBackground(FrameLayout coverBackground) {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {
String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();
// Asynchronously display the cover background image downloaded from Parse
if (coverBackgroundURL != null) {
Picasso.with(getApplicationContext())
.load(coverBackgroundURL)
//.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.color.placeholderblue)
.fit()
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
}
错误:
Caused by: java.lang.IllegalStateException: Fit cannot be used with a Target.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java: 502)
at com.yitter.android.activity.UserProfileActivity.downloadCoverBackground(UserProfileActivity.java: 160)
at com.yitter.android.activity.UserProfileActivity.onCreate(UserProfileActivity.java: 90)
at android.app.Activity.performCreate(Activity.java: 6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2500)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access0(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
这里出了什么问题?
删除.fit()
删除后我附上代码
private void downloadCoverBackground(FrameLayout coverBackground) {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {
String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();
// Asynchronously display the cover background image downloaded from Parse
if (coverBackgroundURL != null) {
Picasso.with(getApplicationContext())
.load(coverBackgroundURL)
//.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.color.placeholderblue)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
}
我正在尝试使用 Picasso 下载图像,然后将其放在我的 FrameLayout 的背景中。为什么这不起作用?
框架布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coverBackground"
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_gravity="top|center_horizontal">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/transparent_gradient"
android:minHeight="30dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp" />
</FrameLayout>
封面图片下载方法:
private void downloadCoverBackground(FrameLayout coverBackground) {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {
String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();
// Asynchronously display the cover background image downloaded from Parse
if (coverBackgroundURL != null) {
Picasso.with(getApplicationContext())
.load(coverBackgroundURL)
//.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.color.placeholderblue)
.fit()
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
}
错误:
Caused by: java.lang.IllegalStateException: Fit cannot be used with a Target.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java: 502)
at com.yitter.android.activity.UserProfileActivity.downloadCoverBackground(UserProfileActivity.java: 160)
at com.yitter.android.activity.UserProfileActivity.onCreate(UserProfileActivity.java: 90)
at android.app.Activity.performCreate(Activity.java: 6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2500)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access0(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
这里出了什么问题?
删除.fit()
删除后我附上代码
private void downloadCoverBackground(FrameLayout coverBackground) {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {
String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();
// Asynchronously display the cover background image downloaded from Parse
if (coverBackgroundURL != null) {
Picasso.with(getApplicationContext())
.load(coverBackgroundURL)
//.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.color.placeholderblue)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
} else {
coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
}
}