Animate rotation of an image in Android

后端 未结 4 584
暗喜
暗喜 2021-01-14 08:08
  • I have a gear image which I want to continuously rotate about a fixed point.

  • Earlier I was accomplishing this by including the image in my Android

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 08:53

    Make an XML class (suppose: rotate.xml) and place it in res/anim folder and then write the following code in it:

    
    
    

    Then in your java class, do the following in OnCreate:

    final Animation a = AnimationUtils.loadAnimation(CustomActivity.this,
                    R.anim.rotate);
            a.setDuration(3000);
            gear00.startAnimation(a);
    

    OR

    To do it using bitmap, I hope the following sequence of code helps you:

    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());
    

    If you check the following method from:

    ~frameworks\base\graphics\java\android\graphics\Bitmap.java

    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
            Matrix m, boolean filter)
    

    this would explain what it does with rotation and translate.

提交回复
热议问题