0

I have separate class for canvas ViewCanvas in this class I'm drawing two worlds. Here is the code:

public class ViewCanvas extends View {

    private Paint paint;
    private Typeface typeFace;

    public ViewCanvas(Context context) {
        super(context);
    }

    public ViewCanvas(Context context, String first, String second) {
        super(context);

        setDrawingCacheEnabled(true);
        buildDrawingCache(true);
        paint = new Paint();
        typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/BigNoodleTitling.ttf");
        bounds = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        paint.setColor(Color.BLACK);
        paint.setTextSize(20);
        paint.setTypeface(typeFace);

        // Closing hardware acceleration
        setLayerType(LAYER_TYPE_SOFTWARE, paint);

        // Drawing first word
        canvas.save();
        .
        .
        .
        .
        // Drawing second world
        canvas.restore();
        .
        .
        .
        .
    }
}

This code works fine. I'm using this ViewCanvas in MainActivity like this:

public class MainActivity extends Activity {

    private ViewCanvas viewCanvas;
    private Bitmap imageForShare;

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

        viewCanvas = new ViewCanvas(this, first, second);
        LinearLayout llCanvas = (LinearLayout) findViewById(R.id.llCanvas);
        llCanvas.addView(viewCanvas);


        imageForShare = viewCanvas.getDrawingCache();
}

Here imageForShare is null, and I don'n know why.

In my onClick method, viewCanvas.getDrawingCache() it's working great. imageForShare is not null and I can use it. Here is my code:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnFacebook:
            imageForShare = viewCanvas.getDrawingCache();
            break;
    }
}

Where is the problem, and I want imageForShare to be available in my onCreate method.

Ziem
  • 6,579
  • 8
  • 53
  • 86
KiKo
  • 1,668
  • 7
  • 27
  • 56

3 Answers3

0

try this:

public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().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;
}

link

Community
  • 1
  • 1
Piotr Golinski
  • 990
  • 8
  • 19
0

Can you not use OnStart() to get the screenshot?
The visible lifetime of an activity happens between a call to onStart()

Shailesh
  • 490
  • 5
  • 11
  • I need it because I want to share with Pinterest. – KiKo Apr 23 '15 at 14:42
  • use a boolean flag to run the code only once in onStart() and you can move the onCreate() code to onStart(). Let me know if you need further explanation. – Shailesh Apr 23 '15 at 14:45
  • public class MainActivity extends Activity { private boolean subCreate; private Bitmap imageForShare; private ViewCanvas viewCanvas; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); subCreate = false; } protected void onStart() { super.onStart(); if (!subCreate) { subCreate = true; // Rest of your onCreate code } } } – Shailesh Apr 23 '15 at 15:02
0

OK, I found a solution. The creating of imageForShare in onCreate method is impossible because canvas is drawing after imageForShare is created. So i put creation of imageForShare in onWindowFocusChanged, and is working great:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    imageForShare = viewCanvas.getDrawingCache(); 
}
KiKo
  • 1,668
  • 7
  • 27
  • 56