0

I am trying to draw a scalable rectangle and can also change its position ,what I want is to use these square/rectangle coordinate pixels .Any libraries or any suggestions will be extremely useful .I have used this https://stackoverflow.com/a/17807469/6625633 .But failed to do so.

Luther
  • 573
  • 1
  • 6
  • 16
  • [This](https://android-arsenal.com/details/1/3487) can help you do. I know it's for cropping the image but you can implement its free movable view and use it as you want instead of cropping or with some presets, use [this](https://android-arsenal.com/details/1/6283). – Lalit Fauzdar Mar 07 '18 at 06:21
  • I have tried this , but making changes to such a large piece of code is practically impossible for me. – Luther Mar 07 '18 at 07:38

1 Answers1

1

You can do it using canvas:

public class YourView extends View {
   float rectX = 0, rectY = 0, rectWidth = 128, rectHeight = 128;
   int color = 0xFFff00ff, viewWidth, viewHeight;
   Paint p;

   public YourView(Context con) {
      super(con);
      p = new Paint();
      viewWidth = getWindowManager().getDefaultDisplay().getWidth();
      viewHeight = getWindowManager().getDefaultDisplay().getHeight(); // fullscreen view
   }

   @Override
   public void onMeasure(int w, int h) {
      setMeasuredDimension(viewWidth, viewHeight); // view size
   }

   @Override
   public void onDraw(Canvas canvas) {
      p.setColor(color); // rect color
      canvas.drawRect(rectX, rectY, rectX+rectWidth, rectY+rectHeight, p);
      // drawRect(x, y, endX, endY, Paint p)
   }
}

For example:

YourView yv = new YourView(this);
yv.rectX = 64;
yv.rectY = 64;
yv.invalidate(); // update view
// setContentView(yv); // if you want to display only your custom View

You can also move the View around the screen:

Button btn = new Button(this);
btn.post(new Runnable() {
   @Override
   public void run() {
      btn.setX(x);
      btn.setY(y);
   }
});
congard
  • 945
  • 2
  • 10
  • 28
  • I am using canvas but the problem is when i use setcontent view it gives an error saying Binary XML file line #0: awa.com.imageprocessing.DrawView cannot be cast to android.view.ViewGroup – Luther Mar 07 '18 at 07:45
  • I cant use 2 views at same time – Luther Mar 07 '18 at 07:47
  • @SandeepDhami If you want to display only your square/rect, you can not use xml at all. Only `setContentView(yv)` in onCreate – congard Mar 07 '18 at 07:50
  • no i want an image and above this image i want this rectanagle to get pixels point of the image that is where the rect edges are coinciding with image – Luther Mar 07 '18 at 07:54
  • @SandeepDhami In this case, you can use the second method I gave, ie specify coordinates such as a button. Or you can draw the same picture on the canvas, and after that draw a rectangle where you want – congard Mar 07 '18 at 08:14
  • Yeah I guess drawing same picture on canvas will help me, let me give it a try – Luther Mar 07 '18 at 08:17
  • the code which u have provided is covering the image – Luther Mar 07 '18 at 08:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166381/discussion-between-sandeep-dhami-and-db-congard). – Luther Mar 07 '18 at 08:40