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 6171889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:17:52+00:00 2026-05-23T23:17:52+00:00

i want to custom view,it can include some effect:first draw a pic on the

  • 0

i want to custom view,it can include some effect:first draw a pic on the view as a background then draw a rectangle on the image,and draw other image named A in the rectangle, we know the rectangle have four fixed point,when i drag one of these,the rectangle can scale,at the same time A also is scale, i have read more link,but not find good example,i have done something,but cannot finish the scale rectangle,my code is:

public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";

private static final int LineLength = 30;

Paint paint = new Paint();
float locationX, locationY;
private int mLastTouchX;
private int mLastTouchY;

private int mPosX;
private int mPosY;

private int mPosX1;
private int mPosY1;
Bitmap bitmap, bmp, xiao;


int screenWidth, screenHeight;

int xLength;

boolean isFirst = true;
boolean isLeft = false;

Rect r, rBig,outRect;

public DrawView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    screenHeight = wm.getDefaultDisplay().getHeight();
    screenWidth = wm.getDefaultDisplay().getWidth();
    mPosX = screenWidth / 2;
    mPosY = screenHeight / 2;

    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    xiao = BitmapFactory.decodeResource(getResources(),
            R.drawable.msn_protocol);

    xLength = (int) Math.hypot(xiao.getWidth(), xiao.getHeight());

    r = new Rect();
    r.set((int) mPosX - LineLength - xiao.getWidth(), (int) mPosY
            - LineLength - xiao.getHeight(), (int) mPosX - LineLength,
            (int) mPosY - LineLength);

//  Log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);
    rBig = new Rect();
    rBig.set((int) mPosX - LineLength, (int) mPosY - LineLength,
            (int) mPosX + LineLength, (int) mPosY + LineLength);
    //Log.i("r", rBig.left + " " + rBig.top + " " + rBig.right + " " + rBig.bottom);



}

@Override
public void onDraw(Canvas canvas) {



    canvas.drawBitmap(bitmap, 0, 0, null);

    canvas.drawBitmap(xiao, mPosX - LineLength - xiao.getWidth(), mPosY
            - LineLength - xiao.getHeight(), null);


    canvas.drawLine(mPosX - LineLength,
            mPosY - LineLength - xiao.getHeight() / 2, mPosX + LineLength,
            mPosY - LineLength - xiao.getHeight() / 2, paint);


    canvas.drawLine(mPosX - LineLength - xiao.getWidth() / 2, mPosY
            - LineLength, mPosX - LineLength - xiao.getWidth() / 2, mPosY
            + LineLength, paint);


    canvas.drawBitmap(xiao, mPosX + LineLength,
            mPosY - LineLength - xiao.getHeight(), null);


    canvas.drawBitmap(xiao, mPosX - LineLength - xiao.getWidth(), mPosY
            + LineLength, null);


    canvas.drawBitmap(xiao, mPosX + LineLength, mPosY + LineLength, null);


    canvas.drawLine(mPosX + LineLength + xiao.getWidth() / 2, mPosY
            - LineLength, mPosX + LineLength + xiao.getWidth() / 2, mPosY
            + LineLength, paint);


    canvas.drawLine(mPosX - LineLength,
            mPosY + LineLength + xiao.getHeight() / 2, mPosX + LineLength,
            mPosY + LineLength + xiao.getHeight() / 2, paint);


    if (isLeft) {



        Matrix matrix = new Matrix();
        matrix.preScale(0.8f, 0.8f);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0,
                bmp.getWidth(), bmp.getHeight(), matrix, true);
        canvas.drawBitmap(rotatedBitmap, mPosX - LineLength, mPosY
                - LineLength, null);


    }

}

