Il seguente programma disegna qualcosa sullo schermo sfruttando una SurfaceView.
Il programma usa i metodi drawText(), drawRect() e drawBitmap().

PERCHE' le coordinate devono essere di tipo "float" ? Non dovrebbero essere "int" ?


Un'altra domanda: se provo a usare drawOval() Android Studio mi dice:

"Call requires API level 21 (current min is 15): android.graphics.Canvas#drawOval less... (Ctrl+F1)"

mi sembra STRANO che non si possano disegnare ellissi prima dell'API level 21!



********** ProvaSurfaceView.java ***********
package prove.myapplication68;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ProvaSurfaceView extends SurfaceView
{
SurfaceHolder surfaceHolder;

public ProvaSurfaceView(Context c)
{
super(c);
disegna();
}
public ProvaSurfaceView(Context c,AttributeSet attrs)
{
super(c,attrs);
disegna();
}
public ProvaSurfaceView(Context c,AttributeSet attrs,int defStyle)
{
super(c,attrs,defStyle);
disegna();
}

public void disegna()
{
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback()
{
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Canvas canvas = holder.lockCanvas(null);
disegnaQualcosa(canvas);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
});

}

public void disegnaQualcosa(Canvas canvas)
{
Bitmap bitmap=BitmapFactory.decodeResource(getResources() ,R.mipmap.ic_launcher);
Paint paint=new Paint();

paint.setTextSize(50);
paint.setColor(getResources().getColor(android.R.c olor.holo_red_light));

canvas.drawText("Ciao",100f,100f,paint);
canvas.drawRect(10f, 10f, 50f, 50f, paint);
canvas.drawBitmap(bitmap,150f,150f,null);
//canvas.drawOval(100F,100F,200F,200F,paint); // <--- Errore, PERCHE?
}
}


********** activity_main.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" androidaddingLeft="@dimen/activity_horizontal_margin"
androidaddingRight="@dimen/activity_horizontal_margin"
androidaddingTop="@dimen/activity_vertical_margin"
androidaddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<prove.myapplication68.ProvaSurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>


********** MainActivity.java **********
package prove.myapplication68;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}