UI的Android后台任务

时间:2015-07-15 09:37:15

标签: android multithreading bitmap executorservice android-gpuimageview

我正在Android下开发一个应用程序来处理位图并在位图上应用GPU过滤器。由于速度很慢,我决定设置一个“默认图像”,就像一张假图片一样,直到我想要过滤的图片完成后才显示。

在此之前,我希望在显示之前修改位图,但它不能提供良好的用户体验。

问题是我在系统级别发出警告说:

System.err﹕ android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我看到一些线程表明我必须使用runOnUiThread,但我不知道如何使其工作..这是我的应用程序的代码

活动FilterActivity接收包含文件路径的URI。 FilterPreview方法正在处理将设置为我想要执行的操作排队的类。

public class FiltersActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filters);

        mFiltersView = (ListView) findViewById(R.id.filters_place_holder);
        ViewGroup.LayoutParams params = mFiltersView.getLayoutParams();

        Intent intent = getIntent();
        mSnapShotUri = intent.getData();
        getBitmapFromURI(mSnapShotUri);
        ....

    private void getBitmapFromURI(Uri uri) {
        try {
            mSnapShot = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "File not found");
        }
        finally {
            mPreviewBitmap = Bitmap.createScaledBitmap(mBitmap, PreviewSizeWidth, PreviewSizeHeight, false);
            FiltersPreview();
        }
    }

    private void FiltersPreview() {

        boolean mPreview = true;
        mFiltersView.setAdapter(new CameraImageFiltersAdapter(this, mSnapShot, mPreviewBitmap, mPreview));
        mFiltersView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                CameraFiltersFactory effect = (CameraFiltersFactory) v.findViewById(R.id.filteredImage).getTag();
                effect.save(new GPUImage.OnPictureSavedListener() {

                   @Override public void onPictureSaved(Uri uri) {

                        Intent previewIntent = new Intent(FiltersActivity.this, PreviewActivity.class);
                        previewIntent.setData(uri);
                        startActivityForResult(previewIntent, 0);

                    }
                });
            }
        });
    }

在我的应用程序中,下面的部分正在减慢UI,因为我没有使用后台任务/线程来更新UI。

        mFiltersView.setAdapter(new CameraImageFiltersAdapter(this, mSnapShot, mPreviewBitmap, mPreview));

CameraImageFiltersAdapter类(stub_id是在应用过滤器过程中显示的假图像)

public CameraImageFiltersAdapter(Context c, Bitmap original, Bitmap current, boolean isPreview) {
        ....
        mExecutorService = Executors.newFixedThreadPool(5);

    }

    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView filteredImage;

        if (convertView == null) {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_filter, null);
            filteredImage = (ImageView) convertView.findViewById(R.id.filteredImage);
            filteredImage.setImageResource(stub_id);
            queueFiltered(filteredImage, position);
        }

        return convertView;
    }

    private void queueFiltered(ImageView imageView, int position) {
        CameraFiltersFactory holder = new CameraFiltersFactory(mContext, imageView);
        mExecutorService.submit(new FilterLoader(holder, position));

    }

    class FilterLoader implements Runnable {
        CameraFiltersFactory FilterToLoad;
        int FilterPosition;

        FilterLoader(CameraFiltersFactory FilterToLoad, int pos) {
            this.FilterToLoad = FilterToLoad;
            this.FilterPosition = pos;
        }

        @Override
        public void run() {
            try {
                FilterToLoad.setResources(mOriginalBitmap, mCurrentBitmap, mFilterIds[FilterPosition], mPreview);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

有什么想法吗?

0 个答案:

没有答案