android画布画bitmap,android – 如何通过画布DrawBitmap绘制位图的一部分

自己解决!

这很复杂,但结果相当不错.

开始了:

所以对于我的情况(当OnTouchListener的对象可以离开X和Y轴上的边框时),我已经做了Post Conditions(某种规定).

条件

Width = imageView的宽度,我想显示结果.

Height = imageView的高度,我想显示结果;

左边

> X_Coord< 0&& Y_Coord - 高度/ 2 < 0&& Y_Coord< Bitmap.Height

这是我们的顶级区域.

> X_Coord< 0&& Y_Coord - Height / 2> 0&& Y_Coord< Bitmap.Height

这是我们的中间区.

> X_Coord< 0&& Y_Coord - Height / 2> 0&& Y_Coord> Bitmap.Height

这是我们的底层.

右边

> X_Coord> Bitmap.Height&& Y_Coord – Height / 2> 0&& Y_Coord Bitmap.Height

这是我们的中间区.

> X_Coord> Bitmap.Height&& Y_Coord – 高度/ 2 < 0&& Y_Coord< Bitmap.Height

这是我们的顶级区域.

> X_Coord> Bitmap.Height&& Y_Coord – Height / 2> 0&& Y_Coord> Bitmap.Height

这是我们的底层.

Standart(中间区域,不会左侧或右侧)

> X_Coord – Width / 2> 0&& X_Coord< Bitmap.Width&& Y_Coord - 高度/ 2 < 0&& Y_Coord< Bitmap.Height

这是我们的顶级区域.

> X_Coord – Width / 2> 0&& X_Coord< Bitmap.Width&& Y_Coord - Height / 2> 0&& Y_Coord> Bitmap.Height

这是我们的底层.

> X_Coord – Width / 2> 0&& X_Coord< Bitmap.Width&& Y_Coord - Height / 2> 0&& Y_Coord< Bitmap.Height

这是我们的中间区.

所以通过这个“条件”,我在我的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,mBitmap.getWidth(),mBitmap.getHeight(),paint);

//draw portion of bitmap

canvas.drawBitmap(mBitmap,new Rect(Difference,mBitmap.getHeight()),new Rect(0,mBitmap.getWidth() - Difference,null);

//and that's all!

}

//do the same for other condition....etc

break;

}

return true;

}

请享用!

PS对不起我的eng

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值