PRÁTICA 10 – CANVAS E DRAW
Objetivo:
Utilizar métodos Canvas e Bitmaps em um ImageView para desenhar diretamente na tela.
Introdução
Canvas -> Tela do Android.
BitMap -> Mapa de bits que representa uma imagem na tela do Android.
ImageView -> Local onde pode ser carregada (JPG por exemplo) ou gerada uma imagem.
Tarefa:
Criar tipos aplicações de desenho e gráficos:
Programa 1 – Design padrão com um imageView vazio (onde será desenhado):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/layout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Desenho!" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <ImageView android:layout_width="300dp" android:layout_height="300dp" android:id="@+id/grafico" android:layout_gravity="center_horizontal" android:adjustViewBounds="true" /> </LinearLayout> |
Programa 1 – Alguns desenhos simples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { ImageView graphicImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); graphicImage = (ImageView) findViewById(R.id.grafico); Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); canvas.drawCircle(100,100,10, paint); paint.setColor(Color.RED); canvas.drawLine(100,100,200,200, paint); paint.setColor(Color.YELLOW); canvas.drawText("Desenho",100,200, paint); paint.setColor(Color.GREEN); canvas.drawRect(20,20,40,40,paint); graphicImage.setImageBitmap(bitmap); } } |
Programa 2 – Layout para fazer gráficos de parábolas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/layout" android:orientation="vertical"> <ImageView android:layout_width="300dp" android:layout_height="300dp" android:id="@+id/grafico" android:layout_gravity="center_horizontal" android:adjustViewBounds="false" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Equação de Segundo Grau" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="a" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/a" android:layout_weight="1" android:inputType="numberDecimal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="b" android:id="@+id/textView3" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b" android:layout_weight="1" android:inputType="numberSigned|numberDecimal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="c" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/c" android:layout_weight="1" android:inputType="numberSigned|numberDecimal" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X1=" android:id="@+id/x1" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X2=" android:id="@+id/x2" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Resolver" android:id="@+id/button2" android:layout_weight="1" android:layout_gravity="right" android:clickable="true" android:onClick="calculaSegundoGrau" /> </LinearLayout> |
Programa 2 – Programa para fazer gráficos de parábolas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ImageView graphicImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); graphicImage = (ImageView) findViewById(R.id.grafico); } public void drawGraphics(double a, double b, double c) { Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); canvas.drawLine(0,150,300,150, paint); canvas.drawLine(150,0,150,300, paint); paint.setColor(Color.BLUE); double x,y; double scaleX = 150; double scaleY = 150; //Escala de -20 a 20 (0.1 de precisao) for (int loop=-200;loop<200;loop++) { x = loop / 10d; y = a * x*x + b * x + c; x = x + scaleX; y = y + scaleY; canvas.drawPoint((float)x, (float)y, paint); } graphicImage.setImageBitmap(bitmap); } public void calculaSegundoGrau(View v) { EditText a_edit = (EditText) findViewById(R.id.a); EditText b_edit = (EditText) findViewById(R.id.b); EditText c_edit = (EditText) findViewById(R.id.c); TextView x1_text = (TextView) findViewById(R.id.x1); TextView x2_text = (TextView) findViewById(R.id.x2); x1_text.setText("X1 = "); x2_text.setText("X2 = "); if (a_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de a!",Toast.LENGTH_SHORT).show(); return; } if (b_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de b!",Toast.LENGTH_SHORT).show(); return; } if (c_edit.getText().length() == 0) { Toast.makeText(this, "Esqueceu o valor de c!",Toast.LENGTH_SHORT).show(); return; } double a = Double.parseDouble(a_edit.getText().toString()); double b = Double.parseDouble(b_edit.getText().toString()); double c = Double.parseDouble(c_edit.getText().toString()); double delta = b * b - 4*a*c; if (delta < 0) { Toast.makeText(this, "Delta menor que ZERO!",Toast.LENGTH_SHORT).show(); return; } double x1 = -b + Math.sqrt(delta); double x2 = -b - Math.sqrt(delta); x1_text.setText("X1 = " + String.format("%.2f",x1)); x2_text.setText("X2 = " + String.format("%.2f",x2)); drawGraphics(a,b,c); } } |
Desafio: Plote uma função seno e faça um Layout onde pode-se configurar a frequência em Hz:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/layout" android:orientation="vertical"> <ImageView android:layout_width="300dp" android:layout_height="300dp" android:id="@+id/grafico" android:layout_gravity="center_horizontal" android:adjustViewBounds="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Senóide" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Amplitude" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/amp" android:layout_weight="1" android:inputType="numberDecimal" android:text="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Frequência (Hz)" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/freq" android:layout_weight="1" android:inputType="numberSigned|numberDecimal" android:text="1" /> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener { ImageView graphicImage; EditText editTextAmp; EditText editTextFreq; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); graphicImage = (ImageView) findViewById(R.id.grafico); editTextAmp = (EditText) findViewById(R.id.amp); editTextFreq = (EditText) findViewById(R.id.freq); editTextAmp.setOnFocusChangeListener(this); editTextFreq.setOnFocusChangeListener(this); } public void drawGraphics(double amp, double freq) { Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); canvas.drawLine(0,150,300,150, paint); canvas.drawLine(150,0,150,300, paint); paint.setColor(Color.BLUE); double y, xIni, plotX1, plotY1, plotX2, plotY2; xIni = -6; y = amp * Math.sin(2*Math.PI*freq*xIni); plotX1 = (xIni*50 + 150); plotY1 = (y*50 + 150); for (double x=xIni;x<6;x=x+0.01d) { y = amp * Math.sin(2*Math.PI*freq*(x)); plotX2 = (x*50 + 150); plotY2 = (y*50 + 150); canvas.drawLine((float)plotX1, (float)plotY1, (float)plotX2, (float)plotY2, paint); plotX1 = plotX2; plotY1 = plotY2; } graphicImage.setImageBitmap(bitmap); } @Override public void onFocusChange(View v, boolean hasFocus) { double amp = Double.parseDouble(editTextAmp.getText().toString()); double freq = Double.parseDouble(editTextFreq.getText().toString()); drawGraphics(amp,freq); } } |