Android中截图(surfaceView)源码(2)

/**
  * If called, creates a screenshot and saves it as a JPG in the folder
  * "droidnova" on the sdcard.
  */
 //在surfaceView上不停的绘图
 public void doDraw(long elapsed, Canvas canvas)
 {
  canvas.drawColor(Color.BLACK);
  synchronized (mElements)
  {
   for (Element element : mElements)
   {
    element.doDraw(canvas);
   }
  }
  canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: "
    + mElementNumber, 10, 10, mPaint);
 }
 public void saveScreenshot()
 {
  if (ensureSDCardAccess())
  {
   Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
     Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(bitmap);
   //在surfaceView上不停的绘图
   doDraw(1, canvas);
   File file = new File(mScreenshotPath + "/"
     + System.currentTimeMillis() + ".jpg");
   FileOutputStream fos;
   try
   {
    fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
   }
   catch (FileNotFoundException e)
   {
    Log.e("Panel", "FileNotFoundException", e);
   }
   catch (IOException e)
   {
    Log.e("Panel", "IOEception", e);
   }
  }
 }

/**
  * Helper method to ensure that the given path exists. TODO: check external
  * storage state
  */
 private boolean ensureSDCardAccess()
 {
  File file = new File(mScreenshotPath);
  if (file.exists())
  {
   return true;
  }
  else if (file.mkdirs())
  {
   return true;
  }
  return false;
 }
}

ScreenshotActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;

public class ScreenshotActivity extends Activity {
    private Panel mPanel;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mPanel = new Panel(this);
        setContentView(mPanel);
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.screenshot:
                mPanel.saveScreenshot();
                break;
        }
        return true;
    }
}

ViewThread.java

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class ViewThread extends Thread {
    private Panel mPanel;
    private SurfaceHolder mHolder;
    private boolean mRun = false;
    private long mStartTime;
    private long mElapsed;
   
    public ViewThread(Panel panel) {
        mPanel = panel;
        mHolder = mPanel.getHolder();
    }
   
    public void setRunning(boolean run) {
        mRun = run;
    }
   
    @Override
    public void run() {
        Canvas canvas = null;
        mStartTime = System.currentTimeMillis();
        while (mRun) {
            canvas = mHolder.lockCanvas();
            if (canvas != null) {
                mPanel.doDraw(mElapsed, canvas);
                mElapsed = System.currentTimeMillis() - mStartTime;
                mHolder.unlockCanvasAndPost(canvas);
            }
            mStartTime = System.currentTimeMillis();
        }
    }
}

其实这很好理解。由于我的代码写在整个项目中,跌出来比较麻烦,就贴了一个以可以完整通过的工程。

下载地址:

下载在Linux公社的1号FTP服务器里,下载地址:

FTP地址:ftp://www.linuxidc.com

用户名:

密码:

在 2012年LinuxIDC.com\7月\Android中截图(surfaceView)源码

下载方法见

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxgfd.html