RecycleView内部的Android ViewPager获取RuntimeException:Canvas:尝试绘制太大(207360000bytes)的位图

时间:2017-10-22 18:32:59

标签: java android android-fragments canvas bitmap

我在RecycleView中有一个带有复杂视图项的ViewPager。 RecycleView将被放入片段中。当我将片段加载到Activity中时。我看到片段的onCreateView方法被调用。但我总是得到RuntimeException anvas:尝试绘制太大(207360000bytes)的位图作为日志:

  

E / AndroidRuntime:致命异常:主要                                                                            过程:myappname,PID:8986                                                                            java.lang.RuntimeException:Canvas:尝试绘制太大(207360000bytes)的位图。                                                                                在android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)                                                                                在android.graphics.Canvas.drawBitmap(Canvas.java:1415)                                                                                在android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)                                                                                在android.widget.TextView.onDraw(TextView.java:5745)                                                                                在android.widget.CheckedTextView.onDraw(CheckedTextView.java:380)                                                                                在android.view.View.draw(View.java:17071)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.draw(View.java:17074)                                                                                在android.support.design.internal.ForegroundLinearLayout.draw(ForegroundLinearLayout.java:196)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4248)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.draw(View.java:17074)                                                                                在android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3657)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.draw(View.java:17074)                                                                                在android.support.design.internal.ScrimInsetsFrameLayout.draw(ScrimInsetsFrameLayout.java:87)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16048)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.draw(View.java:17074)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16048)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16048)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16048)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16048)                                                                                在android.view.View.draw(View.java:16837)                                                                                在android.view.ViewGroup.drawChild(ViewGroup.java:3764)                                                                                在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)                                                                                在android.view.View.draw(View.java:17074)                                                                                在com.android.internal.policy.DecorView.draw(DecorView.java:751)                                                                                在android.view.View.updateDisplayListIfDirty(View.java:16053)                                                                                在android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)                                                                                在android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)                                                                                在android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)                                                                                在android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)                                                                                在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)                                                                                在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)

