2015-01-09 30 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并绘制图像。