2012-06-05 30 views
0

旋转位图时出现问题。我的要求如下,如何在Android中点击按钮时旋转位图?

  1. 通过相机捕获图像并在ImageView中显示捕获图像。
  2. 当我点击/触摸图像时,在图像视图上显示两个按钮。一个按钮用于将图像相对于顺时针(45度)方向旋转,另一个按钮用于将图像相对于逆时针方向旋转。

    我用下面的代码这个过程中,不幸的是它只能一次一次,当我在第一时间t单击该按钮后,有在按一下按钮没有任何变化都会相应旋转45度的图像。 //的onclick

    公共无效的onClick(视图v){

    switch (v.getId()) { 
    
        case R.id.right_image_rotate: 
    
         try { 
          System.gc(); 
          Runtime.getRuntime().gc(); 
          unbindDrawables(findViewById(R.id.image_viewer)); 
          imageRotate(); 
         } catch (Exception e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         break; 
    
        case R.id.left_image_rotate: 
    
         try { 
          System.gc(); 
          Runtime.getRuntime().gc(); 
          unbindDrawables(findViewById(R.id.image_viewer)); 
          imageRotate(); 
         } catch (Exception e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         break; 
        case R.id.image_viewer: 
         rotateLayout.setVisibility(View.VISIBLE); 
         imageLeft_rotate.setVisibility(View.VISIBLE); 
         imageRight_rotate.setVisibility(View.VISIBLE); 
         break; 
        default: 
         break; 
        } 
    } 
    
    
    
        Bitmap bitmapOrg = BitmapFactory.decodeFile(saved_image_file 
          .getAbsolutePath()); 
    
        int width = bitmapOrg.getWidth(); 
    
        int height = bitmapOrg.getHeight(); 
    
        int newWidth = 200; 
    
        int newHeight = 200; 
    
        // calculate the scale - in this case = 0.4f 
    
        float scaleWidth = ((float) newWidth)/width; 
    
        float scaleHeight = ((float) newHeight)/height; 
    
        Matrix matrix = new Matrix(); 
    
        matrix.postScale(scaleWidth, scaleHeight); 
        matrix.postRotate(45); 
    
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, 
          height, matrix, true); 
    
        srcBitmap.setScaleType(ScaleType.CENTER); 
        srcBitmap.setImageBitmap(resizedBitmap); 
    
+0

我们需要更多的代码。你在使用onClickListener吗? matrix.portRotate(deg)也应该在45度下正常工作。 – bhekman

+0

看到这个答案http://stackoverflow.com/a/10332901/1289716 和http://stackoverflow.com/a/10042149/1289716 – MAC

+0

@BradHekman:我累了,但它只有一次。当我点燃拳头时,它会旋转,然后我点击第二次意味着它不会旋转。 – Aerrow

回答

2

这是因为每次单击该按钮时,你仍然引用原始图像,而不是一个你变了。如果您想继续旋转或修改已更改的图像,则必须参考该图像;不是原来的。

只需创建一个全局变量并将更改后的位图存储在那里。然后当按下按钮时,检查它是否为空。如果为空,请使用原件;否则使用改变的一个

+0

听起来有道理。 – bhekman

+0

@Cruceo:谢谢... – Aerrow

+0

欢迎你,祝你的应用程序 – Guardanis

相关问题