Search code examples
androidviewbitmapviewgroup

How to add a bitmap to a view, then add that view to a viewgroup


Maybe a simple question but i have an activity that retrieves a bitmap(camerashot) from a folder on the sdcard. The activity's custom view extends viewgroup. i'd like to add the bitmap to the view then add that view to the view group then display. eventually there will be multiple views added in this way. what are the best practices for this? for example would you get the activity to get the bitmap first, then in the viewgroup file access that bitmap with the following.

bitmap bm = (ActivityName()getContext()).methodToGetBitmap();

The bitmap can be added to a view, which inturn can be addeded to the viewgroup then displayed. thanks

[update] activity

public class HorizontalPagerActivity extends Activity {


    private static final String TAG = "*********hpActivity";
    private Context mContext = this;
    File tempFile;
    byte [] imageArray;
    private Bitmap b = null;

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

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);



            setContentView(R.layout.hpview);
            final ViewGroup viewgroup = (ViewGroup)findViewById(R.id.hpview); 




            tempFile = new File(Environment.getExternalStorageDirectory().
                    getAbsolutePath() + "/"+"image.jpeg");

            imageArray = new byte[(int)tempFile.length()];





         try{

                InputStream is = new FileInputStream(tempFile);
                BufferedInputStream bis = new BufferedInputStream(is);
                DataInputStream dis = new DataInputStream(bis);


                int i = 0;

                while (dis.available() > 0 ) {
                imageArray[i] = dis.readByte();
                i++;
                }

                dis.close();


           } catch (Exception e) {

                   e.printStackTrace();
                }



           Bitmap bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length);
          // Bitmap b = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
           b = bm.copy(bm.getConfig(), true);

            if(b == null){
                Log.e(TAG, "b = null");
            }else{
                Log.e(TAG, "b =  not null");
            }






             Canvas canvas = new Canvas(b);
             Log.e(TAG, "canvas created");




             View view = new View(this);
             Log.e(TAG, "view created");
             LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT);
                view.setLayoutParams(lp);
                view.draw(canvas);


             viewgroup.addView(view);
             Log.e(TAG, "view added to viewgroup");


             runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    viewgroup.postInvalidate();
                }
            });

             Log.e(TAG, " inval called on viewgroup");

             Log.e(TAG, "no of chidren = "+viewgroup.getChildCount());





     }


}

the activity's view ia a viewgroup. so this is the parent view. the activity preprocesses the bitmap, creates and adds the views dynamically. it also calls invalidate on the parent viewgroup using runOnUiThread().


Solution

  • Get the parent view, and then dynamically append views to it.

    Of course the activity would have to do the preprocessing and also append those views to the view group. Ideally this should be done in an AsyncTask, so that your activity can handle its current function. When the AsyncTask finshes, either use a handler to update the views/append or just run the update on the ui thread (runOnUIThread(...)).