1. Создайте новое Android-приложение, см. семинарское занятие 2.
2. В пакете org.ggpi.MyAndroidProject02 создайте новый класс
с именем Test. В контекстном меню: New\Class...
Компонент, который должен выводить простую двумерную графику,
следует наследовать от класса View:
... extends View ...
Пример 1. Файл Test.java
package org.ggpi.MyAndroidProject02; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; //import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class Test extends View { private int height; private int width; Paint paint=new Paint(); public Test(Context context, AttributeSet attrs) { super(context, attrs); } public Test(Context context){ this(context, null); } @Override protected void onMeasure(int widthSpecId, int heightSpecId) { this.height = View.MeasureSpec.getSize(heightSpecId); this.width = View.MeasureSpec.getSize(widthSpecId); setMeasuredDimension(width, height); } protected void onDraw(Canvas canvas){ super.onDraw(canvas); paint.setColor(Color.BLUE); paint.setStrokeWidth(2); canvas.drawLine(0, 0, width, height, paint); canvas.drawLine(width, 0, 0, height, paint); paint.setColor(Color.RED); canvas.drawLine(width/2, 0, width/2, height, paint); canvas.drawLine(width, height/2, 0, height/2, paint); paint.setColor(Color.GREEN); paint.setTextSize(height/10); canvas.drawText("green", width/4, height/8, paint); paint.setColor(Color.CYAN); canvas.drawCircle(width/2, height/2, width/4, paint); paint.setColor(Color.RED); canvas.drawCircle(width/2, height/2, width/8, paint); } }
Пример 1. Файл main.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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="It's a drawing sample" /> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <org.ggpi.MyAndroidProject02.Test android:layout_width="100dp" android:layout_height="200dp" /> </LinearLayout>
Пример 2. Файл Test.java: график синуса.
protected void onDraw(Canvas canvas){ super.onDraw(canvas); paint.setColor(Color.GREEN); paint.setStrokeWidth(2); int d_width = width; int d_height = height; int x0 = d_width/2, y0 = d_height/2; canvas.drawLine(0, y0, d_width, y0, paint); // x-axe canvas.drawLine(x0, 0, x0, d_height, paint); // y-axe int gx1 = x0, gy1 = y0, gx2, gy2; double amplitude = 1; double y; double xMin = -2*3.14, xMax = 2*3.14; double xScale = width/(xMax - xMin); double yMin = -amplitude, yMax = amplitude; double yScale = height/(yMax - yMin); for (double x = xMin; x < xMax; x+=0.1) { y = amplitude * Math.sin(x); gx2 = x0 + (int)(xScale * x); gy2 = y0 - (int)(yScale * y); canvas.drawLine(gx1, gy1, gx2, gy2, paint); gx1 = gx2; gy1 = gy2; } }
Источники