论坛首页 移动开发技术论坛

安卓实现自定义区域截屏

浏览 6750 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-11-05  

该文章属于原创,转载注明出处:http://www.pm-road.com/index.php/2014/10/29/140/

安卓可以通过Bitmap对象可以实现对整个屏幕的截屏,同时也可以通过该类实现对局部屏幕的截屏,

其中的主要原理就是通过手指对屏幕的onTouch操作:如按下屏幕,在屏幕上移动 ,从屏幕上起来等;

而一般性的截图在截图过程中,选中的屏幕颜色会改变,所以该功能在选取过程中,为了方便看到选择的屏幕大小,我们也会改变屏幕的颜色;

根据思路,如果要在屏幕选择过程中,某个局部显示的屏幕颜色不同,我们需要在当前View里面加入一个View对象,

所以我们应该先写一个类:ScreenShotView.java  该类应该继续View对象,具体代码如下:

package com.titan_geo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class ScreenShotView extends View {
private int x;//手指按下去的x坐标
private int y;//手指按下去的y坐标
private int m;//手指弹起的x坐标
private int n;//手指弹起的的y坐标
private boolean sign;//是否可以进行截屏操作
private Paint paint;//将选取的屏幕画颜色
public ScreenShotView(Context context) {
super(context);
paint = new Paint(Paint.FILTER_BITMAP_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {
if(sign){
paint.setColor(Color.TRANSPARENT);//白
}else{
paint.setColor(Color.RED);//将选取的屏幕设置成红色
paint.setAlpha(80);//透明度80
canvas.drawRect(new Rect(x, y, m, n), paint);
}
super.onDraw(canvas);
}

public void setSeat(int x,int y,int m,int n){
this.x = x;
this.y = y;
this.m = m;
this.n = n;
}

public boolean isSign() {
return sign;
}

public void setSign(boolean sign) {
this.sign = sign;
}

}
 

该文章属于原创,转载注明出处:http://www.pm-road.com/index.php/2014/10/29/140/

然后在要截屏的activity中加入相应的代码:(比如说:MainActivity.java)

// 截图属性
private int screenshot_x;// 绘画开始的横坐标
private int screenshot_y;// 绘画开始的纵坐标
private int screenshot_m;// 绘画结束的横坐标
private int screenshot_n;// 绘画结束的纵坐标
private int screenshot_width;// 绘画的宽度
private int screenshot_height;// 绘画的高度
private Bitmap screenshot_bitmap;// 生成的位图
private ScreenShotView screenshotView;// 绘画选择区域
private String screenUrl;
private Context context;

private ImageView image1;//需要截图的图片

 

在此(MainActivity.java)的oncreate方法中增加如下代码:

//将截屏的view 要放在该activity中

screenshotView= new ScreenShotView(this);

image1 = (ImageView) findViewById(R.id.image1);//需要截屏的部分,可以是任何view对象

//声明一个对话框,提示可以开始截屏了

new AlertDialog.Builder(this)
.setMessage(“选取屏幕开始截屏”)
.setPositiveButton(“确定”,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
screenshotView.setSign(true);//当按下确定按钮时,将截屏中的方法设置为true
if (myView.isSign()) {
screenshotView.setSeat(0, 0, 0, 0);
screenshotView.setSign(false);
} else {
screenshotView.setSign(true);
}
screenshotView.postInvalidate();
}
}).show();

//将截屏的内容放在该activity中
this.addContentView(screenshotView, new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
 

 

然后对image1增加ontouch事件:

image1.setOnTouchListener(new ScreenShotListener());//声明一个内部类的事件

内部类代码如下:

class ScreenShotListener implements OnTouchListener {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
screenshot_x = 0;
screenshot_y = 0;
screenshot_width = 0;
screenshot_height = 0;
screenshot_x = (int) event.getX();
screenshot_y = (int) event.getY();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
screenshot_m = (int) event.getX();
screenshot_n = (int) event.getY();
screenshotView.setSeat(screenshot_x, screenshot_y, screenshot_m,
screenshot_n);
screenshotView.postInvalidate();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getX() > screenshot_x) {
screenshot_width = (int) event.getX() – screenshot_x;
} else {
screenshot_width = (int) (screenshot_x – event.getX());
screenshot_x = (int) event.getX();
}
if (event.getY() > screenshot_y) {
screenshot_height = (int) event.getY() – screenshot_y;
} else {
screenshot_height = (int) (screenshot_y – event.getY());
screenshot_y = (int) event.getY();
}
new AlertDialog.Builder(context)
.setMessage(“确定截图”)
.setPositiveButton(“确定”,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface,
int i) {
//截图完毕之后,会提示是否保存所需要的图片,如果点确定 ,则执行此方法
saveBitmap();//具体代码如下

}
}).setNegativeButton(“取消”, null).show();

}
if (screenshotView.isSign()) {
return false;
} else {
return true;
}
}

}

 

/**
* 保存位图
*
* @param activity
* @return
*/
private void saveBitmap() {

View view = this.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
screenshot_bitmap = view.getDrawingCache();
Rect frame = new Rect();
this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int toHeight = frame.top;
screenshot_bitmap = Bitmap.createBitmap(screenshot_bitmap,
screenshot_x, screenshot_y + 2 * toHeight, screenshot_width,
screenshot_height);
try {
FileOutputStream fout = new FileOutputStream(screenUrl);//这里screenUrl是保存图片的路径 ,在这里需要声明一下,否则会报错
screenshot_bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
if(fout != null){
fout.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
view.setDrawingCacheEnabled(false);

}

 
 

代码完毕,该文章属于原创,转载注明出处:http://www.pm-road.com/index.php/2014/10/29/140/

论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics