SurfaceView is a class in Android that provides you a drawing surface. You can embed the SurfaceView in the view hierarchy. The SurfaceView punches a hole in its window to display its surface. You use the getHolder() method of the SurfaceView to access the underlying surface. You must ensure that the surface is valid before you can draw something on it. The surface is valid after it is created and before it is destroyed. You can use the surfaceCreated() and surfaceDestroyed() methods to discover when the surface is created and destroyed.
In the previous post, you learnt how to use the SurfaceView to render 3gp view. In this Android tip, I am going to show you how to circles on the SurfaceView and make them animated.
To have a workable example application that draws and animates circles, you create a new Android project. Then modify the activity_main.xml file to add a SurfaceView as shown below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.example.andtip;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
private SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Paint p;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make a reference to the SurfaceVeiw
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
//get surface holder
surfaceHolder = surfaceView.getHolder();
//set Callback interface to the surface holder
surfaceHolder.addCallback(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
p=new Paint(); //create Paint object
p.setColor(Color.BLUE); //set Paint color
p.setStyle(Paint.Style.STROKE); //set Paint style
p.setStrokeWidth(10); //set Paint stroke with
Thread th=new Thread(){
public void run(){
//draw and animate circles
for(float i=5.0f;i<50.0f;i+=5.0f){
Canvas canvas =surfaceHolder.lockCanvas();
canvas.drawCircle(i, i, 2*i,p);
//delay drawing
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
};
th.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
Posted by: Dara | post date: 07-14-2014 | Subject: Android Apps Development
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.