Rotate Square With Rotateanimation
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 fro
Solution 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;
publicclassImageFunctionsActivityextendsActivity
{
/** Called when the activity is first created. */
ImageView iv;
float degree=0;
GestureDetector gd;
Context context;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView) findViewById(R.id.imageTeddy);
context=getApplicationContext();
rotate(degree);
}
voidrotate(float x)
{
BitmapbitmapOrg= BitmapFactory.decodeResource(getResources(),R.drawable.tedd);
intwidth= bitmapOrg.getWidth();
intheight= bitmapOrg.getHeight();
intnewWidth=200;
intnewHeight=200;
// calculate the scale - in this case = 0.4ffloatscaleWidth= ((float) newWidth) / width;
floatscaleHeight= ((float) newHeight) / height;
Matrixmatrix=newMatrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
BitmapresizedBitmap= Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
iv.setScaleType(ScaleType.CENTER);
iv.setImageBitmap(resizedBitmap);
}
@OverridepublicbooleanonKeyDown(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);
}
returntrue;
}
}
Solution 2:
I think that this will answer your question :
Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it. Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.
(Source)
Post a Comment for "Rotate Square With Rotateanimation"