概要
vistaでandroid studio 1.0.1やってみた。
練習問題やってみた。
練習問題
sin波を、表示せよ。
方針
- カスタムviewを、作る。
写真
サンプルコード
package com.ohisamallc.ohiapp154.ohiapp154;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by ore on 22/08/29.
*/
public class SinView extends View {
private Paint paint;
public SinView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.BLUE);
}
public SinView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
}
public void setColor(int color) {
paint.setColor(getResources().getColor(color));
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
int j;
double angle;
for (j = 0; j < 720; j++)
{
angle = ((double) j) / 360 * 2 * Math.PI;
int k = (int)(100 * Math.sin(angle) + 150);
canvas.drawLine(j, k, j, k + 1, paint);
}
}
}
レイアウト
<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">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.ohisamallc.ohiapp154.ohiapp154.SinView
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
以上。