android:hardwareAccelerated="true"
Canvas canvas = surfaceHolder.lockCanvas(); // no hardware acceleration in this case
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean enableAccelerationFlag = getIntent().getExtras() .getBoolean(MainActivity.HW_ENABLED_KEY); if (enableAccelerationFlag) { // Build target in AndroidManifest.xml and/or Eclipse must be 11 or // higher to compile this code. getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } setContentView(R.layout.single_thread); surfaceView = (SingleThreadSurfaceView) findViewById(R.id.single_thread_view); // Enable calling onDraw() method that is switched off by default. surfaceView.setWillNotDraw(false); }
@SuppressLint("NewApi") private void draw(Canvas canvas) { canvas.drawColor(Color.RED); for (int i = 0; i < IMAGES_PER_FRAME; i++) { int x = (int) (Math.random() * (canvas.getWidth() - bitmap.getWidth())); int y = (int) (Math.random() * (canvas.getHeight() - bitmap.getHeight())); canvas.drawBitmap(bitmap, x, y, null); } canvas.drawText("fps=" + fps, 0, 30, paint); boolean isViewAccelerated = false; boolean isCanvasAccelerated = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // This code must not be executed on a device with API // level less than 11 (Android 2.x, 1.x) isViewAccelerated = surfaceView.isHardwareAccelerated(); isCanvasAccelerated = canvas.isHardwareAccelerated(); } canvas.drawText("isViewAccelerated=" + isViewAccelerated, 0, 60, paint); canvas.drawText("isCanvasAccelerated=" + isCanvasAccelerated, 0, 75, paint); }
private void measureFps() { frameCounter++; long now = SystemClock.uptimeMillis(); long delta = now - lastFpsCalcUptime; if (delta > FPS_CALC_INTERVAL) { fps = frameCounter * FPS_CALC_INTERVAL / delta; frameCounter = 0; lastFpsCalcUptime = now; } }
Platform / Device | Graphic Model | FPS | View sped up? | Canvas accelerated? |
---|---|---|---|---|
Android 2.2 Emulator 800x480 | 1. Main flow, no acceleration | four | not | not |
2. Additional flow, no acceleration | 20 | not | not | |
Android 2.2.2 HTC Desire 800x480 | 1. Main flow, no acceleration | 7 | not | not |
2. Additional flow, no acceleration | 50 | not | not | |
Android 3.2.1 Acer A500 1280x800 | 1. Main stream, default acceleration | 20 | not | not |
2. Main stream, acceleration flag set | 28 | Yes | Yes | |
3. Extra flow, default acceleration | thirty | not | not | |
4. Extra flow, acceleration flag set | thirty | Yes | not | |
Android 4.0.3 HTC Sensation XE 960x540 | 1. Main stream, default acceleration | 26 | not | not |
2. Main stream, acceleration flag set | 39 | Yes | Yes | |
3. Extra flow, default acceleration | 26 | not | not | |
4. Extra flow, acceleration flag set | 26 | Yes | not |
Source: https://habr.com/ru/post/147781/
All Articles