简体   繁体   English

如何通过Canvas DrawBitmap绘制位图的一部分

[英]How to Draw Portion of Bitmap via Canvas DrawBitmap

I have an 我有一个
来源条件 :

  1. FrameLayout(marked with red) FrameLayout(标有红色)
  2. Source ImageView(black) 源ImageView(黑色)
  3. Object(imageview) with OnTouchListener (orange) OnTouchListener的对象(imageview)(橙色)

Via Object with OnTouchListener,i want to show a portion of bitmap,that are filled on imageview( source imageview ). 使用OnTouchListener通过对象 ,我想显示在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 SourceBitmap - 是添加到源ImageView的图像
  2. event.getX() / event.getY() is an coord,where i start to draw portion of bitmap event.getX() / event.getY()是一个coord,我开始绘制位图的一部分
  3. 250 , 250 - its an size of portion bitmap(part). 250,250 -其部分位图(部分)的尺寸。

and the result is: 结果是:

第一个案件的结果

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). 所以出现的问题,当我的对象(touchlistener),去边境(我有此可能性橙色物体, 走出去的边界与Object.width()/ 2)。

So in this case: 所以在这种情况下:
案例#2
how can i achieve this result: 我怎样才能达到这个结果:
案例#2的结果除外
where result of portion will be: 部分的结果将是:

  1. Part of bitmap 位图的一部分
  2. second part is color of framelayout background. 第二部分是framelayout背景的颜色。

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! 谢谢!

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 ). 所以对于我的情况 (当OnTouchListener的对象可以走出X轴和Y轴的边界时),我已经制定了Post Conditions (某种规则 )。


Conditions 条件

Width = Width of imageView ,where i want to show result. 宽度 =宽度ImageView的 ,在这里我想显示的结果。
Height = Height of imageView ,where i want to show result; 高度 = imageView的高度,我想要显示结果;

LeftSide 左边

  1. X_Coord < 0 && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height X_Coord < 0 && Y_Coord - 高度/ 2 <0 && Y_Coord < Bitmap.Height
    This is our Top Area . 这是我们的顶级区域
  2. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height X_Coord < 0 && Y_Coord - 高度/ 2 > 0 && Y_Coord < Bitmap.Height
    This is our Middle Area . 这是我们的中部地区
  3. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height X_Coord < 0 && Y_Coord - 高度/ 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 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 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 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) Standart(中间区域,不会左侧或右侧)

  1. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height X_Coord - 宽度/ 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - 高度/ 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 X_Coord - 宽度/ 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - 高度/ 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 X_Coord - 宽度/ 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - 高度/ 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. 所以通过这个“ 条件 ”,我正在我的MotionEvent.ACTION_MOVE案例中绘制位图的一部分。
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! 请享用!

PS Sorry for my eng :). PS对不起我的英文:)。

If your sourceBitmap is just bitmap set into ImageView then result is predictable. 如果您的sourceBitmap只是位图设置到ImageView那么结果是可预测的。 To achieve your goal you need: 为了实现您的目标,您需要:

  1. Orange square coordinates in coordinates of FrameLayout . FrameLayout坐标中的橙色方形坐标。 Looks like you already have correct ones. 看起来你已经有了正确的。
  2. As sourceBitmap you have to use Bitmap created by your FrameLayout.draw method. 作为sourceBitmap您必须使用FrameLayout.draw方法创建的Bitmap Notice that your orange square have to be not FrameLayout's child. 请注意,您的橙色方块必须不是FrameLayout's子项。

If you post all of your code somewhere I can check it. 如果您将所有代码发布到某处,我可以检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM