开发者

How do I display an Bitmap class object in android without using XML

I am trying to display an bitmap I created in and android program, but all the tutorials I find involve either Drawables or XML in order to display them. Can someone show me the steps needed to display an Bitmap in code?

This is not the entirety of the code, this is just the majority, the rest is related to getting the camera working.

class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Paint mPaint;
privat开发者_Go百科e Drawable bitmap;
private Bitmap currentprev;
private ImageView mImageView;

private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
{
        public void onPreviewFrame( byte[] data, Camera Cam ) {
                Log.d("CombineTestActivity", "Preview registered");
                Log.d("CombineTestActivity", "Data length = " 
                        + data.length );
               // currentprev = BitmapFactory.decodeByteArray( data, 0, 
               //       data.length );
               currentprev = BitmapFactory.decodeResource( getResources(),
                   R.drawable.creature00 );
               if( currentprev == null )
                   Log.d("CombineTestActivity", "currentprev is null" );
               if( mImageView == null )
                   Log.d("CombineTestActivity", "mImageView is null" );
        //Code fails here, gives null pointer exception
                mImageView.setImageBitmap( currentprev );
                Log.d("CombineTestActivity", "Preview Finished" );
        }
};

private OnTouchListener mCorkyListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {
                Log.d("CombineTestActivity", "touch registered" );
                mCamera.takePicture( null, null, mPicCallback );
                return false;
        }
};

private Camera.PictureCallback mPicCallback = new Camera.PictureCallback() {
        public void onPictureTaken( byte[] data, Camera mCamera ) {

                Log.d("CombineTestActivity", "picture method run" );
        }
};

Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mImageView = (ImageView)findViewById(R.id.imageview);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mPaint = new Paint();
    bitmap = context.getResources().getDrawable(
        R.drawable.creature00);
    if( mImageView == null )
        Log.d("CombineTestActivity", "mImageView is null" );

this.setOnTouchListener( mCorkyListener );

}


You need to use the BitmapFactory to create the Bitmap and load it into the ImageView. Like this:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
ImageView view = (ImageView)findViewById(R.id.my_image_view);
view.setImageBitmap(image);


Bitmap bm; // some bitmap you have loaded.
ImageView iv; // the image view you want to display the bitmap in

iv.setImageBitmap(bm);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