Basic use of the Picasso framework

Keywords: Android network xml encoding

Picasso Framework Download Address: https://github.com/square/picasso 
    
First import dependencies in Android studio
  1. compile 'com.squareup.picasso:picasso:2.5.2'

1. Picture loading
1.1 Load local pictures
  1. int resourceId = R.mipmap.ic_launcher;
  2. Picasso.with(this).load(resourceId).into(iv1);
1.2 Load sd card pictures
  1. File file = new File(path);
  2. Picasso.with(this).load(file).into(iv2);
1.3 Load Network Pictures
  1. String uri = "http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg";
  2. Picasso.with(this).load(uri).into(iv3);
1.4 Load network pictures (initial and error pictures)
  1. String path = "http://img.my.csdn.net/uploads/201407/26/1406382765_7341.jpg";
  2. Picasso.with(this)
  3. .load(path5)
  4. .placeholder(R.mipmap.ic_launcher) //Show pictures by default
  5. .error(R.mipmap.ic_launcher) //Picture displayed incorrectly when loading
  6. .into(iv5);

2. Load pictures without caching
  1. Picasso.with(this) //View large image to discard cache and speed up memory recycling
  2. .load(Images.imageThumbUrls[3])
  3. .memoryPolicy(NO_CACHE, NO_STORE)
  4. .into(iv6);

3. Picture Filling Method
3.1 This property fills the entire View according to the size of the Image View, regardless of scale, which may stretch or shrink the picture
Picasso measures the size of the picture and the image view, calculates the best size and quality for picture display, reduces memory, and has no effect on the view.
  1. Picasso.with(this).load(imageUrl).fit().into(imageView);
3.2 Scaled downscaling of a picture to center it and fill it with View will result in incomplete picture display and must be used in conjunction with the resize method
  1. Picasso.with(this).load(imageUrl).resize(320, 640).centerCrop().into(imageView);
3.3 Proportionally cropped pictures can be fully displayed, but if they are smaller than View, they cannot fill the entire View and must be used in conjunction with the resize method
  1. Picasso.with(this).load(imageUrl).resize(320, 640).centerInside().into(imageView);
3.4 The size of the test picture used here is 1240*1563. If the resize width is larger than the original width and height of the picture, resize will not work and will be displayed in the original width and height of the picture.
  1. Picasso.with(this).load(imageUrl).resize(1240, 1563).onlyScaleDown().into(imageView);


4. Rewrite the onDetachedFromWindow method of ImageView, callback when it disappears from the screen, remove drawable references, and speed up memory recycling.
If the top picture on a screen is larger and the bottom picture is smaller, because Picasso is loaded asynchronously, the small picture will be loaded first, but the user would prefer to see the above picture loaded first.

At the bottom of the picture after loading, Picasso supports setting priorities, which are HIGH, MEDIUM, and LOW. All loading defaults are MEDIUM.

  1. Picasso.with(this)
  2. .load("http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg")
  3. .priority(HIGH) //priority()Set Priority for Picture Loading
  4. .into(recyclerImageView);

5. Cancel the transition effect of picture noFade()
  1. Picasso.with(this).load(imageUrl).noFade().into(imageView);
By default, pictures display with a transitional effect that can be cancelled by adding the.noFade method, which is rarely used

6. Picture rotation rotate()
  1. //Rotate 45 degrees clockwise around (0,0)
  2. Picasso.with(this).load(imageUrl).rotate(45).into(imageView);
  3. //Rotate 45 degrees clockwise from (64,64) Center
  4. Picasso.with(this).load(imageUrl).rotate(45, 64, 64).into(imageView);

7. Cache Policy

Picasso provides a debugging method for the cache, which can be set with the following code

  1. Picasso.with(this).setIndicatorsEnabled(true);

8. tag label management

Children's shoes who have used list-loaded pictures know how to stop loading pictures during the list scrolling process and restore them when they stop scrolling, so how does this work in Picasso?(
This uses the tag tag function.
Set the tag with the following code:

  1. Picasso.with(this).load(imageUrl).tag("landptf").into(imageView);

The following methods are provided in the Picasso class to control tag s

  1. cancelTag(Object tag)
  2. pauseTag(Object tag)
  3. resumeTag(Object tag)

Well understood by name, we call it when the list scrolls

  1. Picasso.with(this).pauseTag("landptf");
Called when scrolling stops
  1. Picasso.with(this).resumeTag("landptf");

As far as cancelTag is used to cancel downloads, we generally cancel outstanding requests when the Activity is destroyed.

  1. Picasso.with(this).cancelTag("landptf");

