2012-09-20 59 views
0

我有一個包含圖像的ImageView。該圖像通過按鈕點擊旋轉,但有時變得更小或獲得其原始大小。我不知道是什麼原因造成的。位圖大小變化

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    //tablelayout here 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:orientation="vertical" > 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:maxWidth="200dp" 
     android:maxHeight="200dp" 
     /> 
    </LinearLayout> 
</RelativeLayout> 

Inital設置:

iv = (ImageView)findViewById(R.id.imageView1); 
     int id = getResources().getIdentifier("landolt", "drawable", getPackageName()); 
     iv.setImageResource(id); 

     myImg = BitmapFactory.decodeResource(getResources(), R.drawable.landolt); 
     matrix = new Matrix(); 

     size = 200; 

     randomize = new Random(); 
     random = randomize.nextInt(8) + 1; 
     rotate = getAngle(random); //function created by me 
     matrix.postRotate(rotate); 
     rotatedBitmap = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true); 
     iv.setImageBitmap(rotated); 

因此,圖像旋轉,在代碼中沒有大小的變化。

上按一下按鈕的旋轉:

   btn1.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (rotate == -90) 
         { 
          //rotate back to original direction 
          old_rotate = -rotate; 
          matrix.postRotate(old_rotate); 
          //next rotation 
          random = randomize.nextInt(8) + 1; 
          rotate = getAngle(random); 
          matrix.postRotate(rotate); 
          rotatedBitmap = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true); 
          iv.setLayoutParams(new LinearLayout.LayoutParams(size, size)); 
          iv.setImageBitmap(rotated); 
         } 
        } 
        }); 

當我開始練習時,圖像顯示的原始大小,有時小。當我點擊一個按鈕時,圖像變得更小或者變得像原始大小一樣大。

這是怎麼回事?

+0

你的佈局xml的其餘部分是什麼樣的? – CaseyB

+0

查看編輯代碼 – erdomester

+0

難道是因爲它觸及邊緣,並且因爲您有android:adjustViewBounds =「true」,它會縮小整個事物的範圍嗎? – CaseyB

回答

1

我想通了爲什麼圖像不斷變化的大小。旋轉時,它在imageView中沒有足夠的位置。我在這link找到了解決我的問題的解決方案。