在AsyncTask中实现Picasso的位置

时间:2016-01-04 15:36:13

标签: java android android-asynctask picasso

我正在使用AsyncTask从数据库中获取数据并加载到ListView中。我想在加载到ListView之前使用Picasso调整图像大小。唯一的问题是我不知道在哪里实施它。

public void getListFromDb(){
Cursor res = myDb.ViewAll();
startManagingCursor(res);


//Map cursor from db to viewFields
String[] fromFieldNames = new String[]{DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4, DatabaseHelper.COL_5, DatabaseHelper.COL_6};
int[] toViewIDS = new int[]{R.id.viewName, R.id.viewAddress, R.id.viewPostcode, R.id.viewType, R.id.imageView};

//Create SimpleCursorAdaptor with null cursor
SimpleCursorAdapter myCursorAdaptor = new SimpleCursorAdapter(this, R.layout.item_layout, null, fromFieldNames, toViewIDS, 0);
// Set adaptor for listView
myList.setAdapter(myCursorAdaptor);

new AsyncTask<SimpleCursorAdapter, Void, Cursor>() {
    private SimpleCursorAdapter mSimpleCursorAdapter;
    @Override
    protected Cursor doInBackground(SimpleCursorAdapter... params) {
        // Save cursorAdapter to use in postExecute
        this.mSimpleCursorAdapter = params[0];
        // Load cursor on background thread with search function
           return myDb.ViewAll();
        }
    }

在将图像显示在ListView中之前,我可以在哪里实现Picasso来调整图像大小。

由于

2 个答案:

答案 0 :(得分:0)

Picasso已经实施了resize方法并且已经在backbround中运行,因此您不需要为它实现异步任务。

Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.resize(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);

请参阅docs

答案 1 :(得分:0)

Picasso库将使用asyncTask在后台进行操作。 根据{{​​3}}。您只需在this answerListView

中加载图片即可
     Picasso.with(context) 
 .load(url)
 .placeholder(R.drawable.ic_launcher)
 .error(R.drawable.ic_error) 
 .resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size).centerInside()
// Also .resize(Width , Height)

.into(imgView);