[英]How to Draw Portion of Bitmap via Canvas DrawBitmap
Via Object with OnTouchListener,i want to show a portion of bitmap,that are filled on imageview( source imageview ). 使用OnTouchListener通过对象 ,我想显示在imageview( 源图像视图 )上填充的位图的一部分。
So it's not a problem,i'm doing like this: 所以这不是问题,我这样做:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);
where : 其中 :
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). 所以出现的问题,当我的对象(touchlistener),去边境(我有此可能性橙色物体, 走出去的边界与Object.width()/ 2)。
So in this case: 所以在这种情况下:
how can i achieve this result: 我怎样才能达到这个结果:
where result of portion will be: 部分的结果将是:
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 ). 所以对于我的情况 (当OnTouchListener的对象可以走出X轴和Y轴的边界时),我已经制定了Post Conditions (某种规则 )。
Width = Width of imageView ,where i want to show result. 宽度 =宽度ImageView的 ,在这里我想显示的结果。
Height = Height of imageView ,where i want to show result; 高度 = imageView的高度,我想要显示结果;
So via this " Conditions ",i'm drawing portion of bitmap on my MotionEvent.ACTION_MOVE
case. 所以通过这个“ 条件 ”,我正在我的
MotionEvent.ACTION_MOVE
案例中绘制位图的一部分。
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 :). PS对不起我的英文:)。
If your sourceBitmap
is just bitmap set into ImageView
then result is predictable. 如果您的
sourceBitmap
只是位图设置到ImageView
那么结果是可预测的。 To achieve your goal you need: 为了实现您的目标,您需要:
FrameLayout
. FrameLayout
坐标中的橙色方形坐标。 Looks like you already have correct ones. sourceBitmap
you have to use Bitmap
created by your FrameLayout.draw
method. sourceBitmap
您必须使用FrameLayout.draw
方法创建的Bitmap
。 Notice that your orange square have to be not FrameLayout's
child. FrameLayout's
子项。 If you post all of your code somewhere I can check it. 如果您将所有代码发布到某处,我可以检查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.