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

原文地址:http://blog.csdn.net/otcyan/article/details/40737235