开发者

bitmap not being drawn in custom view

开发者 https://www.devze.com 2023-02-23 01:43 出处:网络
i\'ve an app that captures a picture from camera ans stores on sdcard. i\'ve created a custom view that reads that image to an array and creates a bitmap from it. i\'ve created another bitmap fr开发者

i've an app that captures a picture from camera ans stores on sdcard. i've created a custom view that reads that image to an array and creates a bitmap from it. i've created another bitmap fr开发者_StackOverflowom the original one using Bitmap.createBitmap(w,h,config), so that i've a mutable copy. in my view's onDraw the bitmap is not being drawn, just a black image is displayed. any ideas why the bitmap is not being drawn? thanks

BitmapFactory.Options bfo = new BitmapFactory.Options();
            bfo.inSampleSize = 1;
            bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
         bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());

and in my onDraw()

@Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);

            Log.e(TAG, "***********BITMAP bm = "+bm.toString());
            canvas.drawBitmap(bgr, 0, 0, null);

[edit]

public class LoadPic2 extends Activity {

    private static final String TAG = "*********LOADPIC";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "***********inside oncreate about to set contentview = ");
        setContentView(new TouchView(this));


    }

    class TouchView extends View{


        private File tempFile;
        private byte[] imageArray;
        Bitmap bgr;
        Bitmap bm;
        Bitmap overlayRed;
        Bitmap overlayBlue;
        Paint pTouch;
        int X = -100;
        int Y = -100;
        Canvas c2;

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

            Log.e(TAG, "***********inside Touchview constructor = ");

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

            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();
                }
            Log.e(TAG, "***********end of touchview imagearray  = "+ imageArray.length);
            BitmapFactory.Options bfo = new BitmapFactory.Options();
            bfo.inSampleSize = 1;
            bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
            bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
            bgr = bm.copy(bm.getConfig(), true);
           // Log.e(TAG, "***********bgr = "+bgr.toString()+bgr);

            overlayRed = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
            overlayRed.eraseColor(Color.RED);
            overlayBlue = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
            overlayBlue.eraseColor(Color.BLUE);
           // overlayRed = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
           // overlayBlue = BitmapFactory.decodeResource(getResources(),R.drawable.icon).copy(Config.ARGB_8888, true); 

            c2 = new Canvas(overlayBlue);//overlay

            pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
            pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
            pTouch.setColor(Color.TRANSPARENT);
            pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));

            Log.e(TAG, "***********end of touchview constructor = "+bm.toString());

        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {

            switch (ev.getAction()) {

                case MotionEvent.ACTION_DOWN: {

                    X = (int) ev.getX();
                    Y = (int) ev.getY();
                    invalidate();

                    break;
                }

                case MotionEvent.ACTION_MOVE: {

                        X = (int) ev.getX();
                        Y = (int) ev.getY();
                        invalidate();
                        break;

                }           

                case MotionEvent.ACTION_UP: 

                    break;

            }
            return true;
        }


        @Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);

            Log.e(TAG, "***********BITMAP bm = "+bm.toString());
            canvas.drawBitmap(bgr, 0, 0, null);
            c2.drawBitmap(overlayRed, 0, 0, null); //exclude this line to show all as you draw
            c2.drawCircle(X, Y, 80,pTouch); 
            canvas.drawBitmap(overlayBlue, 0, 0, null);



        }


    }




}


You are creating an entirely new mutable Bitmap of the same size, not making a mutable copy.

Try using Bitmap#copy:

bgr = bm.copy(bm.getConfig(), true);

The true parameter indicates that you want a mutable copy.

0

精彩评论

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