質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

497閲覧

矩形(Rect)を描写

S.I

総合スコア48

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

1グッド

0クリップ

投稿2017/10/12 03:45

編集2017/10/12 06:45

androidstudioのメイン画面で矩形を描写しようと思っているのですが、Design画面に表示されません。androidstudio上では、MainActivity.javaのコードの19行目onDrawという文字の色がグレーになっており認識されません........ちなみに、エラーメッセージは出ません。

activity_main.xml

java

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.nakahara.game.MainActivity"> 8 9 10 <com.example.nakahara.game.MyView 11 android:id="@+id/view" 12 android:layout_width="0dp" 13 android:layout_height="0dp" 14 android:layout_marginBottom="16dp" 15 android:layout_marginLeft="16dp" 16 android:layout_marginRight="16dp" 17 android:layout_marginTop="16dp" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintLeft_toLeftOf="parent" 20 app:layout_constraintRight_toRightOf="parent" 21 app:layout_constraintTop_toTopOf="parent" 22 app:layout_constraintHorizontal_bias="0.0" 23 app:layout_constraintVertical_bias="0.0" /> 24</android.support.constraint.ConstraintLayout> 25

MyView.java

java

1import android.content.Context; 2import android.graphics.Canvas; 3import android.graphics.Color; 4import android.graphics.Paint; 5import android.graphics.Rect; 6import android.graphics.RectF; 7import android.view.View; 8 9public class MyView extends View { 10 public MyView(Context context) { 11 super(context); 12 } 13 14 @Override 15 protected void onDraw(Canvas canvas) { 16 Paint paint = new Paint(); 17 paint.setColor(Color.argb(255, 0, 0, 0)); 18 19 Rect rect = new Rect(10, 20, 30, 40); 20 canvas.drawRect(rect, paint); 21 22 RectF rectF = new RectF(40.5f, 20.5f, 60.5f, 40.5f); 23 canvas.drawRect(rectF, paint); 24 25 paint.setStyle(Paint.Style.STROKE); 26 canvas.drawRect(10, 50, 30, 80, paint); 27 } 28} 29 30
mhashi👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dit.

2017/10/12 05:21

間違っていてもコードには関係ないところなのですがどうしても気になってしまったので…短形ではなく矩形(くけい)です…。
S.I

2017/10/12 05:36

本当ですね。。。ありがとうございます。
guest

回答2

0

ベストアンサー

onDraw()はViewクラスに属するメソッドなので、MainActivityの中で記述しても意味がありません。Viewを継承したクラスを自作し、その中にonDrawを記述するようにしましょう。例えばMyView.javaとして作成したなら、こんな感じになるでしょう。

MyView.java(package文とimport文は省略しています)

Java

1public class MyView extends View { 2 public MyView(Context context) { 3 super(context); 4 } 5 6 public MyView(Context context, AttributeSet attrs) { 7 super(context, attrs); 8 } 9 10 @Override 11 protected void onDraw(Canvas canvas) { 12 Paint paint = new Paint(); 13 paint.setColor(Color.argb(255, 0, 0, 0)); 14 15 Rect rect = new Rect(10, 20, 30, 40); 16 canvas.drawRect(rect, paint); 17 18 RectF rectF = new RectF(40.5f, 20.5f, 60.5f, 40.5f); 19 canvas.drawRect(rectF, paint); 20 21 paint.setStyle(Paint.Style.STROKE); 22 canvas.drawRect(10, 50, 30, 80, paint); 23 } 24} 25

なお、Color.argb()の引数を255,255,255,255とされていましたが、これでは真っ白になってしまうので、白いActivity上では何も見えません。上記では第2引数から第4引数を0にして、黒い矩形が表示されるようにしています。

こうして自作したMyViewをXMLに配置したければ、下記のように完全修飾クラス名(パッケージ名まで含まれたクラス名)をタグ名として記述します。

XML

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.nakahara.game.MainActivity"> 8 9 <com.example.nakahara.game.MyView 10 android:id="@+id/view" 11 android:layout_width="0dp" 12 android:layout_height="0dp" 13 android:layout_marginBottom="16dp" 14 android:layout_marginLeft="16dp" 15 android:layout_marginRight="16dp" 16 android:layout_marginTop="16dp" 17 app:layout_constraintBottom_toBottomOf="parent" 18 app:layout_constraintLeft_toLeftOf="parent" 19 app:layout_constraintRight_toRightOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21</android.support.constraint.ConstraintLayout>

こんなところでとりあえず図柄は表示されるのではないでしょうか。細かい調整はこの先の話でしょう。

投稿2017/10/12 05:14

編集2017/10/12 08:36
keicha_hrs

総合スコア6766

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

S.I

2017/10/12 06:36

とても丁寧で分かりやすい回答ありがとうございます。コードを打ちなおしました。Design画面には表示されるようになったのですが、エミュレータで実行すると、 game keeps stopping × close app となります…なぜなのでしょう…
S.I

2017/10/12 06:39

Custom view MyView is not using the 2- or 3-argument View constructors; XML attributes will not work Tip: Try to refresh the layout. Design画面の下にRender errors、Render problemとの表示がありました。 1~2行目はその内容です。
keicha_hrs

2017/10/12 08:38 編集

ごめんなさい、私のサンプルのチョンボです。XMLにカスタムビューを配置する場合、View継承クラスには引数2つ(または3つ)のコンストラクターが必要でした。それに沿って回答を書き直しているので、それで試してみてください。
S.I

2017/10/12 08:51

実行できました!!! お手数おかけしました!本当にありがとうございました^^
guest

0

AppCompatActivityにonDrawというメソッドは存在しません。
そのため、未使用メソッドの警告として灰色になっていると考えられます。メッセージを確認してください。

また、onDrawはViewに実装されているメソッドなので、やりたいこととやっていることがズレています。独自のViewを作りたいのであればViewを継承したクラスを実装してください。

さらに上記をやったとしても、Design画面には表示されないと思いますよ。

投稿2017/10/12 04:57

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問