12

我有一个
源条件

  1. FrameLayout(红色标注)
  2. 源 ImageView(黑色)
  3. 带有 OnTouchListener(橙色)的对象(图像视图)

通过带有 OnTouchListener 的对象,我想显示在 imageview(源 imageview)上填充的位图的一部分。

所以这不是问题,我正在这样做:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);

其中

  1. SourceBitmap - 是添加到源 ImageView 的图像
  2. event.getX() / event.getY()是一个坐标,我开始绘制位图的一部分
  3. 250 , 250 - 它是部分位图(部分)的大小。

结果是:

第一种情况的结果

所以问题出现了,当我的对象(带有触摸监听器)进入边界时(我已经为橙色对象提供了这种可能性,使用 Object.width()/2 离开边界)。

所以在这种情况下: 我怎样才能达到这个结果: 部分的结果将是:
案例#2

案例 #2 的异常结果

  1. 位图的一部分
  2. 第二部分是框架布局背景的颜色。

我目前尝试的方法:

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

有什么建议么?谢谢!

4

2 回答 2

10

自己解决!
这很复杂,但结果非常好。
我们开始吧
所以对于我的情况(当带有 OnTouchListener 的对象可以超出 X 和 Y 轴的边界时),我已经制定了发布条件(某种规定)。


条件

Width = imageView 的宽度我想在其中显示结果。
高度= imageView 的高度我想在哪里显示结果;

左边

  1. X_Coord < 0 && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    这是我们的顶部区域
  2. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height
    这是我们的中间区域
  3. X_Coord < 0 && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    这是我们的底部区域

右边

  1. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord < Bitmap.Height
    这是我们的中间区域
  2. X_Coord > Bitmap.Height && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    这是我们的顶部区域
  3. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    这是我们的底部区域

Standart(中间区域,不向左侧或右侧)

  1. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 < 0 && Y_Coord < Bitmap.Height
    这是我们的顶部区域
  2. X_Coord - Width / 2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height / 2 > 0 && Y_Coord > Bitmap.Height
    这是我们的底部区域
  3. X_Coord - Width/2 > 0 && X_Coord < Bitmap.Width && Y_Coord - Height/2 >0 && Y_Coord < Bitmap.Height
    这是我们的中间区域

所以通过这个“条件”,我在我的MotionEvent.ACTION_MOVE情况下绘制位图的一部分。
让我们看一些例子:

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

享受!

PS对不起我的英语:)。

于 2015-12-14T15:54:10.043 回答
0

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

  1. 坐标中的橙色正方形坐标FrameLayout。看起来你已经有了正确的。
  2. 正如sourceBitmap您必须使用您的方法Bitmap创建的一样。FrameLayout.draw请注意,您的橙色方块必须不是FrameLayout's孩子。

如果您将所有代码发布在某个地方,我可以检查它。

于 2015-12-09T13:34:56.147 回答