compile 'com.squareup.picasso:picasso:2.5.2'
int resourceId = R.mipmap.ic_launcher;
Picasso.with(this).load(resourceId).into(iv1);
File file = new File(path);
Picasso.with(this).load(file).into(iv2);
String uri = "http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg";
Picasso.with(this).load(uri).into(iv3);
String path = "http://img.my.csdn.net/uploads/201407/26/1406382765_7341.jpg";
Picasso.with(this)
.load(path5)
.placeholder(R.mipmap.ic_launcher) //Show pictures by default
.error(R.mipmap.ic_launcher) //Picture displayed incorrectly when loading
.into(iv5);
Picasso.with(this) //View large image to discard cache and speed up memory recycling
.load(Images.imageThumbUrls[3])
.memoryPolicy(NO_CACHE, NO_STORE)
.into(iv6);
Picasso.with(this).load(imageUrl).fit().into(imageView);
Picasso.with(this).load(imageUrl).resize(320, 640).centerCrop().into(imageView);
Picasso.with(this).load(imageUrl).resize(320, 640).centerInside().into(imageView);
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.
Picasso.with(this)
.load("http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg")
.priority(HIGH) //
priority()Set Priority for Picture Loading.into(recyclerImageView);
Picasso.with(this).load(imageUrl).noFade().into(imageView);
//Rotate 45 degrees clockwise around (0,0)
Picasso.with(this).load(imageUrl).rotate(45).into(imageView);
//Rotate 45 degrees clockwise from (64,64) Center
Picasso.with(this).load(imageUrl).rotate(45, 64, 64).into(imageView);
Picasso provides a debugging method for the cache, which can be set with the following code
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:
Picasso.with(this).load(imageUrl).tag("landptf").into(imageView);
The following methods are provided in the Picasso class to control tag s
cancelTag(Object tag)
pauseTag(Object tag)
resumeTag(Object tag)
Well understood by name, we call it when the list scrolls
Picasso.with(this).pauseTag("landptf");
Picasso.with(this).resumeTag("landptf");
As far as cancelTag is used to cancel downloads, we generally cancel outstanding requests when the Activity is destroyed.
Picasso.with(this).cancelTag("landptf");
public class ListViewPicassoActivity extends Activity{
RecyclerView recyclerView;
private List<String> mDatas;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
initData();
initView();
}
private void initView(){
recyclerView = (RecyclerView) findViewById(R.id.recycler);
LinearLayoutManager manager=new LinearLayoutManager(this); // Set LayoutManager for Layout Management
// manager.setOrientation(LinearLayoutManager.HORIZONTAL); //Set horizontal or vertical
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);// (Optional) Setting this option can improve performance if you can determine that the height of each item is fixed
- adapter=new Adapter(mDatas,this);
recyclerView.setAdapter(adapter);
// Set Adapter
//Listen while RecyclerView slides
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
- //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,
- //Memory leaks occur when the garbage collection mechanism recycles, so they need to be handled in the onDestory() method accordingly.
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
final Picasso picasso = Picasso.with(ListViewPicassoActivity.this);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
picasso.resumeTag("RecyclerView"); //Visible Markers
} else {
picasso.pauseTag("RecyclerView"); //Pause Mark Unmark cancleTag()
}
}
});
}
private void initData() {
mDatas = new ArrayList<String>();
for (int i = 0; i <= 19; i++) {
mDatas.add("item---" +i);
}
}
}
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
public List<String> datas = null;
private Context context;
public Adapter(List<String> datas, Context context) {
this.datas = datas;
this.context=context;
}
//Create a new View, called by LayoutManager
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.lv_item,viewGroup,false);
return new ViewHolder(view);;
}
//Operation to bind data to interface
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.mTextView.setText(datas.get(position));
Picasso.with(context)
.load(Images.imageThumbUrls[position])
.tag("RecyclerView") //Parameter is Object
.into(viewHolder.imageView);
}
//Number of Get Data
@Override
public int getItemCount() {
return datas.size();
}
- //Custom ViewHolder, holding all interface elements for each Item
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public ImageView imageView;
public ViewHolder(View view){
super(view);
mTextView=(TextView)view.findViewById(R.id.textview);
imageView= (ImageView) view.findViewById(R.id.imageViewitem);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="115dp"
android:background="#0099ff"
android:gravity="center_vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="35dp"
android:gravity="center"
android:textSize="19sp"
android:textColor="#000000"
android:text="I am an entry"
android:layout_marginTop="5dp"/>
<ImageView
android:id="@+id/imageViewitem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"/>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#FF0000"
/>
</LinearLayout>