0

我正在 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();
            }
        }
    }

任何想法?

4

2 回答 2

0

您不能更改 ThreadPool 线程中的视图,使用主线程处理程序发布消息并在主线程中处理它。或者像这样使用 AsyncTask处理 UI 线程中的位图

于 2015-07-15T10:12:51.203 回答
0

将活动作为适配器的参数传递,然后调用:

activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //change the image here
                        }
                    });
于 2015-07-15T10:21:52.003 回答