XTL*_*XTL 11 android canvas border bitmap drawbitmap
使用OnTouchListener 通过对象,我想显示在imageview(源图像视图)上填充的位图的一部分.
所以这不是问题,我这样做:
Bitmap bt = Bitmap.createBitmap(sourceBitmap,event.getX(),event.getY(),250,250);
其中:
结果是:
所以出现的问题,当我的对象(touchlistener),去边境(我有此可能性橙色物体,去了边框与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;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢!
自己解决了!
这很复杂,但结果非常好.
在这里我们去:
所以对于我的情况(当OnTouchListener的对象可以走出X轴和Y轴的边界时),我已经制定了Post Conditions(某种规则).
宽度 =宽度ImageView的,在这里我想显示的结果.
高度 = imageView的高度,我想要显示结果;
所以通过这个" 条件 ",我在我的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;
}
Run Code Online (Sandbox Code Playgroud)
请享用!
PS对不起我的英文:).
| 归档时间: |
|
| 查看次数: |
1182 次 |
| 最近记录: |