1

也许是一个简单的问题,但我有一个从 sdcard 上的文件夹中检索位图(相机)的活动。活动的自定义视图扩展了视图组。我想将位图添加到视图中,然后将该视图添加到视图组中,然后显示。最终会有多个视图以这种方式添加。对此的最佳做法是什么?例如,您是否会首先获得获取位图的活动,然后在视图组文件中使用以下内容访问该位图。

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

位图可以添加到视图中,而视图又可以添加到视图组中然后显示。谢谢

【更新】活动

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());





     }


}

活动的视图是一个视图组。所以这是父视图。活动预处理位图,动态创建和添加视图。它还使用 runOnUiThread() 在父视图组上调用 invalidate。

4

2 回答 2

1

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(...)).

于 2012-01-12T22:46:38.340 回答
0

我只需要将视图更改为 ImageView 并且上面的代码有效。所以我认为 Jox 的建议是正确的.... 获取父视图组,然后动态添加视图。如果是图像,则使用 imageview。

于 2012-01-16T13:55:38.273 回答