public boolean onTouch(View view, MotionEvent event) {

    isFirst = false;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        mPosX1 = (int) event.getX();
        mPosY1 = (int) event.getY();
        mLastTouchX = mPosX1;
        mLastTouchY = mPosX1;


    //  Log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);
        Log.i("ACTION_DOWN", "" + mPosX1 + " " + mPosY1);
        if (r.contains(mPosX1, mPosY1)) {
            isLeft = true;

            invalidate();
        } 

        break;
    }

    case MotionEvent.ACTION_MOVE: {

         int x = (int) event.getX();
         int y = (int) event.getY();
         Log.i("aa",""+x+""+y);




         int dx = x - mLastTouchX;
         int dy = y - mLastTouchY;

        mLastTouchX = x;
        mLastTouchY = y;

        mPosX += dx;
        mPosY += dy;

        r.set((int) mPosX - LineLength - xiao.getWidth(), (int) mPosY
                - LineLength - xiao.getHeight(), (int) mPosX - LineLength,
                (int) mPosY - LineLength);

        rBig.set((int) mPosX - LineLength, (int) mPosY - LineLength,
                (int) mPosX + LineLength, (int) mPosY + LineLength);

    //  Log.i("r", rBig.left + " " + rBig.top + " " + rBig.right + " " + rBig.bottom);
             invalidate(); 


        break;
    }

    case MotionEvent.ACTION_UP: {

        break;
    }

    case MotionEvent.ACTION_CANCEL: {

        break;
    }

    }
    return true;
}


       }

the pic as :https://i.stack.imgur.com/wxi35.png,the effect have finished by system gallery,but i debug the source ,i failed, as my other question: Imitate crop function of system Gallery
and https://stackoverflow.com/questions/6724218/i-cannot-find-the-initial-value-in-gallery-the-source

  • 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-05-23T23:17:52+00:00Added an answer on May 23, 2026 at 11:17 pm

    I dont know about drawing the Rectangle but, this is how I moved the image

    MainPinchView.java

    public class MainPinchView extends Activity {
    int menuid = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainPinchImageView obj = new MainPinchImageView(this);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.my);
        obj.setImage(bmp, 100, 100);
        setContentView(obj);
      }
    

    }

    MainPinchImageView.java

       public class MainPinchImageView extends ImageView {
    
        private static final String TAG = "Touch";
        // These matrices will be used to move and zoom image
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
    
        int flag = 0;
    
        // We can be in one of these 3 states
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;
    
        // Remember some things for zooming
        PointF start = new PointF();
        PointF mid = new PointF();
        float oldDist = 1f;
    
        Context context;
    
        public MainPinchImageView(Context context) {
            super(context);
            super.setClickable(true);
            this.context = context;
    
            matrix.setTranslate(1f, 1f);
            setImageMatrix(matrix);      // sets the default matrix 
            setScaleType(ScaleType.MATRIX); //Controls how the image should be resized or moved to match the size of this ImageView.
    
            setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // Handle touch events here...
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        savedMatrix.set(matrix); // 
                        start.set(event.getX(), event.getY());
                        Log.d(TAG, "mode=DRAG");
                        mode = DRAG;
                        break;
    
                    case MotionEvent.ACTION_MOVE:
    
                        if (mode == DRAG) {
                            // ...
                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - start.x, event.getY()- start.y);
                        } 
                        break;
                    }
                    setImageMatrix(matrix);
                    return true; // indicate event was handled
                }
            });
        }
    
        public void setImage(Bitmap bm, int displayWidth, int displayHeight) {
            super.setImageBitmap(bm);
    
            int displayheight = (getResources().getDisplayMetrics().heightPixels)/2;
            int displaywidth = (getResources().getDisplayMetrics().widthPixels)/2;
    
            int imgw = displayWidth/2;
            int imgh = displayHeight/2;
    
    
    
    
            // Fit to screen.
            float scale;
            if ((displayHeight / bm.getHeight()) >= (displayWidth / bm.getWidth())) {
                scale = (float) displayWidth / (float) bm.getWidth();
            } else {
                scale = (float) displayHeight / (float) bm.getHeight();
            }
    
            savedMatrix.set(matrix);
            matrix.set(savedMatrix);
            matrix.postScale(scale, scale, mid.x, mid.y);
            setImageMatrix(matrix);
    
            savedMatrix.set(matrix);
            matrix.set(savedMatrix);
            matrix.postTranslate(displaywidth - imgw, displayheight - imgh);
    
            setImageMatrix(matrix);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that I want to add some custom drawing to. I
I want to write a custom view engine that returns custom text (like coma
I want to have a custom view for UIActivityIndicatorView rather than relying on options
I want to cache custom data in an ASP.NET application. I am putting lots
I'm trying to implement a DataGrid in ASP.NET, and want to achieve custom paging
I want to create a custom control in C#. But every time I have
I want to create a custom MSBuild task that changes my .cs files before
I want to build a custom control to reuse in my project which consists
I want to build my own custom log4j (network) adapter to solve my problem
I want to double buffer a custom control which contains buttons. I have tried

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.