<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 fl.addView(new MyView(this)); } public class MyView extends View{ private float view_width=300; private float view_height=300; public MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint(); paint.setAntiAlias(true); Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.ic_launcher); //创建一个在水平方向和垂直方向都重复的BitmapShader对象 BitmapShader bitmapShader=new BitmapShader(bitmap_bg,TileMode.REPEAT,TileMode.REPEAT); paint.setShader(bitmapShader);//设置渲染对象 canvas.drawRect(0, 0,view_width,view_height,paint); Bitmap bm=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.img1); //创建一个在水平方重复,在垂直方向镜像的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渲染的椭圆形 } } }
运行结果如图所示
转载请注明出处:http://blog.csdn.net/acmman/article/details/45601887