2010-11-20

自作Viewをレイアウトに追加

seesaaから、移動。

自作Viewの貼り付けについては、ここを参考に。
で、本に乗ってたのを改造したのを。長いですが。

SurfaceViewなんですが、ここを見ると一手間要るようです。
が、なぜか動きます。Viewと同じやり方で。何かの参考になればいいですが。

 とりあえず、xmlから。色とか文字はstring.xmlで管理するようですが
それは、まあ、そのうちに。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        >
           <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#00c608"
            android:textSize="12sp"
            android:text="適当にボタンを押してください\n数字はフォントサイズです"
            android:layout_weight="2"
        />
    </LinearLayout>
    <net.asasvata.SampleSurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
    />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="start"
        />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="stop"
        />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        >
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1"
            >
            <RadioButton
                android:id="@+id/radioButton0"
                android:text="H"
                android:textColor="#00c608"
               />
             <RadioButton
                android:id="@+id/radioButton1"
                android:text="M"
                android:textColor="#00c608"
               />
               <RadioButton
                android:id="@+id/radioButton2"
                android:text="L"
                android:textColor="#00c608"
               />
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

パッケージ名とファイル名を指定。あとは同じようなやり方。
次は、SurfaceViewを。

package net.asasvata;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet; //これが重要みたいです。
import android.view.*;
//サーフェイスビューの利用
public class SampleSurfaceView extends SurfaceView     
           implements   SurfaceHolder.Callback,Runnable {

 private SurfaceHolder holder;//サーフェイスホルダー
    private Thread thread;        //スレッド

    private int px = 0;//X座標
    private int py = 5;//Y座標
    private int vx = 0;//X速度
    private int vy = 0;//Y座標
    //コンストラクタ
    public SampleSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //サーフェイスホルダーの生成
        holder = getHolder();
        holder.addCallback(this);
        holder.setFixedSize(getWidth(), getHeight());
    }
    //フィールドをprivateにしてみたので、ゲッター&セッター
    public int getVx() {
        return vx;
    }
    public int getVy() {
        return vy;
    }
    public void setVx(int vx) {
        this.vx = vx;
    }
    public void setVy(int vy) {
        this.vy = vy;
    }
    //サーフェイスの生成
    public void surfaceCreated(SurfaceHolder holder) {
        
        //スレッドの開始
        thread = new Thread(this);
        thread.start();
    }
    //サーフェイスの変更
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    }
    //サーフェイスの破棄
    public void surfaceDestroyed(SurfaceHolder holder) {
        thread = null;
    }
    //スレッドの処理
    public void run() {
        int i = 5;

        Paint paint = new Paint();
        paint.setTextSize(i);
        Canvas canvas;
        int j=0, k=0, l = 0;

        while(thread != null) {
            //ダブルバッファリング
            String str = ""+i;
            paint.setTextSize(i);
            paint.setColor(Color.rgb(255-j, 255-k, 255-l));
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.argb(255, j, k, l));
            canvas.drawText(str, px, py, paint);
            holder.unlockCanvasAndPost(canvas);
            //移動
            if (vx > 0) {
                //文字サイズに合わせて跳ね返る位置を調整
                if (getWidth()-paint.measureText(str)< px){
                    vx = -vx;
                    i += 5; j += 10; k += 20; l += 30;
                    if (i > 101) i = 5;
                    if (j > 255) j = 0;
                    if (k > 255) k = 0;
                    if (l > 255) l = 0;
                }
            } else {
                if (px < 0) {
                    vx = -vx;
                    i += 5; j += 10; k += 20; l += 30;
                    if (i > 101) i = 5;
                    if (j > 255) j = 0;
                    if (k > 255) k = 0;
                    if (l > 255) l = 0;
                }
            }
            if (vy < 0) {
                //フォントサイズに合わせて跳ね返る位置を調整・・・のはずなんですが。
                if (py < i) vy = -vy;
            } else {
                if (getHeight()< py) vy = -vy;
            }
            px += vx;
            py += vy;
            //スリープ
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
        }
    }
}

次は、Activity。

 package net.asasvata;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;

//サーフェイスビュー、テキストビュー、ボタン、ラジオボタンを使います
public class SampleActivity extends Activity implements View.OnClickListener {
   
    SampleSurfaceView sv;
    private Button button1, button2;
    private RadioGroup radioGroup;
  
    int num = 1;//ボタンを押したときの状態保持のために使います
    int sp = 0;//移動速度を変更するために使います
  
    //アプリの初期化
    @Override
    public void onCreate(Bundle icicle) {      
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
      
        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
      
        sv = (SampleSurfaceView)findViewById(R.id.surfaceView);
      
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
      
        button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(this);
      
        radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.check(R.id.radioButton1);
    }
  
    //ボタンとラジオボタンがクリックされたときの処理
    public void onClick(View view) {
      
        //ラジオボタンが押されたときの処理
        switch (radioGroup.getCheckedRadioButtonId()) {
        //ラジオボタンのidによって処理を変えます
        case R.id.radioButton0:
            sp = 14;
            //ラジオボタンが押されたときの速度方向を維持します
            keepSpDir(sv);
            break;
        case R.id.radioButton1:
            sp = 7;
            keepSpDir(sv);
            break;
        case R.id.radioButton2:
            sp = 3;
            keepSpDir(sv);
            break;
        }
      
        //ボタンがクリックされたときの処理
        if (view == button1) {
            //button1が連続して押されたとき何もしないようにしてます
            if (num == 0) {
            } else {
                //button2が押されたときのnumの値で処理を変えます
                switch (num) {
                case 1:
                    //速度方向の維持
                    sv.setVx(sp); sv.setVy(sp);
                    num = 0;//これで連続して押されたときの処理につなげます
                    break;
                case 2:
                    sv.setVx(-sp); sv.setVy(sp);
                    num = 0;
                    break;
                case 3:
                    sv.setVx(sp); sv.setVy(-sp);
                    num = 0;
                    break;
                case 4:
                    sv.setVx(-sp); sv.setVy(-sp);
                    num = 0;
                    break;
                }
            }
        } else if (view == button2) {
            //button2が押された時点での速度方向によってnumの値を変えます
            if (sv.getVx()>0 && sv.getVy()>0) {
                sv.setVx(0); sv.setVy(0);
                num = 1;
            } else if (sv.getVx()<0 && sv.getVy() >0) {
                sv.setVx(0); sv.setVy(0);
                num = 2;
            } else if (sv.getVx()>0 && sv.getVy() <0) {
                sv.setVx(0); sv.setVy(0);
                num = 3;
            } else if (sv.getVx()<0 && sv.getVy()<0) {
                sv.setVx(0); sv.setVy(0);
                num = 4;
            }
        }
    }
  
    private void keepSpDir(SampleSurfaceView sv) {
      
        if (sv.getVx()>0 && sv.getVy()>0) {
            sv.setVx(sp); sv.setVy(sp);
        } else if (sv.getVx()<0 && sv.getVy() >0) {
            sv.setVx(-sp); sv.setVy(sp);
        } else if (sv.getVx()>0 && sv.getVy() <0) {
            sv.setVx(sp); sv.setVy(-sp);
        } else if (sv.getVx()<0 && sv.getVy()<0) {
            sv.setVx(-sp); sv.setVy(-sp);
        } else {
            sv.setVx(sp); sv.setVy(sp);
        }
    }
}

 なんかよく分からないままに改造したんで、おかしなとこもあるかも。
まあ、だいたいで。

ここだと、貼り付けたときもインデントが残る・・・。
初めからここで書いとけばよかった。

0 件のコメント:

コメントを投稿