这是我的代码: 1.我的ViewPager适配器:

    public class HighLightAdapter extends PagerAdapter {

    private final String TAG = "VT_HighLightAdapter";

    private List<HighLightModelReponse> dataModels;

    public HighLightAdapter(List<HighLightModelReponse> dataModels){
        Log.d(TAG,"HighLightAdapter contructor");
        this.dataModels = dataModels;
    }

    @Override
    public int getCount() {
        return dataModels.size();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.d(TAG,"instantiateItem");
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.hightlights_items,container,false);
        RelativeLayout base_item = (RelativeLayout) itemView.findViewById(R.id.base_item);
        //header
        TextView header_catalog = (TextView) itemView.findViewById(R.id.tv_header_catalog);
        TextView header_details = (TextView) itemView.findViewById(R.id.tv_header_detail);
        //content image
        ImageView image_preview = (ImageView) base_item.findViewById(R.id.img_preview);
        ImageView image_delete = (ImageView) base_item.findViewById(R.id.img_delete_favorate);
        image_delete.setVisibility(View.INVISIBLE);
        //footer
        TextView footer_title = (TextView) base_item.findViewById(R.id.tv_footer_title);
        TextView footer_slogan = (TextView) base_item.findViewById(R.id.tv_footer_slogan);
        TextView footer_info = (TextView) base_item.findViewById(R.id.tv_footer_info);
        //rating
        RatingBar ratingBar = (RatingBar) base_item.findViewById(R.id.footer_rating);
        TextView numberRate = (TextView) base_item.findViewById(R.id.tv_number_rate);

        TextView numberComments = (TextView) base_item.findViewById(R.id.tv_number_comment);

        HighLightModelReponse highLightModelReponse = dataModels.get(position);
        String image_url = highLightModelReponse.getImage_url();
        if(image_url != null && !image_url.isEmpty()){
            Picasso.with(container.getContext())
                    .load(image_url)
                    .resize(image_preview.getWidth(),image_preview.getHeight())
                    .placeholder(R.drawable.ic_contact)
                    .error(R.drawable.ic_contact)
                    .into(image_preview, new Callback() {
                        @Override
                        public void onSuccess() {
                            Log.d(TAG,"Picasso onLoad success");
                        }

                        @Override
                        public void onError() {
                            Log.d(TAG,"Picasso onLoad Error");
                        }
                    });
           /* UiUtils.loadImageFromLink(container.getContext(),image_url,image_preview,image_preview.getWidth(),image_preview.getHeight());
            image_preview.setScaleType(ImageView.ScaleType.CENTER_CROP);*/
        }
        header_catalog.setText(highLightModelReponse.getCatalog());
        header_details.setText("Xem chi tiết");

        footer_title.setText(highLightModelReponse.getTitle());
        footer_slogan.setText(highLightModelReponse.getSlogan());
        footer_info.setText(highLightModelReponse.getMore_infor());

        ratingBar.setRating(highLightModelReponse.getAverRating());
        numberRate.setText(String.valueOf(highLightModelReponse.getNumberRating()));
        numberComments.setText(String.valueOf(highLightModelReponse.getNumberComments()));
        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager)container).removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    }
  1. 这是我的RecycleView Adaper:

    public class HighLightRecycleAdapter extends RecyclerView.Adapter {
    
    private final String TAG = "VT_HighLightRecycleAdapter";
    private List<List<HighLightModelReponse>> highLightData;
    
    private Context mContext;
    
    
    public HighLightRecycleAdapter(Context context,List<List<HighLightModelReponse>> datas){
        Log.d(TAG,"HighLightRecycleAdapter contructor");
        this.mContext = context;
        highLightData = datas;
    }
    
    
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pager_item,parent,false);
        return new HighLightViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        List<HighLightModelReponse> pagerDatas = highLightData.get(position);
        initializeViews(pagerDatas,holder,position);
    }
    
    @Override
    public int getItemCount() {
        return highLightData.size();
    }
    
    private void initializeViews(List<HighLightModelReponse> dataModel, final RecyclerView.ViewHolder holder, int position){
    
        Log.d(TAG,"initializeViews");
        HighLightAdapter adapter = new HighLightAdapter(dataModel);
        ((HighLightViewHolder)holder).viewPager.setAdapter(adapter);
        ((HighLightViewHolder)holder).viewPager.setClipToPadding(false);
        ((HighLightViewHolder)holder).viewPager.setPadding(40, 0, 40, 0);
        ((HighLightViewHolder)holder).indicator.setViewPager(((HighLightViewHolder)holder).viewPager);
        final float density = mContext.getResources().getDisplayMetrics().density;
        ((HighLightViewHolder)holder).indicator.setRadius(5 * density);
    }
    
    public static class HighLightViewHolder extends RecyclerView.ViewHolder{
    
        private final String TAG = "HighLightViewHolder";
    
        @BindView(R.id.view_pager)
        ViewPager viewPager;
        @BindView(R.id.indicator)
        CirclePageIndicator indicator;
    
        public HighLightViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            Log.d(TAG, "HighLightViewHolder contructor");
        }
    }
    }
    
  2. My Fragment

    public class HighLightFragment extends BaseFragment<HighLightPresenter> implements HighLightContract.View {
    
    private final String TAG = "VT_HighLightFragment";
    
    private List<List<HighLightModelReponse>> mHighLightData;
    
    @BindView(R.id.highlight_recycleview)
    RecyclerView mHighlightRecycleview;
    
    
    @Override
    protected int getLayoutId() {
        return R.layout.hightlights_activity;
    }
    
    @Override
    protected void initEventAndData() {
        Log.d(TAG,"initEventAndData");
        initDataHighLightModelTest();
    
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
    
        HighLightRecycleAdapter recycleAdapter = new HighLightRecycleAdapter(mContext,mHighLightData);
        mHighlightRecycleview.setAdapter(recycleAdapter);
        DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(mHighlightRecycleview.getContext(), mLayoutManager.getOrientation());
        mHighlightRecycleview.addItemDecoration(mDividerItemDecoration);
    }
    
    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
    
    
    private void initDataHighLightModelTest(){
        String catalog1 = "catalog1";
        String catalog2 = "catalog2";
        String catalog3 = "catalog3";
        String catalog4 = "catalog4";
    
        String title1 = "title1";
        String slogan1 = "slogan1";
        String detail1 = "detail1";
        float aver_rating1 = 4.5f;
        int rating_number1 = 7;
        int comment_number1 = 16;
        String image_cover1 = "https://khachsan.dulichvietnam.com.vn/uploads/image/tin-tuc-khach-san/khach-san-grand-plaza-ha-noi4.JPG";
    
        String title2 = "title2";
        String slogan2 = "slogan2";
        String detail2 = "";
        float aver_rating2 = 4.5f;
        int rating_number2 = 7;
        int comment_number2 = 16;
        String image_cover2 = "https://kenh14cdn.com/2016/3-1476470249125.jpg";
    
        String title3 = "title3";
        String slogan3 = "slogan3";
        String detail3 = "";
        float aver_rating3 = 4.5f;
        int rating_number3 = 7;
        int comment_number3 = 16;
        String image_cover3 = "http://vietnamtourism.gov.vn/images/2014/ManchinSaPa.jpg";
    
    
        HighLightModelReponse h1 = new HighLightModelReponse(catalog1,title1,slogan1,detail1,image_cover1,comment_number1,rating_number1,aver_rating1);
        /*HighLightModelReponse h2 = new HighLightModelReponse(catalog2,title2,slogan2,detail2,image_cover2,comment_number2,rating_number2,aver_rating2);
        HighLightModelReponse h3 = new HighLightModelReponse(catalog3,title3,slogan3,detail3,image_cover3,comment_number3,rating_number3,aver_rating3);*/
    
        List<HighLightModelReponse> pagerItem1 = Arrays.asList(new HighLightModelReponse[]{h1});
        /*List<HighLightModelReponse> pagerItem2 = Arrays.asList(new HighLightModelReponse[]{h1,h2,h3});
        List<HighLightModelReponse> pagerItem3 = Arrays.asList(new HighLightModelReponse[]{h1,h2,h3});*/
        mHighLightData = new ArrayList<>();
        mHighLightData.add(pagerItem1);
       /* mHighLightData.add(pagerItem2);
        mHighLightData.add(pagerItem3);*/
    }
    }
    
  3. 这是我的活动代码:

    public class MainActivity extends BaseActivity<MainPresenter> implements 
    MainContract.View, NavigationView.OnNavigationItemSelectedListener, 
    BottomNavigationView.OnNavigationItemSelectedListener {
    private final String TAG = "VT_MainActivity";
    
    private HighLightFragment mHighLightFragment;
    
    private int hideFragment = Constants.HIGH_LIGHT_FRAGMENT;
    private int showFragment = Constants.HIGH_LIGHT_FRAGMENT;
    
    @BindView(R.id.fragment_container)
    FrameLayout mFragmentContainter;
    
    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
    
    @Override
    protected void initEventAndData() {
        Log.d(TAG,"initEventAndData");
        initFragment();
        /*showFragment = mPresenter.getCurrentFragment();
        hideFragment = Constants.HIGH_LIGHT_FRAGMENT;
        showHideFragment(getTargetFragment(showFragment), getTargetFragment(hideFragment));
        hideFragment = showFragment;*/
        mHighLightFragment = new HighLightFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container,mHighLightFragment,"highlight");
        ft.commit();
    
    }
    
    
    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int viewId = item.getItemId();
        switch (viewId) {
            case R.id.nav_login:
                break;
            case R.id.nav_notify:
                break;
            case R.id.nav_response:
                break;
            case R.id.nav_contact:
                break;
            case R.id.nav_rate:
                break;
            case R.id.nav_share:
                break;
            case R.id.nav_intro:
                break;
            case R.id.menu_hot:
                showFragment = Constants.HIGH_LIGHT_FRAGMENT;
                break;
            case R.id.menu_map:
                break;
            case R.id.menu_faviorate:
                break;
            case R.id.menu_event:
                break;
            case R.id.menu_register:
                break;
            default:
                break;
        }
        return true;
    }
    
    private void initFragment() {
        Log.d(TAG,"initFragment");
        mHighLightFragment = new HighLightFragment();
    }
    
    private Fragment getTargetFragment(int item) {
        switch (item) {
            case Constants.HIGH_LIGHT_FRAGMENT:
                Log.d(TAG,"getTargetFragment HIGH_LIGHT_FRAGMENT");
                return mHighLightFragment;
            case Constants.EVENT_FRAGMENT:
                return null;
            case Constants.MAP_FRAGMENT:
                return null;
            case Constants.FAVIORATA_FRAGMENT:
                return null;
            case Constants.REGISTER_FRAGMENT:
                return null;
            default:
                return null;
        }
    }
    
    private void showHideFragment(Fragment showFragment, Fragment hideFragment) {
        try {
            Log.d(TAG,"showHideFragment showFragment " + showFragment + "hideFragment " + hideFragment);
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                    .remove(hideFragment)
                    .show(showFragment)
                    .commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
    
  4. 有人可以帮助我! 非常感谢你的帮助

    P / s:我无法发布我的布局和Fragment和Activity文件的一些基类,因为它使这个帖子太大(超过30000个字符)。如果您需要任何课程或布局,请免费告诉我。

0 个答案:

没有答案