简体   繁体   中英

Create Bitmap from Activity

I am a bit new to Android. But have developed a lot to iOS.

I have a parent activity (Activity1), it haves to load and setup another activity (Activity2) in the background. Then Activity2 is ready, it have to call a method that can take the Activity2 and return a bitmap image.. ( I need this image for facebook share. )

So my questions are:

1.How do it initials a second activity with out showing it

2.How do I take the second activity and return it as a bitmap

    public class Activity1 extends Activity implements OnClickListener, KKServerApiListener {

    private Activity2 facebookshareActivity;


   @Override
    protected void onCreate(final Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_summary);

      initView();


      /* Try 1 */
      facebookshareActivity = new KKFacebookShareActivity(route);
      /* Tried with some different things here */

      /* Try 2 */
      Intent myIntent = new Intent(this, KKFacebookShareActivity.class);
      myIntent.putExtra("number", route.id); 
      this.startActivity(myIntent);  
  }

  public void makeDialogShare(final Context context, final String str) {
        /*SETTING UP THE DIALOG */

        // ... //

        // ... //

        dialogButtonOk.setText("Share");
        dialogButtonOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {

                /*  My try on getting my activity to a bitmap */
                View screenView = ****
                screenView.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
                screenView.setDrawingCacheEnabled(false);

                /* Haven't worked  on this part just something i pasted from facebook */
                SharePhoto photo = new SharePhoto.Builder()
                        .setBitmap(bitmap)
                        .build();
                SharePhotoContent content = new SharePhotoContent.Builder()
                        .addPhoto(photo)
                        .build();

                showDashboard(ctx, true);
                //finish();
            }
        });
        final Button dialogButtonCancel = (Button) dialog.findViewById(R.id.btnCancel);
        dialogButtonCancel.setText("Continue");
        dialogButtonCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                dialog.dismiss();
                //finish();
            }
        });
        dialog.show();
    }
 }

If you necessary need to get an Image of what you have in Activity2 you can try inflating the layout of the Activity. Then create a Bitmap with the View just inflated and return it. Something like this in your code:

/*  My try on getting my activity to a bitmap */
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View screenView = inflater.inflate(R.layout.your_activity_layout, null);

        // add views and set rules (same as in the XML) to screenView
        ...

        screenView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        screenView.layout(0, 0, screenView.getMeasuredWidth(), screenView.getMeasuredHeight());

        Bitmap bitmap = Bitmap.createBitmap(screenView.getMeasuredWidth(), screenView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        screenView.draw(c);

        ...

            /* Haven't worked  on this part just something i pasted from facebook */
            SharePhoto photo = new SharePhoto.Builder()
                    .setBitmap(bitmap)
                    .build();
            SharePhotoContent content = new SharePhotoContent.Builder()
                    .addPhoto(photo)
                    .build();

            showDashboard(ctx, true);

R.layout.your_activity_layout is the layout you want to share.

Note: Since you are creating the view manually you will probably need to add some views and set configurations programmatically in order to recreate the layout (XML of you view).

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