[英]rotating custom image around its center
I have an png image width 200 x height 50 in pixel. 我有一个png图像宽度200 x高度50像素。 I need to rotate it around its center by an angle.
我需要围绕它的中心旋转一个角度。
My onDraw method 我的onDraw方法
@Override
public void onDraw(Canvas canvas){
initDrawingTools();
drawRect(canvas);
drawBack(canvas);
Matrix mat = new Matrix();
Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.needle);
cX = getWidth()/2-bMap.getWidth()/2;
cY = getHeight()/2-bMap.getHeight()/2;
mat.setTranslate(cX, cY);
mat.postRotate(angleSpeed,cX, cY);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), mat, true);
canvas.drawBitmap(bMapRotate, mat, null);
}
This is the closest i managed. 这是我管理的最接近的。 As i understand the center of image is floating while it is rotating.
据我所知,图像中心在旋转时是浮动的。 For example: 0 degree - 200x50, 90 degree 50x200 etc. And in this case its not rotating around its center.
例如:0度 - 200x50,9度50x200等。在这种情况下,它不会围绕其中心旋转。 Could someone give me some hints or explain how to get the result?
有人可以给我一些提示或解释如何得到结果?
EDIT working Mikel Pascualc suggestion: 编辑工作Mikel Pascualc建议:
How to make arrow remain in angled position after animation??? 动画后如何使箭头保持在倾斜位置???
seekBar = (SeekBar)findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
ROTATE_TO = speed;
spin();}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
ROTATE_FROM = speed;}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
speed = progress;
// textView = (TextView)findViewById(R.id.textView1);
// textView.setText(progress);
//rodykle.onSpeedChanged((float)progress);
}
});
}
public void spin(){
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText("From: " + ROTATE_FROM + "\nTo: " + ROTATE_TO + "\nProgress: " + speed);
ImageView needle = (ImageView) findViewById(R.id.needle1);
RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration((long) 2*1000);
r.setRepeatCount(0);
r.setFillAfter(true); // <-- ADDED THIS TO STAY IMG IN ANGLE AFTER ANIMATION
needle.startAnimation(r);
}
You better use an animation. 你最好使用动画。 Example:
例:
public class NeedleRotateActivity extends Activity {
private static final float ROTATE_FROM = 0.0f;
private static final float ROTATE_TO = -10.0f * 360.0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView needle = (ImageView) findViewById(R.id.needle);
RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration((long) 2*1500);
r.setRepeatCount(0);
r.setFillAfter(true);
needle.startAnimation(r);
}
I have done a similar thing to this. 我对此做了类似的事情。 I rotated a bottle round itself to slowly reduce the speed until it stops.
我将一个瓶子自己旋转,慢慢降低速度,直至停止。 Sharing a piece of code from that, hope it help you.
从中分享一段代码,希望对您有所帮助。
rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(duration);
rotateAnimation.setRepeatCount(0);
imgBottle.startAnimation(rotateAnimation);
rotateAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim) {
};
public void onAnimationRepeat(Animation anim) {
};
public void onAnimationEnd(Animation anim) {
duration = duration + 70;
};
});
imgBottle.startAnimation(rotateAnimation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.