android中圖片的翻轉和旋轉

來源:互聯網
上載者:User

圖片的翻轉和旋轉

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.