[英]Animate rotation of an image in Android
我有一個齒輪圖像,我想連續旋轉固定點。
之前我通過在我的Android類中將圖像作為ImageView包含並對其應用RotateAnimation來實現此目的。
@InjectView(R.id.gear00) ImageView gear00; RotateAnimation ra07 = new RotateAnimation(0, 359, 129, 186); ra07.setDuration(10000); ra07.setRepeatCount(RotateAnimation.INFINITE); ra07.setInterpolator(new LinearInterpolator()); gear00.setAnimation(ra07);
基本上,我將ImageView注入到類中並應用旋轉動畫。
但是,我再也沒有使用ImageView的奢侈了。 我必須使用Bitmap並在畫布上旋轉它。
我怎樣才能在onDraw()方法中完成我之前正在做的事情,並且在畫布上連續旋轉固定點的位圖?
EDIT1:
我嘗試了下面提到的一個建議,我的代碼看起來有點像下面的內容
在onCreate()中:
Matrix matrix = new Matrix();
matrix.setRotate(10, 100, 200);
然后在onDraw()中(其中gear00Scaled是要在畫布上旋轉的位圖):
canvas.drawBitmap(gear00Scaled,matrix,new Paint());
我嘗試的另一種方法包括保存畫布,旋轉它,然后恢復它:
canvas.save();
canvas.rotate(10);
canvas.drawBitmap(gear00Scaled,100,200,null);
canvas.restore();
似乎都沒有工作!
創建一個XML類(假設:rotate.xml)並將其放在res / anim文件夾中,然后在其中編寫以下代碼:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" />
然后在您的java類中,在OnCreate
執行以下操作:
final Animation a = AnimationUtils.loadAnimation(CustomActivity.this,
R.anim.rotate);
a.setDuration(3000);
gear00.startAnimation(a);
要么
要使用位圖,我希望以下代碼序列可以幫助您:
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());
如果您從以下方法檢查以下方法:
〜框架\\基\\圖形\\ java中\\機器人\\圖形\\ Bitmap.java
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
這可以解釋它對旋轉和翻譯的作用。
我想在我的應用程序中將自定義圖像旋轉為進度對話框。 您可以使用以下代碼旋轉圖像:
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f ,
Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(1000);
imageView.setAnimation(anim);
imageView.startAnimation(anim);
在你的onCreate()做
Matrix matrix = new Matrix();
在onDraw中
float angle = (float) (System.currentTimeMillis() % ROTATE_TIME_MILLIS)
/ ROTATE_TIME_MILLIS * 360;
matrix.reset();
matrix.postTranslate(-source.getWidth() / 2, -source.getHeight() / 2);
matrix.postRotate(angle);
matrix.postTranslate(centerX, centerY)
canvas.drawBitmap(source, matrix, null);
invalidate(); // Cause a re-draw
ROTATE_TIME_MILLIS是整圈時間,例如2000是2秒。
代碼前面有兩件事:
所以有兩種方法:第一種是使用imageview,我個人覺得它更簡單,更好。 下面是我的活動類中的一個方法,mLittleChef是一個ImageView。
public void doCanvas(){
//Create our resources
Bitmap bitmap = Bitmap.createBitmap(mLittleChef.getWidth(), mLittleChef.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Bitmap chefBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.dish_special);
//Link the canvas to our ImageView
mLittleChef.setImageBitmap(bitmap);
ValueAnimator animation= ValueAnimator.ofFloat(0, 359, 129, 186);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
//Clear the canvas
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.save();
canvas.rotate(value, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawBitmap(chefBitmap, 0, 0, null);
canvas.restore();
mLittleChef.invalidate();
}
});
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(1000);
animation.start();
}
另一種方法是使用自定義畫布類。 我為ExampleDrawView mLittleChefDraw創建了自己的自定義視圖類,而不是ImageView; 在我的布局中。 你可能不得不用這一個來實現你在旋轉方面正確尋找的東西,我只是用畫布的中間作為樞軸點進行360度轉彎。
public class ExampleDrawView extends View {
Bitmap bitmap;
Float mRotate= 0f;
Handler h;
//State variables
final int STATE_PAUSE = 2;
final int STATE_ROTATE = 3;
int STATE_CURRENT;
public ExampleDrawView(Context context, AttributeSet attrs) {
super(context, attrs);
h = new Handler();
bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.dish_special);
STATE_CURRENT= STATE_PAUSE;
}
Runnable move = new Runnable() {
@Override
public void run() {
switch (STATE_CURRENT){
case STATE_ROTATE:
if (mRotate<360){
mRotate++;
invalidate();
}else{
STATE_CURRENT= STATE_PAUSE;
}
h.postDelayed(move, 20);
break;
}
}
};
public void startDrawing(){
if(STATE_CURRENT == STATE_PAUSE){
STATE_CURRENT= STATE_ROTATE;
mRotate=(float) 0;
h.postDelayed(move, 20);
}
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//change your rotate point here, i just made it the middle of the canvas
canvas.rotate(mRotate,getWidth()/2,getHeight()/2);
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
然后回到活動中調用它來啟動它:
public void doCanvasCustomView(){
mLittleChefDraw.startDrawing();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.