Draw a circle on an existing image

后端 未结 2 586
野的像风
野的像风 2020-12-30 13:13

I\'m trying to draw a circle on an image which placed as res/drawable/schoolboard.png. the image fills the activity background. the following does not work:

相关标签:
2条回答
  • 2020-12-30 13:35

    There are some errors in your code: first of thing you cannot give reference Id for drawable in findViewById so I think you mean something like that

    ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
    

    schoolboard_image_view is the image id in your xml layout (check your layout for the right id)

    BitmapFactory.Options myOptions = new BitmapFactory.Options();
        myOptions.inDither = true;
        myOptions.inScaled = false;
        myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
        myOptions.inPurgeable = true;
    
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard,myOptions);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
    
    
        Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    
    
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawCircle(60, 50, 25, paint);
    
        ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(mutableBitmap);
    

    Please make sure to use the right image Id for:

    ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);

    0 讨论(0)
  • 2020-12-30 13:53

    First of all you need to create a new bitmap, because bitmap from BitmapFactory.decodeResource() method is immutable. You can do this with the following code:

    Bitmap canvasBitmap = Bitmap.createBitmap([bitmap_width], [bitmap_height], Config.ARGB_8888);
    

    Use this bitmap in Canvas constructor. Then draw your bitmap on canvas.

    Canvas canvas = new Canvas(canvasBitmap);
    canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
    canvas.drawCircle(60, 50, 25, paint);
    

    Also R.drawable.schoolboard is not a correct view id.

    ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);

    0 讨论(0)
提交回复
热议问题