How to get the drawable from relative layout in android

前端 未结 1 615
春和景丽
春和景丽 2021-01-27 20:21

What i have done: I have set the bitmap for a relative layout as below

RelativeLayout root;
root= (RelativeLayout) itemView.findViewById(R.id.r         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 20:30

    There are a couple of ways you can do this:

    1. Try:

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
    layout.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(layout.getDrawingCache());
    layout.setDrawingCacheEnabled(false);
    

    2. Or try:

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
    Bitmap b = Bitmap.createBitmap( layout.getLayoutParams().width, layout.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    layout.layout(layout.getLeft(), layout.getTop(), layout.getRight(), layout.getBottom());
    layout.draw(c);
    

    To get the current ImageView

    1. To get current position, use

    ViewPager viewPager = getView().findViewById(r.id.pager);
    int position = viewPager.getCurrentItem();
    

    2. To get the current View, use

    View currentView = viewPager.getChildAt(int position);
    

    This currentView is the View created by the onCreateView() method of the current Fragment.

    3. To get the ImageView, use

    ImageView currentImage = (ImageView)currentView.findViewById(R.id.image);
    

    where R.id.image is the ImageView object you want from the current Fragment.

    0 讨论(0)
提交回复
热议问题