Android自定义截屏功能,类似QQ截屏

来源:互联网 发布:农村淘宝招聘司机吗 编辑:程序博客网 时间:2024/05/30 13:41

因为公司业务需求 需要对一个屏幕进行截屏,但自带的截屏功能是远远不够项目的功能需求 ,我们是做一个画板软件 ,需要的像QQ那样截屏之后 ,可以看到我们自定义的工具,有画笔,按钮等等 。android自带的功能非常简单,只需要Intent隐式调用就完全足够了,但他是系统的应用 ,界面固定,无法定制修改。实现方法跟办法有很多种,下面记录下我实现的方法 。我是这样一个思路 ,重写一个View组件 ,在OnDraw里面只负责不画图形(包括半透明的四个矩形,亮框矩形,亮框上的四个小圆点),Ontouch方法是不停的去修改亮框 的坐标点。然后重新绘制 。

效果图:

 
我是把这个图片分解成下面这个图的形状的。我们在onTouch里面就不停地去绘制矩形跟圆点。


具体代码实现主要思路:

1、图片绘制方法:
@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas); //不重写 图片无法出现if(mBitmap!=null){//drawNoLight(canvas);canvas.drawBitmap(mBitmap, iconLeft , iconTop, p_picture) ;//画高亮的边界drawRect(canvas) ;if(isDown)drawCircle(canvas) ;}}

2、图片坐标改动方法:
@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction() ;float x = event.getX() ;float y = event.getY() ;switch (action) {case MotionEvent.ACTION_DOWN:startX = x ;startY = y ;//需要判断是在矩形的外边还是里面(判断是移动还是缩放)if(x>lightRect.left+OFFSET && x<lightRect.right -OFFSET && y>lightRect.top+OFFSET && y<lightRect.bottom -OFFSET){//是移动的状态isMove = true ;isScale = false ;}else if(x<lightRect.left-OFFSET || y<lightRect.top-OFFSET || x>lightRect.right+OFFSET || y>lightRect.bottom+OFFSET){isMove = false ;isScale = false ;}else {isMove = false ;isScale = true ; //缩放 point = getScalePoint(startX , startY);}if(!isScale)isDown = false ;break;case MotionEvent.ACTION_UP :case MotionEvent.ACTION_CANCEL:isDown = true ;break ;case MotionEvent.ACTION_MOVE:if(isMove){ //移动float dx = x - startX ;float dy = y - startY ;moveLightRect(dx , dy) ;startX = x ;startY = y ;isDown = false ;}if(isScale){float dx = x - startX ;float dy = y - startY ;resetLightRect(dx , dy) ;startX = x ;startY = y ;}break ;default:break;}invalidate() ;return true;}

3、图片截取的方法:
public Bitmap getBitmap (){int x = (int)(lightRect.left - iconLeft) ;int y = (int)(lightRect.top - iconTop) ;int w = lightRect.right - lightRect.left ;int h = lightRect.bottom - lightRect.top ;Bitmap bitmap = Bitmap.createBitmap(mBitmap, x, y, w, h) ;return bitmap ;}

PS:这个只是一个View   可以实现图片的截取,这时我们需要加一些自定义的按钮进来,就使用一个布局文件 ,把按钮布置进去 。举一个简单的例子:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >       <com.example.imagedemo.ImageTailor        android:id="@+id/tailor"        android:layout_width="match_parent"        android:layout_height="match_parent"         />        <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_gravity="bottom"        >        <Button        android:id="@+id/complete"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="完成"        />        <Button             android:id="@+id/cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="取消"        />            </LinearLayout>    </FrameLayout>

*:关键类和关键的方法我放在我的资源里,需要的朋友可以下载直接运行 看下效果。也可以看也这个Demo。 主要是ImageTailor.java这个类的实现 。有什么建议请大家提出来 ,共同学习。得意

0 0