使用rotateAnimation旋转方块

时间:2012-01-10 18:21:24

标签: java android

我有一个带有ImageView的LinearLayout,中心有一个方形图像,我需要应用旋转。在其4个边的每一边上与ImageView相邻是由4个其他视图构成的框架。如果我旋转ImageView 45degrees,ImageView是否会被其他视图剪切? rotateAnimation如何尊重ImageView的边界?

2 个答案:

答案 0 :(得分: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;
}

}

答案 1 :(得分:0)

我认为这会回答你的问题:

  

注意:无论动画如何移动或调整大小,包含动画的视图边界都不会自动调整以适应它。即便如此,动画仍将被绘制超出其视图的范围,并且不会被剪裁。但是,如果动画超出父视图的边界,则会发生剪裁。

Source