简体   繁体   中英

Screenshot of List view and convert it to an bitmap

I simply need a Screenshot as an Bitmap of my ListView. I can't figure out how to do this. The bitmap of the screen ist than used to blur it and set is as an background in another Fragment. Where do i take Screenshot from? BaseAdapter or my Fragment which contains the ListView? or in the new Fragment that gets opened after clicking an Item in the ListView?

UPDATE: Im calling the method inside a ViewTreeObserver from my New Fragment. The Method loadBitmapFromView works perfectly. My Problem now is i don't know how to get hold of the ListView which i want the picture from. The Params i use mContainer,mContainer.getWidth(),mContainer.getHeight() should change to the one from my ListView. (The params right now are form the new Fragment for testing purposes)

private void applyBlur() {

    mContainer.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mContainer.getViewTreeObserver().removeOnPreDrawListener(this);
            mContainer.buildDrawingCache();

            blur(MOKListViewFragment.loadBitmapFromView(mContainer,mContainer.getWidth(),mContainer.getHeight()), mContainer);
            return true;
        }
    });
}

This is how i call my new fragment from my BaseAdapter of the ListView which i actually want my picture from.

ImageView imageView = (ImageView) rowView.findViewById(R.id.thumb_button_1);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MOKPagerFragment pagerFragment = new MOKPagerFragment();
                    FragmentTransaction fragmentTransaction = ((Activity) mContext).getFragmentManager().beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransaction.add(R.id.fragment_container, pagerFragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            });

Sorry if this is confusing.

Take the screenshot from Fragment which contains the ListView , use this code to take the screenshot:

public static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}
if your phone is rooted try this

Process sh = Runtime.getRuntime().exec("su", null,null);

                OutputStream  os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();

                os.close();
                sh.waitFor();

then read img.png as bitmap and convert it jpg as follows

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+   File.separator +"img.png");

now you can use screen bitmap

refrence from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM