【问题标题】:How to Draw Portion of Bitmap via Canvas DrawBitmap如何通过 Canvas DrawBitmap 绘制位图的一部分
【发布时间】:2016-03-07 06:02:36
【问题描述】:

我有一个

  1. FrameLayout(用红色标记)
  2. 源 ImageView(黑色)
  3. Object(imageview) 和 OnTouchListener(橙色)

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

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

在哪里

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

结果是:

所以问题出现了,当我的对象(带有触摸监听器)进入边界时(我已经为 orange 对象提供了这种可能性,以 out 离开边界对象宽度()/ 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;
}
}

有什么建议吗?谢谢!

【问题讨论】:

  • 对于您的"How to Draw Portion of Bitmap via Canvas DrawBitmap" 问题请参阅:Canvas#drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) - """绘制指定位图,自动缩放/平移以填充目标矩形。如果源矩形不为空,则指定 要绘制的位图子集。"""
  • @pskink 我试过了,但没有走运。不明白为什么。
  • @pskink 这正是我需要的stackoverflow.com/questions/31405082/…。我用 DrawBitmap 尝试了不同的变体,但不明白为什么,得到不好的结果
  • 那么你尝试了什么?你的代码是什么?
  • @pskink 好的,我现在添加我的代码 sn-ps(但需要一点时间,因为需要重新翻译成 java :))。

标签: android canvas bitmap border drawbitmap


【解决方案1】:

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

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

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

【讨论】:

  • u menea k kajdomu obiektu(橙色方块) prikru4en svoi listener(a ne v customnom frame-layout'e) i eti obiekti(orange square - iavliaiutsa imageview,kotrie dobavleni k framelayout -> AddView(orange对象)),te鬼孩子 :)。 PS 代码太多,无法在此处发布。几乎我解决了我的问题,但 DrawCanvas 方法的行为很奇怪。
【解决方案2】:

我自己解决
这是复杂,但结果非常好。
我们开始
所以对于我的案例(当带有 OnTouchListener 的对象可以在 X 和 Y 轴上超出边界时),我已经制定了 后置条件(某种规定)。


条件

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

左侧

  1. X_Coord 0 && Y_Coord - 高度 / 2 Y_Coord Bitmap.Height
    这是我们的热门区域
  2. X_Coord 0 && Y_Coord - 高度 / 2 > 0 && Y_Coord Bitmap.Height
    这是我们的中间区域
  3. X_Coord 0 && Y_Coord - 高度 / 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 Y_Coord Bitmap.Height
    这是我们的热门区域
  3. X_Coord > Bitmap.Height && Y_Coord - Height / 2 > 0 && Y_Coord > 位图.高度
    这是我们的底部区域

标准(中间区域,不向左或向右)

  1. X_Coord - 宽度/2 > 0 && X_Coord Bitmap.Width && Y_Coord - 高度 / 2 Y_Coord Bitmap.Height
    这是我们的热门区域
  2. X_Coord - 宽度/2 > 0 && X_Coord Bitmap.Width && Y_Coord - 高度 / 2 > 0 && Y_Coord > Bitmap.Height
    这是我们的底部区域
  3. X_Coord - 宽度/2 > 0 && X_Coord Bitmap.Width && Y_Coord - 高度 / 2 > 0 && Y_Coord Bitmap.Height
    这是我们的中间区域

所以通过这个“Conditions”,我正在我的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 对不起我的英语 :)。

【讨论】:

  • 如果你想将android框架UI组件的一部分(自定义或内置)放在位图中,你可以使用它的onDraw方法和目标画布。视图将从点 (0, 0) 开始绘制,因此设置正确的转换,您可以获得 UI 的任何部分。
  • @VallyN 我解决了我的问题。现在生病发布我的解决方案。
  • 仅从我的角度来看,保持对内部 View 对象的引用,如果您想要的一切只是它在屏幕上的外观的一部分,那么这是以后遇到问题的可靠方法。为什么不允许组件代码自己绘制所有内容?
  • @VallyN 感谢您的回复。我尝试在 OnDraw 方法中制作自定义视图并绘制部分图像,但它并不那么舒服。因此,对于架构以及我想如何显示位图的一部分,我用另一种方式解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多