2012-01-10 218 views
0

我有一個LinearLayout與ImageView與中心的正方形圖像,我需要應用旋轉。將ImageView的四邊連接起來是一個由4個其他視圖構成的框架。如果我旋轉ImageView 45degrees,ImageView是否會被其他視圖剪切? rotateAnimation如何尊重ImageView的邊界?旋轉正方形與旋轉動畫

+1

爲什麼不嘗試一下,看看會發生什麼? – 2012-01-10 18:35:57

回答

0

我認爲這會回答你的問題:

注:不管如何動畫可以移動或調整大小,保存你的動畫不會自動調整,以適應它的視圖的邊界。即便如此,動畫仍然會超出其視圖範圍,並且不會被剪輯。但是,如果動畫超出了父視圖的邊界,剪切就會發生。

Source

1
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.KeyEvent; 
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 


public class ImageFunctionsActivity extends Activity 

{

/** Called when the activity is first created. */ 
ImageView iv; 
float degree=0; 
GestureDetector gd; 
Context context; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    iv=(ImageView) findViewById(R.id.imageTeddy); 
    context=getApplicationContext(); 

    rotate(degree); 


} 

void rotate(float x) 
{ 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd); 

    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(x); 

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true); 

    iv.setScaleType(ScaleType.CENTER); 
    iv.setImageBitmap(resizedBitmap); 
} 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if(keyCode==KeyEvent.KEYCODE_DPAD_UP) 
    { 
     degree=degree+10; 
     rotate(degree); 
    } 
    if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // rotate anti-clockwise 
    { 
      degree=degree-10; 
      rotate(degree); 
    } 

    return true; 
} 

}

+0

這個代碼來旋轉圖像,你按UP或DOWN鍵... – MAC 2012-04-06 10:20:53