2012年2月16日木曜日

SurfaceViewを使ったアニメーション


package com.sample.surface.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest01Activity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AnimationSurfaceView surfaceView;
surfaceView = new AnimationSurfaceView(this);
setContentView(surfaceView);

}


/**
*
* SurfaceViewクラス
* アニメーションを実行する。
*/
public class AnimationSurfaceView extends SurfaceView
implements Runnable, SurfaceHolder.Callback {

static final int SIZE_X = 30;
static final int SIZE_Y = 30;

int positionX = 0;
int positionY = 0;

int screenWidth;
int screenHeight;

SurfaceHolder surfaceHolder;
Thread thread;

public AnimationSurfaceView(Context context) {
super(context);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
}

@Override
public void run() {

Canvas canvas = null;
Paint paintBlue  = new Paint();
Paint paintWhite = new Paint();

paintWhite.setStyle(Style.FILL);
paintWhite.setColor(Color.WHITE);

paintBlue.setStyle(Style.FILL);
paintBlue.setColor(Color.BLUE);


while(thread != null){

try{
canvas = surfaceHolder.lockCanvas();

canvas.drawRect(//背景
0,
0,
screenWidth, screenHeight,
paintWhite);

canvas.drawRect(//平行移動する四角形
positionX,
positionY,
positionX + SIZE_X,
positionY + SIZE_Y,
paintBlue);

positionX += 1;//x移動
positionY += 1;//y移動

surfaceHolder.unlockCanvasAndPost(canvas);

}
catch(Exception e){}
}
}

@Override
public void surfaceChanged(
SurfaceHolder holder,
int format,
int width,
int height) {
screenWidth = width;
screenHeight = height;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new Thread(this);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread = null;
}
}
}

Androidで簡易アニメーション


OpenGLやSurfaceViewを使わずに、簡単にアニメーションさせるにはTranslateAnimationやRotateAnimationを使います。
ここから下はサンプルソースです。


package com.sample.animation;//ここは各自変更して下さい

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class AnimationTestActivity extends Activity {//ここも各自変更して下さい
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        //背景になる緑色のレイアウト
        LinearLayout greenLinearLayout = new LinearLayout(this);
        //緑色に設定
        greenLinearLayout.setBackgroundColor(Color.GREEN);
     

        //平行移動するレイアウト
        LinearLayout redLinearLayout = new LinearLayout(this);
        //赤色に設定
        redLinearLayout.setBackgroundColor(Color.RED);
        //横幅、縦幅
        redLinearLayout.setLayoutParams(new LayoutParams(300, 300));


        //アニメーションを設定
        TranslateAnimation redAnimation =
            new TranslateAnimation(100, 100, 0, 330);//x始点、x終点、y始点、y終点
        //アニメーションする時間 4000ミリ秒 = 4秒間アニメーションする
        redAnimation.setDuration(4000);
        //レイアウトにアニメーションを設定
        redLinearLayout.setAnimation(redAnimation);

     
        //回転するビュー
        TextView blueView = new TextView(this);
        //横幅、縦幅
        blueView.setLayoutParams(new LayoutParams(150, 150));
        //青色に設定
        blueView.setBackgroundColor(Color.BLUE);
        blueView.setText("回転移動");
     
        //開始角度、終了角度、中心点x、中心点y
        RotateAnimation blueAnimation = new RotateAnimation(0, 360, 150, 150);
        //アニメーションする時間 4000ミリ秒 = 4秒間アニメーションする
        blueAnimation.setDuration(4000);
        //ビューにアニメーションを設定
        blueView.setAnimation(blueAnimation);

        //ビューの親子関係を設定。
        //アクティビティに親の緑色レイアウトを設定
        setContentView(greenLinearLayout);
        //緑色レイアウトに子の赤色レイアウトを設定
        greenLinearLayout.addView(redLinearLayout);
        //赤色レイアウトに孫青色ビューを設定
        redLinearLayout.addView(blueView);
    }
}




2012年2月12日日曜日

Squirrelの組み込み Mac編 その2

Squirrelの計算結果をCに渡すのを試します。
Squirrel側では、returnではなくprint()を使って渡します。
他のやり方があって、returnでも渡せるかもしれないが、今回はprint()で渡します。


その1で使った test.nut の中身を下記のように変えます。


function foo(i, f, s)
{
    print(123);
    print(0.987);
    print("abc");
}

その1で使った minimal.c の printfunc 関数の中身を下記のように変えます。


    switch (sq_gettype(v, -2)) {
        case OT_INTEGER:
            SQInteger  ret;
            sq_getinteger(v, -2, &ret);
            sq_pop(v, 1);
            printf(" int = %d ", (int)ret);
            break;
        case OT_FLOAT:
            SQFloat  fret;
            sq_getfloat(v, -2, &fret);
            sq_pop(v, 1);            
            printf(" float = %f ", fret);
            break;
        case OT_STRING:
            const SQChar  *cret;
            sq_getstring(v, -2, &cret);
            sq_pop(v, 1);
            printf(" string = %s ", cret);
            break;    
    }


これで実行してやると

 int = 123  float = 0.987000  string = abc 

と表示されると思います。
Squirrel側でprint()した値がC側に渡されたことになります。

2012年2月11日土曜日

Squirrelの組み込み Mac編 その1

MacでSquirrelを試してみました。

Squirrel 3.0.2 をダウンロード http://squirrel-lang.org/#download
コンパイルして、libフォルダの中にできた libsqstdlib.a と libsquirrel.a を使います。

試すのはSQUIRRELフォルダの中のetcフォルダの中にあるminimal.c と test.nut です。
SQWorkという作業フォルダを作ります。その中にlibフォルダを作り、さっきのライブラリを2つ入れておきます。minimal.c をコンパイルするには、ターミナルで



g++ minimal.c -L/SQWork/lib -lsqstdlib -L/SQWork/lib -lsquirrel -o sample




と入力します。
sampleができあがれば、ターミナルから実行してやると




Called foo(), i=1, f=2.5, s='teststring'




という文字列が表示されると思います。
これでSquirrelの組み込みのHelloWorldは終了です。

2012年1月23日月曜日

デザイン変更

blogのデザインをシンプルなものに変更しました。あと、カテゴリを見れるようにしました。

2011年12月14日水曜日

最近

Javaでxファイルのパーサー制作しているんですが、正直キツい!