如何使用处理程序和执行程序将位图从工作线程传递到主线程?

时间:2018-08-07 13:18:36

标签: android imageview handle looper

这是我的工作线程的run()方法,该方法使用执行程序执行:

@Override
public void run() {
    Looper.prepare();
    Log.i(TAG, "Starting Thread" );
    final Bitmap bitmap=getBitmap(imageUrl);
    handler=new Handler();
    Message message;
    message=Message.obtain();
    message.obj=bitmap;
    handler.sendMessage(message);
    Log.i(TAG, "Thread Completed ");
}

这是我希望收到消息的适配器类的代码:

@Override
public void onBindViewHolder(@NonNull final OwnerViewHolder ownerViewHolder, int i) {
    Owner item=list.get(ownerViewHolder.getAdapterPosition());
    ownerViewHolder.image.setImageResource(0);
    download(item.getProfileImage());

    ownerViewHolder.textView.setText(item.getDisplayName());
    ownerViewHolder.textView1.setText(item.getUserType());

    new Handler(Looper.getMainLooper()){
        public void handleMessage(Message message){
            ownerViewHolder.image.setImageBitmap((Bitmap)message.obj);
            super.handleMessage(message);
        }
    };
}

这是我的ThreadPoolExecutor的代码:

private void download(String imageUrl){
    int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();

    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            NUMBER_OF_CORES * 2,
            NUMBER_OF_CORES * 2,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>()
    );
    longThread=new LongThread(imageUrl);
    executor.execute(longThread);
}

我不知道如何使用处理程序和执行程序将位图从工作线程传递到主线程。我该怎么办?

0 个答案:

没有答案