1、MainActivity
package com.njupt.shudhu02; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
2、MyView
package com.njupt.shudhu02; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.view.View; public class MyView extends View{ public MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setARGB(150, 0, 100, 200); // paint.setStyle(Paint.Style.STROKE);//这时候画笔画出来的东西都是空心的 paint.setStrokeWidth(10); paint.setTextSize(100);// /** * canvas.drawRect(100, 300,300,500,paint) * 该函数的4个参数分别为距离左边的距离,上,右,下 */ // canvas.drawRect(100, 300,300,500,paint);//画矩形 /** * 前3个参数的意思是:前两个是圆心的坐标,第三个是半径... */ // canvas.drawCircle(200, 200, 150, paint);//画圆形 /** * canvas.drawText("apple", 50, 200, paint) * 这四个参数分别为:要绘制的文本,X轴的坐标,基准线,所使用的画笔 */ // canvas.drawText("apple", 50, 200, paint);//绘制文本 /** * canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), 100,300, paint) * 前四个参数为:要显示的图形,图片左边界,图片的上边界,所使用的画笔 * */ canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), 100,300, paint);//绘制图形 super.onDraw(canvas); } }