初めて質問させていただきます。
私は今、Android StudioでボタンをクリックしたらSurfaceViewに描画されている円の色を変えるアプリを作りたいと思っていますが、クリックされたときの処理の内容の書き方がわからないです。
具体的には、RelativeLayoutにButtonウィジェットとSurfaceViewウィジェットを配置し、Buttonウィジェットをクリックされた時に、SurfaceViewにcanvas.drawCircleメソッドで描画されている円の色を変えようと思っています。
以下がソースコードです。
まずMainActivityのコードです。
public class MainActivity extends Activity {
SurfaceView surfaceView;
GameView gameView;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView=(SurfaceView)findViewById(R.id.surfaceViewId);
gameView=new GameView(this,surfaceView);
button1=(Button)findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ここに書く内容がわからないです。
}
});
}
(以下略)
次にサーフェイスビューのコードです。
public class GameView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
private final Paint paint =new Paint();
private SurfaceHolder holder;
private Thread thread;
private int x=100,y=100;
public GameView(Context context,SurfaceView sv){
super(context);
holder=sv.getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread=new Thread(this);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread=null;
}
@Override
public void run(){
while (thread!=null){
doDraw(holder);
}
}
public void doDraw(SurfaceHolder holder){
Canvas canvas=holder.lockCanvas();
if(canvas!=null){
paint.setColor(Color.BLUE);
canvas.drawColor(Color.WHITE);
canvas.drawCircle(x,y,50,paint);
holder.unlockCanvasAndPost(canvas);
}
}
}
次にXMLのコードです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
</RelativeLayout><LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="45dp" android:id="@+id/ll1"> <Button android:layout_width="100dp" android:layout_height="match_parent" android:text="色変更赤" android:id="@+id/button" android:onClick="Red" /> <Button android:layout_width="100dp" android:layout_height="match_parent" android:text="色変更青" android:id="@+id/button2" /> <Button android:layout_width="100dp" android:layout_height="match_parent" android:text="色変更緑" android:id="@+id/button3" /> </LinearLayout> <SurfaceView android:layout_width="match_parent" android:layout_height="440dp" android:id="@+id/surfaceViewId" android:layout_below="@+id/ll1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />
理解が浅いのですべてのコードをのせました。
足りない情報があれば追加でのせます。
解決法がわかるかたがいらしたらよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/04 02:48