clip screen是什么意思

如题所述

public class ClipScreen extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new ClipScreenView(this));
}
}

class ClipScreenView extends SurfaceView implements Callback,Runnable{
private SurfaceHolder mSurfaceHolder;
private Thread mThread;
private Canvas mCanvas;
private Canvas mClipCanvas;//The canvas for screenshot.
private boolean mFlag;
private Paint mPaint;
private Bitmap mBmpIcon;
private Bitmap mBmpClip;
private int mScreenW;
private int mScreenH;

public ClipScreenView(Context context) {
super(context);
mSurfaceHolder=this.getHolder();
mSurfaceHolder.addCallback(this);
mPaint=new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);

setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mScreenW=this.getWidth();
mScreenH=this.getHeight();
mBmpIcon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
mBmpClip=Bitmap.createBitmap(this.getWidth(), this.getHeight(), Config.ARGB_8888);
mClipCanvas=new Canvas(mBmpClip);
mClipCanvas.drawColor(Color.WHITE);
mClipCanvas.drawBitmap(mBmpIcon, 0, 0, mPaint);

mFlag=true;
mThread=new Thread(this,"My Thread");
mThread.start();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mFlag=false;
}

@Override
public void run() {
while (mFlag) {
long start=System.currentTimeMillis();
myDraw();
logic();
long end=System.currentTimeMillis();
if ((end-start)<50) {
try {
Thread.sleep(50-(end-start));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

private void logic() {
// TODO Auto-generated method stub

}

private void myDraw() {
try {
mCanvas=mSurfaceHolder.lockCanvas();
if (mCanvas != null) {
mCanvas.drawColor(Color.BLACK);
mCanvas.drawBitmap(mBmpIcon, 0, 0, mPaint);
mCanvas.drawBitmap(mBmpClip, 50, 50, mPaint);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if (mCanvas != null) {
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
}
}
}

}

这一段绘制截屏的Bitmap的代码是需要理一下的,刚开始以为截屏就是截取屏幕的高度和宽度截取一张图片就可以了mBmpClip=Bitmap.createBitmap(this.getWidth(), this.getHeight(), Config.ARGB_8888);以为这点代码就可以了,但是这个只是创建了和屏幕一样大小的一个Bitmap上面什么信息也没有,还需要我们根据这个Bitmap创建一个截屏的画布mClipCanvas将在onDraw上面正常的情况下绘制的东西,在这个截屏的画布上重新绘制一遍,那么这个空白的画布上便有了和要截取的画面一样的东西了。

之前程序中有对excel、word、ppt、pdf的页面多点触摸进行放大缩小的程序代码,当进行放大缩小的时候实际上onDraw的时候绘制的也是一张图片,而不是正常情况下绘制的是文字或者图片信息。原理和这个是一样的。
温馨提示:答案为网友推荐,仅供参考
相似回答