13

I have an
source condition:

  1. FrameLayout(marked with red)
  2. Source ImageView(black)
  3. Object(imageview) with OnTouchListener (orange)

Via Object with OnTouchListener,i want to show a portion of bitmap,that are filled on imageview(source imageview).

So it's not a problem,i'm doing like this:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);

where:

  1. SourceBitmap - is an image that are added to source ImageView
  2. event.getX() / event.getY() is an coord,where i start to draw portion of bitmap
  3. 250,250 - its an size of portion bitmap(part).

and the result is:

result of first case

So the problems occurs,when my object(with touchlistener),going to the border(i have made this possibility for orange object,to go out of border with Object.width()/2).

So in this case:
case #2
how can i achieve this result:
excepted result of case #2
where result of portion will be:

  1. Part of bitmap
  2. second part is color of framelayout background.

What i tried at current moment:

public boolean onTouch(View view, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:

            //i want to draw bigger portion then corrds
            int CurrentX = (int)view.getX() - (view.getWidth());
            int CurrentY = (int)view.getY() - (view.getHeight());

            //case,when object is going out of border
            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;
}
}

Any suggestions? Thanks!

XTL
  • 1,452
  • 2
  • 17
  • 40
  • 1
    for your `"How to Draw Portion of Bitmap via Canvas DrawBitmap"` question see: `Canvas#drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)` - """Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle. If the source rectangle is not null, it specifies the **subset of the bitmap to draw**.""" – pskink Dec 02 '15 at 20:31
  • @pskink i tried and didn't get luck. Don't understand why. – XTL Dec 03 '15 at 09:22
  • @pskink this is exactly what i need http://stackoverflow.com/questions/31405082/when-touch-an-imageview-i-need-to-display-the-touched-area-in-another-imageview . I Tried different variants with DrawBitmap and don't understand why,getting bad result – XTL Dec 03 '15 at 09:33
  • so what did you try? what is your code? – pskink Dec 03 '15 at 09:48
  • @pskink okay, ill add now my code snippets(but need a little bit time,because need to retranslate to java :) ). – XTL Dec 03 '15 at 09:49
  • see [this](http://pastebin.com/ucHL6UAY) – pskink Dec 03 '15 at 10:27
  • @pskink thanks for the reply. But to be honest,it something another,because i have different imageviews(Dots) and i need to show portion of bitmap,via touch of this object,but not touch to the framelayout :). – XTL Dec 03 '15 at 10:42
  • sorry i have no green idea what you mean – pskink Dec 03 '15 at 10:44
  • can u share me working code for me @XTL.I have tried but i am getting erro like " java.lang.IllegalArgumentException: y + height must be <= bitmap.height()" – gautam Nov 17 '17 at 10:12

2 Answers2

10

Solved by myself!
It was complicated,but result is pretty nice.
Here we go:
So for my case(when object with OnTouchListener can go out of border On X and Y axes),i've made Post Conditions(some kind of regulations).


Conditions

Width = Width of imageView,where i want to show result.
Height = Height of imageView,where i want to show result;

LeftSide

  1. X_Coord < 0 && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    This is our Top Area.
  2. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height
    This is our Middle Area.
  3. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    This is our Bottom Area.

RightSide

  1. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height
    This is our Middle Area.
  2. X_Coord > Bitmap.Height && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    This is our Top Area.
  3. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    This is our Bottom Area.

Standart(Area of Middle,that are not going to Left or Right side)

  1. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    This is our Top Area.
  2. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    This is our Bottom Area.
  3. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height
    This is our Middle Area.

So via this "Conditions",i'm drawing portion of bitmap on my MotionEvent.ACTION_MOVE case.
Let's see some example:

public boolean onTouch(View view, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:

        int Width = ResultImgView.getWidth();
        int Height = ResultImgView.getHeight();
        //paint for our Red background
        Paint paint = new Paint();
        paint.setStyle( Style.FILL  );
        paint.setColor( Color.RED );
        Bitmap mBitmap = null;
        Canvas canvas = null;

        //Our Condition 
        if(view.getX() - Width / 2 >= SourceBitmap.getWidth() 
            && view.getY() - Height / 2 > 0 && view.getY() + Height / 2 <  SourceBitmap.getHeight())
        {
            //Nice,we entered here. Seems that we're now located at RightSide at Middle position
            //So let's draw part of bitmap.
            //our margin for X coords
            int Difference = (int)((view.getX() - Width / 2 ) - SourceBitmap.getWidth();
            //dont forget to put margin
            //BTW we're now took portion of bitmap
            mBitmap = Bitmap.createBitmap(SourceBitmap, ((int)view.getX() - Width / 2) - Difference, (int)view.getY() - Height / 2, Width,Height);
            canvas = new Canvas(mBitmap);
            //draw rect
            canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(),paint);
            //draw portion of bitmap
            canvas.drawBitmap(mBitmap,new Rect(Difference, 0,mBitmap.getWidth(),mBitmap.getHeight()),new Rect(0,0,mBitmap.getWidth() - Difference,mBitmap.getHeight()),null);
            //and that's all!
        }

        //do the same for other  condition....etc
        break;
}



   return true;
}

Enjoy!

XTL
  • 1,452
  • 2
  • 17
  • 40
  • If you want to put some portion of android framework UI component (custom or built-in) in bitmap you can use its `onDraw` method with target canvas. View will be drawn starting at point (0, 0) so set right transformation and you can get any portion of UI. – weaknespase Dec 14 '15 at 16:15
  • @VallyN i solved my problem. Now ill post my solution. – XTL Dec 14 '15 at 16:17
  • just from my point of view keeping reference to internal View objects, if everything you want is just a part of how it looks on screen is a sure-proof method of getting problems later. Why not allowing component code to draw everything by itself? – weaknespase Dec 14 '15 at 16:20
  • @VallyN Thanks for the reply. I tried to make custom View and draw part of image in OnDraw Method,but it's not so comfortably. So with architecture and how i want to show portion of bitmap,i solved this with another way. – XTL Dec 14 '15 at 16:27
0

If your sourceBitmap is just bitmap set into ImageView then result is predictable. To achieve your goal you need:

  1. Orange square coordinates in coordinates of FrameLayout. Looks like you already have correct ones.
  2. As sourceBitmap you have to use Bitmap created by your FrameLayout.draw method. Notice that your orange square have to be not FrameLayout's child.

If you post all of your code somewhere I can check it.

Lingviston
  • 5,479
  • 5
  • 35
  • 67
  • u menea k kajdomu obiektu(Orange square) prikru4en svoi listener(a ne v customnom frame-layout'e) i eti obiekti(orange square - iavliaiutsa imageview,kotorie dobavleni k framelayout -> AddView(orange obiekt)),t.e. oni childi :). PS too much code to post here. Almost i resolved my problem,but i have strange behaviour with DrawCanvas method. – XTL Dec 09 '15 at 16:20