我为我的Android应用程序做了一个带有图像的折叠工具栏.它与drawable图像完美配合.我的问题是当我从URL检索图像并将其分配给同一图像视图时.折叠工具栏无法使用.标题不见了,无法滚动,也没有图像.
这是我的截图.
图1是来自drawable的图像,图2是从URL中检索的图像
活动:
import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.design.widget.CollapsingToolbarLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.graphics.Palette; import android.support.v7.widget.Toolbar; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.URL; public class RecipeDisplay extends AppCompatActivity { CollapsingToolbarLayout collapsingToolbarLayout; ImageView image; ImageView img; Bitmap bitmap; ProgressDialog pDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recipe_display); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //default header image for toolbar image = (ImageView) findViewById(R.id.image); image.setImageResource(R.drawable.header); //Loading image using async task new LoadImage().execute("http://www.twinaccommodation.com/media/313799/pub_food_281x281.jpg"); collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); collapsingToolbarLayout.setTitle("Collapsing"); collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent)); } //async task private class LoadImage extends AsyncTask{ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(RecipeDisplay.this); pDialog.setMessage("Loading...."); pDialog.show(); } protected Bitmap doInBackground(String... args) { try { bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent()); } catch (Exception e) { e.printStackTrace(); } return bitmap; } protected void onPostExecute(Bitmap img) { if (img != null) { image.setImageBitmap(img); pDialog.dismiss(); } else { pDialog.dismiss(); Toast.makeText(RecipeDisplay.this, "Error retrieving image", Toast.LENGTH_SHORT).show(); } } } }
LOGCAT也没有错误...请帮我解决这个问题.
编辑:XML布局
使用android studio 1.5及更高版本,您可以创建滚动模板.然后将imageview添加到布局中
然后使用毕加索将其加载到图像中
public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ImageView toolbarImage = (ImageView) findViewById(R.id.image_id); String url = "" //place your url here picassoLoader(this, toolbarImage, url); } public void picassoLoader(Context context, ImageView imageView, String url){ Log.d("PICASSO", "loading image"); Picasso.with(context) .load(url) //.resize(30,30) .placeholder(R.drawable.placeholder_image) .error(R.drawable.placeholder_image) .into(imageView); } }
将picasso库添加到gradle依赖项中
compile 'com.squareup.picasso:picasso:2.5.2'