开发者

Rotate an Android ImageView AND its background

开发者 https://www.devze.com 2022-12-31 10:27 出处:网络
Using this mechanism I\'m able to successfully rotate the image in an Im开发者_Python百科ageView.Works great.

Using this mechanism I'm able to successfully rotate the image in an Im开发者_Python百科ageView. Works great.

However, if the ImageView has a background image, that drawable is not also rotated. How can I also rotate the background image of an ImageView?


Overwrite draw() instead of onDraw() to rotate the background.

@Override
public void draw(Canvas canvas) {
    canvas.save();
    canvas.rotate(45,xRotation,yRotation);
    super.draw(canvas);
    canvas.restore();
}


public class MainActivity extends Activity
{
    private ImageView mImageView = null;
    private Animation mRotateAnimation = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImageView = (ImageView) findViewById(R.id.my_image);
        mRotateAnimation = AnimationUtils.loadAnimation(this, R.anim.my_rotate_90);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mImageView.startAnimation(mRotateAnimation);
            return true;
        }
        return super.onTouchEvent(event);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消