Android Texture View



Android Texture


  • Dynamic TextureView Creation in Android.



Dynamic TextureView Creation in Android.


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

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

    <TextView
        android:id="@+id/textView1"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Dynamic TextureView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Create" />

</RelativeLayout>



Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{
   
    RelativeLayout rl;
    Button b;
    TextureView texture;
    Camera c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        rl = (RelativeLayout) findViewById(R.id.rl);
        b = (Button) findViewById(R.id.button1);
               
        texture = new TextureView(MainActivity.this);
       
        b.setOnClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
           
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
           
            params.topMargin = 250;
           
            texture.setLayoutParams(params);
           
            rl.addView(texture);
           
            Toast.makeText(getBaseContext(), "TextureView Created",
                    Toast.LENGTH_SHORT).show();
        }
    });       
    }

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

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
        int height) {
    // TODO Auto-generated method stub
       
    c = Camera.open();
    Camera.Size size = c.getParameters().getPreviewSize();
   
    texture.setLayoutParams(new FrameLayout.LayoutParams(
            size.width, size.height, Gravity.CENTER));
   
    try {
        c.setPreviewTexture(surface);
    } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
    }
   
    c.startPreview();
   
    texture.setAlpha(0.5f);
    texture.setRotation(45.0f);   
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    // TODO Auto-generated method stub
       
    c.stopPreview();
    c.release();
   
    return true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
        int height) {
    // TODO Auto-generated method stub
       
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    // TODO Auto-generated method stub
       
    }
}


Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

</manifest>

Step 5 : Our output will be like this :