SurfaceViewは高速に画面の描画を行い、ゲームやマルチメディアなど高速処理に向いています。
SurfaceViewの基本的なSampl Code
public class MainActivity extends AppCompatActivity { private Bitmap mImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SurfaceViewTestClass(this)); // SurfaceViewのインスタンスを生成 } public class SurfaceViewTestClass extends SurfaceView implements SurfaceHolder.Callback { //コンストラクタ public SurfaceViewTestClass(Context context) { super(context); getHolder().addCallback(this); } public void surfaceCreated(SurfaceHolder holder) { Paint paint = new Paint(); paint.setTextSize(30); paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); // image取得 mImage = BitmapFactory.decodeResource(getResources(), R.drawable.image); Canvas canvas = holder.lockCanvas(); // Canvas lock canvas.drawBitmap(mImage, 10, 10, paint); canvas.drawText("TestString", 10, 200, paint); canvas.drawCircle(60, 300, 50, paint); holder.unlockCanvasAndPost(canvas); // Canvas unlock } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceDestroyed(SurfaceHolder holder) { } } }
参考 Drawableクラスを使用するとき
Drawable drawable= ContextCompat.getDrawable(getApplicationContext(), R.drawable.image);