如何通过画布绘制DrawBitmap位图的一部分 [英] How to Draw Portion of Bitmap via Canvas DrawBitmap

查看:214
本文介绍了如何通过画布绘制DrawBitmap位图的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结果
 


  1. 的FrameLayout(用红色标出)

  2. 来源ImageView的(黑色)

  3. 对象(ImageView的)与OnTouchListener(橙色)

通过对象与OnTouchListener,我想显示位图的一部分,这是填补ImageView的(源的ImageView )。

因此​​,这不是一个问题,我做这样的:结果
位图BT = Bitmap.createBitmap(sourceBitmap中,event.getX(),event.getY(),250,250);

其中,


  1. sourceBitmap中 - 是要被添加到源ImageView的图片

  2. event.getX() / event.getY()是一个坐标,在那里我开始绘制位图的一部分

  3. 250 250 - 它的部分位图(部分)的大小。

和其结果是:

所以出现的问题,当我的对象(touchlistener),去边境(我有此可能性的橙色的对象,去的退出边界与Object.width()/ 2)。

因此​​,在这种情况下:结果
结果
我如何能实现这样的结果:结果
结果
其中,部的结果将是:


  1. 位图的一部分

  2. 第二部分是背景的FrameLayout的颜色。

我试图在目前的时刻:

 公共布尔onTouch(查看视图,MotionEvent事件){    开关(event.getAction()){
        案例MotionEvent.ACTION_MOVE:            //我想corrds画更大一部分则
            INT CurrentX =(int)的view.getX() - (view.getWidth());
            INT CurrentY =(int)的view.getY() - (view.getHeight());            //情况下,当对象是走出去边境
            如果(CurrentX&下; = 0)
            {
                涂料粉刷=新的油漆();
                paint.setStyle(Style.FILL);
                paint.setColor(Color.RED);                mBitmap = Bitmap.CreateBitmap(sourceBitmap中,(int)的view.getX()+ Math.abs(CurrentX),(INT)view.getY(),250,250);
                帆布帆布=新的Canvas(mBitmap);
                canvas.drawBitmap(mBitmap,新的矩形((INT)view.getX()+ Math.abs(CurrentX),(INT)view.getY(),250 Math.abs(CurrentX),250),新RectF(数学。ABS(CurrentX),0,250,250),涂料);
            }
            打破;
    }    返回true;
}
}

有什么建议?谢谢!


解决方案

!结果解决
这是的复杂,但结果是pretty不错。结果
在这里,我们的:结果
因此,对于我的情况(适用时OnTouchListener对象可以出去边界在X和Y轴),我所做的后期条件(某种法规)。


条件

宽度 =宽度的的ImageView 的,在这里我想显示的结果。结果
身高 =身高的ImageView 的,在这里我想说明的结果;

LeftSide


  1. X_Coord < 0 &放大器;&安培; Y_Coord - 身高/ 2 < 0安培;&安培; 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 结果
    这是我们的底部区域


RightSide


  1. X_Coord > Bitmap.Height &放大器;&安培; Y_Coord - 身高/ 2 > 0安培;&安培; Y_Coord < Bitmap.Height 结果
    这是我们的中东地区即可。

  2. X_Coord > Bitmap.Height &放大器;&安培; Y_Coord - 身高/ 2 < 0安培;&安培; Y_Coord < Bitmap.Height 结果
    这是我们的热门区

  3. X_Coord > Bitmap.Height &放大器;&安培; Y_Coord - 身高/ 2 > 0安培;&安培; Y_Coord > Bitmap.Height 结果
    这是我们的底部区域


斯坦达特(中面积,即不会左侧或右侧)


  1. X_Coord - 宽度/ 2 > 0 &放大器;&安培; X_Coord < Bitmap.Width &放大器;&安培; Y_Coord - 身高/ 2 < 0安培;&安培; 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 结果
    这是我们的中东地区


所以,通过这种条件,我画我的 MotionEvent.ACTION_MOVE 情况下位图的一部分。结果
让我们来看看一些例子:

 公共布尔onTouch(查看视图,MotionEvent事件){    开关(event.getAction()){
        案例MotionEvent.ACTION_MOVE:        INT宽度= ResultImgView.getWidth();
        INT高度= ResultImgView.getHeight();
        //油漆为我们的红色背景
        涂料粉刷=新的油漆();
        paint.setStyle(Style.FILL);
        paint.setColor(Color.RED);
        位图mBitmap = NULL;
        帆布帆布= NULL;        //我们的条件
        如果(view.getX() - 宽/ 2 - = SourceBitmap.getWidth()
            &功放;&安培; view.getY() - 身高/ 2 - ; 0安培;&安培; view.getY()+身高/ 2版; SourceBitmap.getHeight())
        {
            //不错,我们在此输入。看来,我们现在位于RightSide在中间位置
            //因此,让我们画位图的一部分。
            //我们对于x COORDS保证金
            INT差=(INT)((view.getX() - 宽/ 2) - SourceBitmap.getWidth();
            //不要忘记把保证金
            // BTW我们现在把位图的一部分
            mBitmap = Bitmap.createBitmap(sourceBitmap中,((INT)view.getX() - 宽/ 2) - 差(INT)view.getY() - 身高/ 2,宽度,高度);
            帆布=新的Canvas(mBitmap);
            //绘制矩形
            canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(),漆);
            //画出位图的一部分
            canvas.drawBitmap(mBitmap,新的矩形(差异,0,mBitmap.getWidth(),mBitmap.getHeight()),新的矩形(0,0,mBitmap.getWidth() - 差,mBitmap.getHeight()),空) ;
            //就这样!
        }        //其他情况做同样的....等
        打破;
}   返回true;
}

尽情享受吧!

PS对不起,我的工程:)

I have an
:

  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:

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:

how can i achieve this result:

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!

解决方案

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!

PS Sorry for my eng :).

这篇关于如何通过画布绘制DrawBitmap位图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