9. List Load Display
  1. public class ListViewPicassoActivity extends Activity{
  2. RecyclerView recyclerView;
  3. private List<String> mDatas;
  4. private Adapter adapter;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.listview);
  9. initData();
  10. initView();
  11. }
  12. private void initView(){
  13. recyclerView = (RecyclerView) findViewById(R.id.recycler);
  14. LinearLayoutManager manager=new LinearLayoutManager(this); // Set LayoutManager for Layout Management
  15. // manager.setOrientation(LinearLayoutManager.HORIZONTAL); //Set horizontal or vertical
  16. recyclerView.setLayoutManager(manager);
  17. recyclerView.setHasFixedSize(true);// (Optional) Setting this option can improve performance if you can determine that the height of each item is fixed
  18.  
  19. adapter=new Adapter(mDatas,this);
  20. recyclerView.setAdapter(adapter); // Set Adapter
  21. //Listen while RecyclerView slides
  22. recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
  23. //If the tag status is pause or resume, Picasso will hold a reference to the tag, and if the user exits the current Activity at that time,
  24. //Memory leaks occur when the garbage collection mechanism recycles, so they need to be handled in the onDestory() method accordingly.
  25. @Override
  26. public void onScrollStateChanged(RecyclerView recyclerView, int newState)
  27. {
  28. final Picasso picasso = Picasso.with(ListViewPicassoActivity.this);
  29. if (newState == RecyclerView.SCROLL_STATE_IDLE) {
  30. picasso.resumeTag("RecyclerView"); //Visible Markers
  31. } else {
  32. picasso.pauseTag("RecyclerView"); //Pause Mark Unmark cancleTag()
  33. }
  34. }
  35. });
  36. }
  37. private void initData() {
  38. mDatas = new ArrayList<String>();
  39. for (int i = 0; i <= 19; i++) {
  40. mDatas.add("item---" +i);
  41. }
  42. }
  43. }
  1. public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
  2. public List<String> datas = null;
  3. private Context context;
  4. public Adapter(List<String> datas, Context context) {
  5. this.datas = datas;
  6. this.context=context;
  7. }
  8. //Create a new View, called by LayoutManager
  9. @Override
  10. public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  11. View view = LayoutInflater.from(context).inflate(R.layout.lv_item,viewGroup,false);
  12. return new ViewHolder(view);;
  13. }
  14. //Operation to bind data to interface
  15. @Override
  16. public void onBindViewHolder(ViewHolder viewHolder, int position) {
  17. viewHolder.mTextView.setText(datas.get(position));
  18. Picasso.with(context)
  19. .load(Images.imageThumbUrls[position])
  20. .tag("RecyclerView") //Parameter is Object
  21. .into(viewHolder.imageView);
  22. }
  23. //Number of Get Data
  24. @Override
  25. public int getItemCount() {
  26. return datas.size();
  27. }
  28. //Custom ViewHolder, holding all interface elements for each Item
  29. public class ViewHolder extends RecyclerView.ViewHolder{
  30. public TextView mTextView;
  31. public ImageView imageView;
  32. public ViewHolder(View view){
  33. super(view);
  34. mTextView=(TextView)view.findViewById(R.id.textview);
  35. imageView= (ImageView) view.findViewById(R.id.imageViewitem);
  36. }
  37. }
  38. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <android.support.v7.widget.RecyclerView
  6. android:id="@+id/recycler"
  7. android:scrollbars="none"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:fadingEdge="none"
  11. />
  12. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="115dp"
  6. android:background="#0099ff"
  7. android:gravity="center_vertical"
  8. >
  9. <TextView
  10. android:id="@+id/textview"
  11. android:layout_width="match_parent"
  12. android:layout_height="35dp"
  13. android:gravity="center"
  14. android:textSize="19sp"
  15. android:textColor="#000000"
  16. android:text="I am an entry"
  17. android:layout_marginTop="5dp"/>
  18. <ImageView
  19. android:id="@+id/imageViewitem"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:layout_marginBottom="5dp"/>
  23. <View
  24. android:layout_width="match_parent"
  25. android:layout_height="3dp"
  26. android:background="#FF0000"
  27. />
  28. </LinearLayout>


This is the Picasso framework, and how to use it is described in the following materials:
Brief description of picasso use  :  http://blog.csdn.net/chenguang79/article/details/50621748  
    

Posted by devinemke on Thu, 13 Jun 2019 09:53:35 -0700