图片的翻转和旋转
在画图时想实现翻转,但是一直没找到方法。只在网上找到一个翻转图片本身的方法
Resources res = this.getContext().getResources();
img = BitmapFactory.decodeResource(res, R.drawable.slogo);
Matrix matrix = new Matrix();
matrix.postRotate(90);
/*翻转90度*/
int width = img.getWidth();
int height = img.getHeight();
r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
然后可以直接把r_imgdraw到画布上,例如:
canvas.drawBitmap(a_img, 10, 10, p);
很简单吧~~ 貌似实现很多动画效果也有很多系统函数可用。Matrix 是一个处理翻转、缩放等图像效果的重要类
Matrix.postScale 可设置缩放比例,默认为1。
画图时图片的旋转可参考下面的代码:
public void
run() {
int x0=50;
int y0=50;
int x1=200;
int y1=200;
Canvas g ;
Paint paint = new Paint();
Bitmap img=Bitmap.createBitmap(x1-x0,y1-y0,Bitmap.Config.ARGB_8888);
g=new Canvas(img);
paint.setColor(Color.BLUE);
g.drawRect(new RectF(0,0,x1-x0,y1-y0), paint);
paint.setColor(Color.RED);
g.drawText("N", (x1-x0)/2, 10, paint);
g.drawText("W", 0, (y1-y0)/2, paint);
g.drawText("S", (x1-x0)/2, y1-y0, paint);
g.drawText("E", x1-x0-10, (y1-y0)/2, paint);
while
(blRun)
{
g= holder.lockCanvas();
//获取画布
paint.setColor(Color.RED);
g.drawLine((x0+x1)/2,0, (x0+x1)/2, 480, paint);
g.drawLine(0,(y0+y1)/2, 320, (y0+y1)/2, paint);
g.save();
g.rotate(180, (x0+x1)/2, (y0+y1)/2);
g.drawBitmap(img,x0, y0, paint);
paint.setColor(Color.BLUE);
g.restore();
g.drawLine(0,0, 320, 480, paint);
holder.unlockCanvasAndPost(g);
//解锁画布,提交画好的图像
System.out.println("run");
}
}