gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:36:15 24 4
gpt4 key购买 nike

我有一个带有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com