[android] SurfaceView Animation example
canvas = SurfaceHolder.lockCanvas(null);
SurfaceView.onDraw(canvas);
SurfaceHolder.unlockCanvasAndPost(canvas);
SurfaceViewAnimation.java
package com.ojtit.android.surfaceview.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.LinearLayout;
public class SurfaceViewAnimation extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lLayout = new LinearLayout(getApplicationContext());
MyDraw mydraw = new MyDraw(getApplicationContext());
lLayout.addView(mydraw);
setContentView(lLayout);
}
class MyDraw extends SurfaceView implements Callback {
private MyThread myThread;
private Bitmap bmp;
private SurfaceHolder holder;
private Paint paint;
private int x = 50;
private int y = 50;
int newX = 50;
int newY = 50;
int diffX = newX - x;
int diffY = newY - y;
int incrX = -1;
int incrY = -1;
public MyDraw(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint = new Paint();
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
myThread = new MyThread(holder, this);
myThread.setFlag(true);
myThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
newX = (int) event.getX();
newY = (int) event.getY();
diffX = newX - x;
diffY = newY - y;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
diffX = newX - x;
diffY = newY - y;
if(diffX < 0){
incrX = 1;
}
else if(diffX > 0) {
incrX = 0;
}
else {
incrX = -1;
}
if(diffY < 0){
incrY = 1;
}
else if(diffY > 0){
incrY = 0;
}
else {
incrY = -1;
}
if(diffX != 0){
if(incrX == 0){
x++;
}
else if(incrX == 1){
x--;
}
}
if(diffY != 0){
if(incrY == 0){
y++;
}
else if(incrY == 1){
y--;
}
}
canvas.drawBitmap(bmp, x, y, paint);
}
}
class MyThread extends Thread {
boolean flag ;
SurfaceHolder myHolder;
MyDraw myDraw;
public MyThread(SurfaceHolder holder , MyDraw drawMain) {
myHolder = holder;
myDraw = drawMain;
}
public void setFlag (boolean myFlag) {
flag = myFlag;
}
public void run(){
Canvas canvas = null;
while(flag) {
try {
canvas = myHolder.lockCanvas(null);
myDraw.onDraw(canvas);
}
finally{
if(canvas != null){
myHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
SurfaceHolder.unlockCanvasAndPost(canvas);
SurfaceViewAnimation.java
package com.ojtit.android.surfaceview.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.LinearLayout;
public class SurfaceViewAnimation extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lLayout = new LinearLayout(getApplicationContext());
MyDraw mydraw = new MyDraw(getApplicationContext());
lLayout.addView(mydraw);
setContentView(lLayout);
}
class MyDraw extends SurfaceView implements Callback {
private MyThread myThread;
private Bitmap bmp;
private SurfaceHolder holder;
private Paint paint;
private int x = 50;
private int y = 50;
int newX = 50;
int newY = 50;
int diffX = newX - x;
int diffY = newY - y;
int incrX = -1;
int incrY = -1;
public MyDraw(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint = new Paint();
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
myThread = new MyThread(holder, this);
myThread.setFlag(true);
myThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
newX = (int) event.getX();
newY = (int) event.getY();
diffX = newX - x;
diffY = newY - y;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
diffX = newX - x;
diffY = newY - y;
if(diffX < 0){
incrX = 1;
}
else if(diffX > 0) {
incrX = 0;
}
else {
incrX = -1;
}
if(diffY < 0){
incrY = 1;
}
else if(diffY > 0){
incrY = 0;
}
else {
incrY = -1;
}
if(diffX != 0){
if(incrX == 0){
x++;
}
else if(incrX == 1){
x--;
}
}
if(diffY != 0){
if(incrY == 0){
y++;
}
else if(incrY == 1){
y--;
}
}
canvas.drawBitmap(bmp, x, y, paint);
}
}
class MyThread extends Thread {
boolean flag ;
SurfaceHolder myHolder;
MyDraw myDraw;
public MyThread(SurfaceHolder holder , MyDraw drawMain) {
myHolder = holder;
myDraw = drawMain;
}
public void setFlag (boolean myFlag) {
flag = myFlag;
}
public void run(){
Canvas canvas = null;
while(flag) {
try {
canvas = myHolder.lockCanvas(null);
myDraw.onDraw(canvas);
}
finally{
if(canvas != null){
myHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
No comments: