使用OnTouchListener通过对象,我想显示在imageview上填充的位图的一部分(源图像视图)。
所以这不是问题,我这样做:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);
其中:
结果是:
所以出现问题,当我的对象(使用touchlistener)进入边界(我已经使橙色对象的这种可能性,去 out 的边界时) Object.width()/ 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;
}
}
有什么建议吗?谢谢!
答案 0 :(得分:8)
由自己解决!
这是 复杂 ,但结果非常好
在这里,我们去:
所以对于我的 case (当OnTouchListener的对象可以超出X轴和Y轴的边界时),我已经发布条件(某种法规强>)。
宽度 = imageView 的宽度,我想显示结果。
高度 = imageView 的高度,我想显示结果;
通过此&#34; 条件&#34;,我在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抱歉我的英文:)。
答案 1 :(得分:0)
如果您的sourceBitmap
只是位图设置为ImageView
,那么结果是可预测的。为了实现您的目标,您需要:
FrameLayout
的橙色方形坐标。看起来你已经有了正确的。sourceBitmap
,您必须使用Bitmap
方法创建的FrameLayout.draw
。请注意,您的橙色方块必须不是FrameLayout's
child。如果您将所有代码发布到某处,我可以查看它。