Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8769431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:20:50+00:00 2026-06-13T17:20:50+00:00

Here is my scenario: I have one layout with a view where I can

  • 0

Here is my scenario:

I have one layout with a view where I can cal and get canvas on its onDraw() method. Now, upon an event, say onTouchEvent(), I would like to clip a portion of the canvas and display this portion into another view of another layout.

I found code snippet doing similar thing but they are based on the java.awt package which does not supported by the Android API, especially the core component like java.awt.Graphics2D

I would like to follow the similar algorithm using canvas instead of the Graphics2D in Android to draw its content into another canvas. How do we achieve this?

Basically, I would like to achieve the same result as done in the following code in java.awt:

protected void paint(Graphics2D g2) {
Ellipse2D ellipse = //create ellipse which is to be displayed in another surface
g2.fill(ellipse);  //how to achieve the same result in Android?
g2.draw(ellipse);  //how to achieve the same result in Android?
g2.clip(ellipse);  //how to achieve the same result in Android?
....
... 
//note: Afaik, in Android, the code like Drawable.draw(canvas, ....),
//will achieve result in the opposite way where the Drawable would be drawn into
//the Canvas whereas G2.fill(ellipse) would fill the ellipse with the G2's content
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:20:51+00:00Added an answer on June 13, 2026 at 5:20 pm

    If I understood correctly what you need, you could try the code as follows.

    This is how the activity’s xml layout file look like:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <com.example.test.SourceImageView
            android:id="@+id/srcImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/landscape" /> <!-- landscape.png is the picture on resources -->
    
        <ImageView
            android:id="@+id/dstImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/srcImage"
            android:layout_centerHorizontal="true" />
    
    </RelativeLayout>
    

    The activity’s source code:

    public class MainActivity extends Activity implements SourceImageView.OnCropListener {
    
        private SourceImageView srcImageView;
        private ImageView       dstImageView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            srcImageView = (SourceImageView) findViewById(R.id.srcImage);
            dstImageView = (ImageView) findViewById(R.id.dstImage);
    
            srcImageView.setOnCropListener(this);
        }
    
        @Override
        public void onCrop(Bitmap bitmap) {
            dstImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 50, 50, true));
        }
    }
    

    The custom view I called SourceImageView:

    public class SourceImageView extends ImageView implements OnTouchListener {
    
        private float           x;
        private float           y;
        private Paint           paint;
        private Bitmap          bitmap;
        private OnCropListener  cropListener;
    
        public SourceImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOnTouchListener(this);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
            bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
        }
    
        @Override
        protected void onDetachedFromWindow() {
            bitmap.recycle();
            super.onDetachedFromWindow();
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
    
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                x = event.getX();
                y = event.getY();
    
                if (cropListener != null) {
                    if (x + 20 > bitmap.getWidth() || y + 20 > bitmap.getHeight() || (y - 20) < 0 || (x - 20) < 0) {
                        return true;
                    }
                    Bitmap b = Bitmap.createBitmap(bitmap, (int) x - 20, (int) y - 20, 40, 40);
                    Bitmap overlay = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(overlay);
    
                    c.clipRect(0, 0, b.getWidth(), b.getHeight());
    
                    Path path = new Path();
                    path.addCircle(20, 20, 20, Path.Direction.CW);
                    c.clipPath(path, Op.INTERSECT);
                    c.drawBitmap(b, 0, 0, null);
    
                    cropListener.onCrop(overlay);
    
                    Bitmap overlay2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(overlay2);
                    canvas.drawBitmap(bitmap, 0, 0, null);
                    canvas.drawCircle(x, y, 20, paint);
                    bitmap = overlay2;
                    setImageBitmap(bitmap);
                }
            }
            return true;
        }
    
        public void setOnCropListener(OnCropListener cropListener) {
            this.cropListener = cropListener;
        }
    
        static interface OnCropListener {
            public void onCrop(Bitmap bitmap);
        }
    
    }
    

    Be careful when dealing with bitmaps, they can be an expensive source of memory leaks, specially if you are working with older versions of android. I haven’t checked for leaks in this code. Probably this can be improved (refactored, optimized).

    if this isn’t what you want, I hope that helps giving you ideas and pointing you to the right direction.

    ==EDIT==

    It may not be clear so I must say that I assumed as an example that the cropped part of the image is an area of a circle of 20 px radius.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: I have 2 tables with data, one is the 2009 version
Here is the scenario, I have a WCF service call that takes one string
I have one scenario in which the user can define the column and that
Here is the scenario: You have a Persons table with a one-to-many relationship with
Here is the scenario: I have 2 buttons, one in a form with the
Here's my scenario: I have one page that collects certain data from the user.
I have one scenario here, and maybe someone could enlighten me at this corner
Here's scenario: I have 2 activities and one service First activity is a landing
Here is the scenario: You have two images and they are stacked on one
So, Here is my scenario: I have two activity and one service --> all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.