java - 使用旋转动画旋转正方形

标签 java android

我有一个带有 ImageView 的 LinearLayout,中心有一个方形图像,我需要应用旋转。 ImageView 的 4 条边的每一边都有一个由其他 4 个 View 构成的框架。如果我将 ImageView 旋转 45 度,ImageView 是否会被其他 View 剪切? rotateAnimation 如何尊重 ImageView 的边界?

最佳答案

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;
}

}

关于java - 使用旋转动画旋转正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8808381/

相关文章:

java - 我需要使用 java.awt.Robot.send() 发送任意文本字符串

java - 我想在我的小程序中添加计时器

java - Android 相机预览中的单帧图像

java - 如何在java编码中生成soap请求并获取响应

android - Google Play 因不安全的加密模式而拒绝我的应用

java - android - 包括布局或 fragment

android - Lint 构建失败,出现安全错误 "WrongConstant: Incorrect constant"。 IntDef 注释

java - 我如何注释我的辅助方法,以便 Eclipse 知道它的参数在返回 true 时是非空的?

java - 日期格式失败

android - RecyclerView 未在 RelativeLayout 和 ScrollView 内部展开