1、布局文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > </FrameLayout>
2、MainActivity
public class MainActivity extends Activity { private int view_width; private int view_height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout ll = (FrameLayout)findViewById(R.id.frameLayout1); ll.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); view_width = context.getResources().getDisplayMetrics().widthPixels; // 获取屏幕的宽度 view_height = context.getResources().getDisplayMetrics().heightPixels; // 获取屏幕的高度 } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint();//定义一个画笔 paint.setAntiAlias(true);//使用抗锯齿功能 Bitmap bitmap_bg = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.android); //创建一个在水平和垂直方向都重复的BitmapShader对象 BitmapShader bitmapshader = new BitmapShader(bitmap_bg, TileMode.REPEAT, TileMode.REPEAT); paint.setShader(bitmapshader);//设置渲染对象 canvas.drawRect(0, 0, view_width, view_height, paint);//绘制一个使用BitmapShader渲染的矩形 Bitmap bm = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.img02); //创建一个在水平方向上重复,在垂直方向上镜像的BitmapShader对象 BitmapShader bs = new BitmapShader(bm, TileMode.REPEAT, TileMode.MIRROR); paint.setShader(bs);//设置渲染对象 RectF oval = new RectF(0, 0, 280, 180); canvas.translate(40, 20);//将画面在X轴上平移40像素,在Y轴上平移20像素 canvas.drawOval(oval, paint);//绘制一个使用BitmapShader渲染的椭圆形 super.onDraw(canvas); } } }