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

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

问题描述

我有

I have an
:


  1. FrameLayout红色)

  2. 源ImageView(黑色)

  3. 带有OnTouchListener(橙色)的对象(imageview)

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

Via Object with OnTouchListener,i want to show a portion of bitmap,that are filled on imageview(source imageview).

所以这不是一个问题,我这样做:

Bitmap bt = Bitmap.createBitmap(sourceBitmap, event.getX(),event.getY(),250,250);

So it's not a problem,i'm doing like this:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);

其中


  1. SourceBitmap - 是添加到源ImageView的图片

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

  3. / strong>, 250 - 部分位图的大小(部分)。

  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).

,结果是:

因此,当我的对象(带touchlistener)进入边框时,会出现问题(我已将橙色对象,与Object.width()/ 2)一起输出

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. 部分位图

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


$ b b

我现在尝试的:

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

有任何建议吗?感谢!

推荐答案

自己解决!

<
复杂
,但结果相当不错。

这里我们

case (当对象与OnTouchListener可以走出边界在X和Y轴),我已经邮政条件(某种法规)。

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).

宽度 = imageView 的宽度,其中我要显示结果。

高度 = imageView的高度显示结果;

Width = Width of imageView,where i want to show result.
Height = Height of imageView,where i want to show result;


  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

    这是我们的底部区域

  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




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 > 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 - 宽度/ 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

    这是我们的中区

  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.






所以通过这个条件,我在我的 MotionEvent.ACTION_MOVE case。

让我们看一些例子:


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

享受!

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

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