主要方法:
//----设置矩形可视区域
canvas.save();
canvas.clipRect(20,20,50,50);
canvas.drawBitmap(bmp, 0, 0, paint);
canvas.restore();
//----利用Path设置可视区域
canvas.save();
Path path=new Path();
path.addCircle(100+30, 100+30, 30, Direction.CCW);
canvas.clipPath(path);
canvas.drawBitmap(bmp, 100, 100, paint);
canvas.restore();
//----利用Region设置可视区域
canvas.save();
Region region = new Region();
region.op(new Rect(20,20+300,100,100+300), Region.Op.XOR);
region.op(new Rect(40,20+300,80,150+300), Region.Op.XOR);
canvas.clipRegion(region);
canvas.drawBitmap(bmp, 0, 300, paint);
canvas.restore();
案例运行结果:
案例源码:
1 package caicai.animation; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.Canvas; 7 import android.graphics.Color; 8 import android.graphics.Matrix; 9 import android.graphics.Paint; 10 import android.graphics.PaintFlagsDrawFilter; 11 import android.graphics.Path; 12 import android.graphics.Path.Direction; 13 import android.graphics.Rect; 14 import android.graphics.RectF; 15 import android.graphics.Region; 16 import android.util.Log; 17 import android.view.MotionEvent; 18 import android.view.SurfaceHolder; 19 import android.view.SurfaceHolder.Callback; 20 import android.view.SurfaceView; 21 22 public class clipView extends SurfaceView implements Callback, Runnable { 23 private Paint paint;// 画布 24 private SurfaceHolder sfh; // 用于控制SurfaceView 25 private Canvas canvas;// 画布 26 private boolean flag;// 关闭线程标志 27 private Thread th;// 新建线程 28 private Bitmap bmp;// 位图 29 30 // 设置画布绘图无锯齿 31 private PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, 32 Paint.ANTI_ALIAS_FLAG); 33 34 public clipView(Context context) { 35 super(context); 36 sfh = this.getHolder();// 实例SurfaceHolder 37 sfh.addCallback(this);// 为SurfaceView添加状态监听 38 paint = new Paint();// 实例一个画笔 39 paint.setColor(Color.WHITE);// 设置画笔颜色为白色 40 setFocusable(true);// 设置焦点 41 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tanke); 42 } 43 44 @Override 45 public void surfaceCreated(SurfaceHolder holder) { 46 flag = true; 47 // 实例线程 48 th = new Thread(this); 49 // 启动线程 50 th.start(); 51 } 52 53 public void mydraw() { 54 try { 55 canvas = sfh.lockCanvas();// 锁定画布 56 if (canvas != null) { 57 canvas.drawColor(Color.BLACK); 58 //----设置矩形可视区域 59 canvas.save(); 60 canvas.clipRect(20,20,50,50); 61 canvas.drawBitmap(bmp, 0, 0, paint); 62 canvas.restore(); 63 //----利用Path设置可视区域 64 canvas.save(); 65 Path path=new Path(); 66 path.addCircle(100+30, 100+30, 30, Direction.CCW); 67 canvas.clipPath(path); 68 canvas.drawBitmap(bmp, 100, 100, paint); 69 canvas.restore(); 70 //----利用Region设置可视区域 71 canvas.save(); 72 Region region = new Region(); 73 region.op(new Rect(20,20+300,100,100+300), Region.Op.XOR); 74 region.op(new Rect(40,20+300,80,150+300), Region.Op.XOR); 75 canvas.clipRegion(region); 76 canvas.drawBitmap(bmp, 0, 300, paint); 77 canvas.restore(); 78 canvas.drawBitmap(bmp, 200, 300, paint); 79 } else { 80 Log.i("tt", "获取不到画布");// 释放画布 81 } 82 } catch (Exception e) { 83 84 } finally { 85 if (canvas != null) 86 sfh.unlockCanvasAndPost(canvas); 87 } 88 } 89 90 /** 91 * SurfaceView视图状态发生改变,响应此函数 92 */ 93 @Override 94 public void surfaceChanged(SurfaceHolder holder, int format, int width, 95 int height) { 96 // TODO Auto-generated method stub 97 98 } 99 100 /** 101 * SurfaceView视图消亡时,响应此函数 102 */ 103 @Override 104 public void surfaceDestroyed(SurfaceHolder holder) { 105 flag = false;// 结束游戏,设置线程关闭标志为false 106 107 } 108 109 public void logic() { 110 111 }; 112 113 @Override 114 public void run() { 115 while (flag) { 116 long start = System.currentTimeMillis(); 117 mydraw(); 118 logic(); 119 long end = System.currentTimeMillis(); 120 try { 121 if (end - start < 50) { 122 Thread.sleep(50 - (end - start)); 123 } 124 } catch (InterruptedException e) { 125 e.printStackTrace(); 126 } 127 } 128 } 129 130 // 获取点击坐标 131 @Override 132 public boolean onTouchEvent(MotionEvent event) { 133 return true; 134 } 135 136 }
1 package caicai.animation; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Window; 6 import android.view.WindowManager; 7 8 public class MainActivity extends Activity { 9 /** Called when the activity is first created. */ 10 @Override 11 public void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 //设置全屏 14 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 15 requestWindowFeature(Window.FEATURE_NO_TITLE); 16 //显示自定义的SurfaceView视图 17 setContentView(new clipView(this)); 18 19 } 20 }
谢谢支持网站“趣淘鼓浪屿(www.qtgly.com)”