使用异步任务加载位图并设置imageview

时间:2014-01-01 20:37:42

标签: java android bitmap android-asynctask

我正在尝试遵循本教程:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html我想要了解的是如何使用此类实际加载位图。我复制并粘贴了BitmapWorkerTask类的代码,它说创建一个新任务并使用以下代码执行它:

public void loadBitmap(int resId, ImageView imageView) {
    BitmapWorkerTask task = new BitmapWorkerTask(imageView);
    task.execute(resId);
}

我想在不同的java文件中加载一个位图,然后将imageview设置为该加载的位图,但我不明白我是如何实际使用BitmapWorkerTask实际执行此操作的。

我要加载位图的活动:

public class SecondTab extends Fragment {
    ImageView p1;
    ImageView p2;
    ImageView p3;
    ImageView p4;
    ImageView p5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_secondtab, container, false);
    p1 = (ImageView) rootView.findViewById(R.id.p1);
    p2 = (ImageView) rootView.findViewById(R.id.p2);
    p3 = (ImageView) rootView.findViewById(R.id.p3);
    p4 = (ImageView) rootView.findViewById(R.id.p4);
    p5 = (ImageView) rootView.findViewById(R.id.p5);

;
        loadBitmap(R.drawable.plane1, p1);
    loadBitmap(R.drawable.plane2, p2);
    loadBitmap(R.drawable.plane3, p3);
    loadBitmap(R.drawable.plane4, p4);
    loadBitmap(R.drawable.plane5, p5);

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
}





public void loadBitmap(int resId, ImageView imageView) {
    BitmapWorkerTask task = new BitmapWorkerTask(imageView);
    task.execute(resId);
}

}

0 个答案:

没有答案