2012年5月21日 星期一

[ANDROID]畫一個圖形

ShowDrawActivity.java

package bear.Graphis.showdraw;

import android.app.Activity;
import android.os.Bundle;

public class ShowDrawActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        DrawView drawView=new DrawView(this);
        setContentView(drawView);
    }
}


DrawView.java

package bear.Graphis.showdraw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;

public class DrawView extends View {

 private ShapeDrawable mDrawable;

 public DrawView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  
  int x=10;      //在建構子設定要畫的東西
  int y=10;
  int width=450;
  int height=50;
  
  mDrawable = new ShapeDrawable(new OvalShape());
  mDrawable.getPaint().setColor(Color.CYAN);//設定顏色
     mDrawable.setBounds(y, y, width+x, height+y);//設定顯示圖形的範圍大小
     
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  mDrawable.draw(canvas);
 }


}



結果:


課堂上的練習題之一

沒有留言:

張貼留言