我有,在我需要办理旋转中心的方形图像的ImageView的一个LinearLayout中。毗邻它的每个4边的ImageView的是其他4个观点构建了一个框架。如果我旋转45度ImageView的,请问ImageView的会被切去了其他的看法?如何rotateAnimation尊重的ImageView的界限?
解决方案 进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;
进口android.os.Bundle;
进口android.view.GestureDetector;
进口android.view.KeyEvent;
进口android.widget.ImageView;
进口android.widget.ImageView.ScaleType;
公共类ImageFunctionsActivity扩展活动
{
/ **第一次创建活动时调用。 * /
ImageView的IV;
浮度= 0;
GestureDetector的gd;
上下文语境;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
IV =(ImageView的)findViewById(R.id.imageTeddy);
上下文= getApplicationContext();
旋转(度);
}
空旋转(浮动X)
{
位图bitmapOrg = BitmapFactory.de codeResource(getResources(),R.drawable.tedd);
INT宽度= bitmapOrg.getWidth();
INT高= bitmapOrg.getHeight();
INT newWidth = 200;
INT newHeight = 200;
//计算比例 - 在这种情况下= 0.4f
浮动scaleWidth =((浮点)newWidth)/宽度;
浮动scaleHeight =((浮点)newHeight)/身高;
字模=新的Matrix();
matrix.postScale(scaleWidth,scaleHeight);
matrix.postRotate(X);
位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,宽度,高度,矩阵,真);
iv.setScaleType(ScaleType.CENTER);
iv.setImageBitmap(resizedBitmap);
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件)
{
如果(键code == KeyEvent.KEY code_DPAD_UP)
{
度=度+ 10;
旋转(度);
}
如果(键code == KeyEvent.KEY code_DPAD_DOWN)//逆时针旋转
{
度=度10;
旋转(度);
}
返回true;
}
}
I have a LinearLayout with an ImageView with a square image in the center which I need to apply rotation. Bordering the ImageView on each of its 4 sides is a frame constructed from 4 other views. If I rotate the ImageView 45degrees, does the ImageView get clipped by the other views? How does rotateAnimation respect the boundaries of the 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;
}
}