Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

android - Draw a circle on an existing image

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:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);

    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(60, 50, 25, paint);

    ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);
    imageView.setAdjustViewBounds(true);
    imageView.setImageBitmap(bitmap);

any help will be highly appreciated. thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...