2015-01-09 66 views
0
public void Rotate(View v) 
{ 
    ImageView img = (ImageView)findViewById(R.id.imageView1); 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),arr[current]); 

// Getting width & height of the given image. 
    int w = bmp.getWidth(); 
    int h = bmp.getHeight(); 
// Setting pre rotate to 90 
    Matrix mtx = new Matrix(); 
    mtx.preRotate(90); 
// Rotating Bitmap 
    Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true); 
    BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); 
    img.setImageBitmap(rotatedBMP); 
} 

當我在菜單中選擇旋轉選項時,它只是第一次旋轉時,我再次選擇它,圖像不會旋轉。任何soln它只旋轉一次圖像。在菜單項列表中多次選擇旋轉選項時如何再次旋轉?

+0

這是哪種編程語言/平臺? –

回答

1

實際上,它每次都會將您的位圖旋轉90度。問題是你總是從資源中獲得相同的(不旋轉的)位圖,而不是從ImageView中旋轉。

相反的:

BitmapFactory.decodeResource(getResources(),arr[current]); 

這種嘗試:

Bitmap bmp = ((BitmapDrawable)img.getDrawable()).getBitmap(); 

該解決方案也比較好,因爲你不從資源每次解碼位圖。

0

我剛剛使用簡單的數學。

public void Rotate_r(View v) 
{ 
    rcount++; 
    if(rdgr==360) rdgr=90; 
    else rdgr=rdgr+90; 
    if(rcount==1){ 
     ldgr=rdgr; 
     rcount--; 
    } 
    ImageView img = (ImageView)findViewById(iv); 
    Bitmap bmp =BitmapFactory.decodeResource(getResources(),arr[current]); 
    // Getting width & height of the given image. 
    int w = bmp.getWidth(); 
    int h = bmp.getHeight(); 
    // Setting pre rotate to 90 
    Matrix mtx = new Matrix(); 
    mtx.preRotate(rdgr); 
    // Rotating Bitmap 
    Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true); 
    @SuppressWarnings("deprecation") 
    BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); 
    img.setImageBitmap(rotatedBMP); 
} 

我用一個計數器rcount如果圖像被向右旋轉和遞減如果向左旋轉它簡單地遞增。也將變量rdgr初始化爲90,如rdgr=90,並且當rcount遞增時,它將90個更多地添加到rdgr並繪製圖像。