android 如何用画布绘制位图的局部

6psbrbz9  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(140)

我有一个



1.框架布局(标记为红色)
1.源图像视图(黑色)
1.具有OnTouchListener(橙子)的对象(图像视图)
通过带有OnTouchListener的Object,我想显示在imageview(源imageview)上填充的位图的一部分。
所以这不是一个问题,我这样做:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);

所在地

1.SourceBitmap-是添加到源ImageView的图像
1.event.getX()/**event.getY()**是一个坐标,我从这里开始绘制位图的一部分
1.250250-其大小为部分位图(part)。
结果为:

因此,当我的对象(使用touchlistener)进入边界时,问题就出现了(我已经为橙子对象设置了这种可能性,使用Object.width()/2将其移出边界)。
因此在本例中:

我怎样才能达到这个结果:

其中部分结果将是:
1.部分位图
1.第二部分是框架布局背景颜色。
我目前尝试的是:

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;
}
}

有什么建议吗?谢谢!

dy1byipe

dy1byipe1#

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!

wpx232ag

wpx232ag2#

如果sourceBitmap只是ImageView中的位图集,那么结果是可以预测的。要实现您的目标,您需要:

  1. FrameLayout坐标中的橙子正方形坐标。看起来你已经有了正确的坐标。
    1.作为sourceBitmap,你必须使用FrameLayout.draw方法创建的Bitmap。注意你的橙子正方形必须不是FrameLayout's子对象。
    如果你把你所有的代码贴在某个地方,我可以检查它。

相关问题