Saturday, 7 July 2012

Enable Hardware Acceleration for Android 3.0 or later devices

Android 3.0 (Honeycomb) introduced Hardware Acceleration. To enable Hardware Acceleration for your app, edit AndroidManifest.xml to insert the attribute android:hardwareAccelerated="true" in <application /> tag.

To check if any view/canvas is hardware accelerated, simple call:
- View.isHardwareAccelerated(), or
- Canvas.isHardwareAccelerated()

Hardware Acceleration

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidhardwareacceleration"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/hw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Touch the big icon to check isHardwareAccelerated"/>

</RelativeLayout>


package com.example.androidhardwareacceleration;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView hw;
ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView)findViewById(R.id.iv);
hw = (TextView)findViewById(R.id.hw);

imageView.setImageResource(R.drawable.ic_launcher);

imageView.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
boolean isHWAccelerated = imageView.isHardwareAccelerated();
hw.setText("isHardwareAccelerated: " + String.valueOf(isHWAccelerated));

}});

}

}


Related:
- Enable Hardware Acceleration using Java code


No comments:

Post a Comment