ホームへ戻る

4.4 円・楕円を描く


本節では円・楕円を描画する drawCircle, drawOval を紹介します。
また、paintに今回「グラデーション」をセットしてみました。


public class AndroidsCastleActivity extends Activity {

        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(new MyView(this));
        }

        class MyView extends View {

                public MyView(Context context) {
                        super(context);
                        
                }

                @Override
                protected void onDraw(Canvas canvas) {
                
                        Paint paint = new Paint();
                        paint.setColor(Color.argb(255, 255, 0, 0));//赤セット
                        
                        canvas.drawCircle(50, 50, 50, paint);//円描画
                        
                        //グラデーション情報を作成(0,100)から(240,200)に向けて緑→黄色でグラデーション
                        Shader s = new LinearGradient(0, 100, 240, 200, Color.GREEN, Color.YELLOW,  Shader.TileMode.CLAMP);  
                        paint.setShader(s);//グラデーションをセット 
                        
                        canvas.drawOval(new RectF(0.0f, 100.0f, 240.0f, 200.0f), paint);//楕円描画
                        
                }

        }
}


drawCircleメソッドが円を描画するメソッドです。
drawRect( x, y, 半径, paint );
となっています。

paintのスタイルに今回グラデーション情報を持たせました。
Paintについての章ではないので、詳細は省略します。
詳しくはまた別の章でご説明します。

drawOval は、四角形に内接する楕円を描くメソッドです。
つまり、紫の四角形の領域を指定すれば良いということです。



詳細はリファレンスをどうぞ。

→分からないことがあれば掲示板で質問して下さい


Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

- Remical Soft -