Chinaunix首页 | 论坛 | 博客
  • 博客访问: 861621
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: 嵌入式

2011-08-17 15:16:01

Matrix.setPolyToPoly其实就是多变形的一个坐标变换,效果很不错。
Matrix.setPolyToPoly的解释 - roy756@126 - 百年一剑
 
private void doDraw(Canvas canvas, float src[], float dst[]) {
            canvas.save();
            mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
            canvas.concat(mMatrix);
            
            mPaint.setColor(Color.GRAY);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(0, 0, 64, 64, mPaint);
            canvas.drawLine(0, 0, 64, 64, mPaint);
            canvas.drawLine(0, 64, 64, 0, mPaint);
            
            //Miles:
            canvas.drawCircle(64, 64, 2, mPaint);
            
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.FILL);
            // how to draw the text center on our square
            // centering in X is easy... use alignment (and X at midpoint)
            float x = 64/2;
            // centering in Y, we need to measure ascent/descent first
            float y = 64/2 - (mFontMetrics.ascent + mFontMetrics.descent)/2;
            canvas.drawText(src.length/2 + "", x, y, mPaint);
            
            canvas.restore();
        }
右下角的点是我添加的,可以更清楚的看到图形的变化,我们在看onDraw函数
@Override
 protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;

            canvas.drawColor(Color.WHITE);

            canvas.save();
            canvas.translate(10, 10);
            // translate (1 point)
            doDraw(canvas, new float[] { 0, 0 }, new float[] { 5, 5 });
            canvas.restore();
            
            canvas.save();
            canvas.translate(160, 10);
            // rotate/uniform-scale (2 points)
           /* doDraw(canvas, new float[] { 32, 32, 64, 32 },
                           new float[] { 32, 32, 64, 48 });*/
            doDraw(canvas, new float[] { 32, 32, 64, 64 },
                           new float[] { 32, 32, 64, 0 });
            canvas.restore();

            canvas.save();
            canvas.translate(10, 110);
            // rotate/skew (3 points)
            doDraw(canvas, new float[] { 0, 0, 64, 0, 0, 64 },
                           new float[] { 0, 0, 96, 0, 24, 64 });
            canvas.restore();

            canvas.save();
            canvas.translate(160, 110);
            // perspective (4 points)
            doDraw(canvas, new float[] { 0, 0, 64, 0, 64, 64, 0, 64 },
                           new float[] { 0, 0, 96, 0, 64, 96, 0, 64 });
            canvas.restore();
        }
这里的第一个数组是原来的坐标,每两个表示一点(x,y),所以 { 0, 0, 64, 0, 64, 64, 0, 64 }表示了4点。
阅读(5828) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~