Android - Rotate image inside the button

前端 未结 2 455
囚心锁ツ
囚心锁ツ 2020-12-18 04:37

I want to rotate the image inside the button on click of the button. I tried with just ImageView and it works but for accessibility purpose I need to use Button instead of I

相关标签:
2条回答
  • 2020-12-18 04:38

    The simplest method would be to rotate the entire button (and as Frank N. Stein suggested in the comments, an ImageButton is probably best suited, although there's nothing to stop you from using a different widget).

    There are several ways to rotate the button, but a ViewPropertyAnimator is again likely the most straightforward:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float deg = button.getRotation() + 180F;
            button.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());
        }
    });
    

    Edit: By the way, if you want the arrow to reverse its original animation, you could instead try:

    float deg = (button.getRotation() == 180F) ? 0F : 180F;
    

    instead of float deg = button.getRotation() + 180F;

    0 讨论(0)
  • 2020-12-18 05:00

    You can set bitmap as background in button.

    Matrix matrix = new Matrix();
    
    matrix.postRotate(90);
    
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);
    
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
    

    First take bitmap from any source and then rotate it and then set it as background in button.

    BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
    button.setBackground(bdrawable);
    
    0 讨论(0)
提交回复
热议问题