tags:

views:

108

answers:

2

I wrote code to draw on a view. After it is done, how can I get the resulting image from the view. For example, in the code below, I want to get the the drawable (Image) from mCustomDrawableView. How can I do that? Thanks.

public class HelloTestGraph extends Activity {
    /** Called when the activity is first created. */

    // @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        LinearLayout lo = (LinearLayout) findViewById(R.id.top_view);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        CustomDrawableView mCustomDrawableView = new CustomDrawableView(this);
        mCustomDrawableView.setLayoutParams(param);
        lo.addView(mCustomDrawableView);
    }

    public class CustomDrawableView extends View {
        private ShapeDrawable mDrawable;
        private Drawable mPic;

        public CustomDrawableView(Context context) {
            super(context);

            int x = 10;
            int y = 10;
            int width = 300;
            int height = 50;

            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xff74AC23);
            mDrawable.setBounds(x, y, x + width, y + height);

            mPic = getResources().getDrawable(R.drawable.example_picture);
            mPic.setBounds(x, y + 100, x + width, y + height+100);
        }

        protected void onDraw(Canvas canvas) {
            mDrawable.draw(canvas);
            mPic.draw(canvas);
        }
    }
}
A: 

From inside your custom class, you'd specify getters and setters if you wanted to access them from outside the class.

public Drawable getDrawable() { return mDrawable; }

Then from outside the class (as in your activity), you can call the getDrawable() method on the view once it's instantiated.

Drawable drawable = mCustomDrawableView.getDrawable();
Peter
+1  A: 

This is a little convoluted, but should get you there.

Step 1: Create a Muteable Bitmap of the size you want and stash it aside. It could be the size of the device's screen or the size of your largest view (depending on what you want to do with it later) Save that bitmap pointer aside as something like myBitmap

Step 2: Create a canvas with the aforementioned bitmap. "Canvas myCanvas = new Canvas(myBitmap);"

Step 3: In your onDraw() method, draw your views both to the passed in "canvas" object, and to your own custom one.

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);
    mPic.draw(canvas);
    mDrawable.draw(myCanvas);
    mPic.draw(myCanvas);
}

Step 4: Your original bitmap should now contain the fully rendered version of ONLY your view.

I'm not sure if this is exactly what you're looking for, but it will give you a bitmap (which you can convert into an image) of the contents of your view.

haseman
You approach is right. I also searched on the web and found I can use the following code to do that: Bitmap image = Bitmap.createBitmap(mCustomDrawableView.getWidth(), mCustomDrawableView.getHeight(), Bitmap.Config.RGB_565); mCustomDrawableView.draw(new Canvas(image));