Hello everyone, there is the following:
source condition :

  1. FrameLayout (displayed in red)
  2. Original ImageView (black)
  3. A quadview object (imageview) with a bolted OnTouchListener (orange), it is created inside FrameLayout.

Through the Object with OnTouchListener "th, I want to show the" part "of Bitmap, which is installed in the original ImageView.

In general, I do something like this:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);

where :

  1. SourceBitmap - image that is set to the original imageview
  2. event.getX () / event.getY () coordinates (a quadric object) from which I start to draw Bitmap's “ part ”.
  3. 250 , 250 - the size of the "piece" Bitmap.

and the result:

result of first case

In general, a problem appears when an object (with the OnTouchListener screwed in), goes beyond the boundary of the original ImageView (there is such an opportunity, that is, half the length, can go abroad).

Those. in this case :
case # 2
I expect this result:
excepted result of case # 2
more specifically:

  1. Part of bitmap.
  2. The second part is the " emptiness ", I mean the color of the Framelayout (background).

What I tried at the moment:

 public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: //я хочу получить большую порцию,чем текущие координаты int CurrentX = (int)view.getX() - (view.getWidth()); int CurrentY = (int)view.getY() - (view.getHeight()); //случай когда изображение ушло за границы if(CurrentX <= 0) { Paint paint = new Paint(); paint.setStyle( Style.FILL ); paint.setColor( Color.RED ); mBitmap = Bitmap.CreateBitmap(sourceBitmap,(int)view.getX() + Math.abs(CurrentX),(int)view.getY(),250,250); Canvas canvas = new Canvas(mBitmap); canvas.drawBitmap(mBitmap,new Rect((int)view.getX()+Math.abs(CurrentX), (int)view.getY(),250-Math.abs(CurrentX),250),new RectF(Math.abs(CurrentX), 0, 250,250),paint); } break; } return true; } } 

And the problem is that the result is not what I expect.
And just as you can get the "result" (rectangle), without the original image, because because of the canvas.drawBitmap, the result is drawn over the original image.

    1 answer 1

    In my case, namely, when an object with a bolted OnTouchListener that can go beyond the boundaries of the X, Y axis, relative to the original ImageView , I made special rules (post conditions).

    Conditions:

    Width = The width of the ImageView in which I will display the result.
    Height = Height of the ImageView in which I will display the result.

    Left side:

    1. X_Coord < 0 && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height - this will be the top.
    2. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height - this will be the middle part.
    3. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height - this will be the bottom part.

    Right side:

    1. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height - this will be the middle part.
    2. X_Coord > Bitmap.Height && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height - this will be the top.
    3. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height - this will be the bottom part.

    Standard (middle part that does not reach the edges (right / left side) of the original ImageView ):

    1. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height - this will be the top.
    2. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height - this will be the bottom part.
    3. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height - this will be the middle part.

    Through these "Fasting conditions", I will draw a piece (portion) of Bitmap in the case of MotionEvent.ACTION_MOVE .

    Let's look at an example:

     public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: int Width = ResultImgView.getWidth(); int Height = ResultImgView.getHeight(); //paint для красного бэкграунда Paint paint = new Paint(); paint.setStyle( Style.FILL ); paint.setColor( Color.RED ); Bitmap mBitmap = null; Canvas canvas = null; //Наше условие if(view.getX() - Width / 2 >= SourceBitmap.getWidth() && view.getY() - Height / 2 > 0 && view.getY() + Height / 2 < SourceBitmap.getHeight()) { //Отлично! Мы зашли сюда,а это значит,что в данный момент времени, мы находимся в **Правой Стороне** и относимся к "Средней" Площади. //Теперь можно начать рисовать порцию битмапа. //Считаем наш отступ по Х int Difference = (int)((view.getX() - Width / 2 ) - SourceBitmap.getWidth(); //не забываем установить этот "отступ" mBitmap = Bitmap.createBitmap(SourceBitmap, ((int)view.getX() - Width / 2) - Difference, (int)view.getY() - Height / 2, Width,Height); canvas = new Canvas(mBitmap); //рисуем наш бэкграунд в виде прямоугольника canvas.drawRect(0,0,mBitmap.Width,mBitmap.getHeight(),paint); //рисуем порцию битмапа canvas.drawBitmap(mBitmap,new Rect(Difference, 0,mBitmap.getWidth(),mBitmap.getHeight()),new Rect(0,0,mBitmap.getWidth() - Difference,mBitmap.getHeight()),null); //на этом всем! } //проделать такого же плана работу и для других условий. break; } return true; } 
    • one
      Everyone is very glad that you did something yourself, but the policy of this resource does not at all approve of such "revelations", as well as an inappropriate amount of exclamation marks, bold fonts, and so on. irrelevant to the solution of the issue. - pavlofff