圖片的翻轉和旋轉
在畫圖時想實現翻轉,但是一直沒找到方法。只在網上找到一個翻轉圖片本身的方法
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");
}
}