开发者

Rotate an Android ImageView AND its background

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