Taking a “screenshot” of a specific layout in Android

后端 未结 6 2009
天命终不由人
天命终不由人 2020-11-30 03:26

I have two main issues which are closely linked. I am looking at these problems from a programmatic point of view.

(1) - I wish to take a screenshot of the

6条回答
  •  迷失自我
    2020-11-30 03:54

    EDIT : after seeing OP's comment

    You dont need to think about new activities at all.. Say you are in Activity right now.. Layout A is the main layout for the activity, Layout B and C are two child layouts inside Layout A. Like this,

     Layout A -> Parent
     |
      -------Layout B
     |
      -------Layout C
    

    Now if you want to take screenshot of C only

    1) in onCreate() of activity

     LinearLayout myCLayout = (LinearLayout)this.findViewbyId(R.id.my_c_layout);
     ViewTreeObserver vto   =  myCLayout.getViewTreeObserver();
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @Override
       public void onGlobalLayout() {
          //fully drawn, no need of listener anymore
          myCLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
          getDrawingBitmap();
       }
     });
    

    where getDrawingBitmap() is a function..to take your screenshot..

    public void getDrawingBitmap(){
        LinearLayout myCLayout = (LinearLayout)this.findViewbyId(R.id.my_c_layout);
        Bitmap b = myCLayout.getDrawingCache();
    
        File file = saveBitmapAsFile(b);
    }
    

    EDIT: For ScrollView

    I never tried it.. But I think you can do this..

    1) first override scrollView, and override onMeasure function..

    public class MyScrollView extends ScrollView{
        public MyScrollView(Context context, AttributeSet attrs) {
             super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
            super.onMeasure(widthMeasureSpec, yourFullScrollViewHeight));
        }
    }
    

    and use MyScrollView in your layout.. here yourFullScrollViewHeight is the height of all scrollView content you need to take screenshot of.. I never tried this.. But it might work..

提交回复
热议问题