我的png图像宽度为200 x高度50像素。我需要围绕它的中心旋转一个角度。
我的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);
}
这是我管理的最接近的。据我所知,图像中心在旋转时是浮动的。例如:0度 - 200x50,9度50x200等。在这种情况下,它不会围绕其中心旋转。有人可以给我一些提示或解释如何得到结果吗?
编辑Mikel Pascualc的建议:
动画后如何使箭头保持在倾斜位置
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);
}
答案 0 :(得分:3)
您最好使用动画。 示例:
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);
}
答案 1 :(得分:0)
我对此做了类似的事情。我将一个瓶子自己旋转,慢慢降低速度,直至停止。从中分享一段代码,希望对您有所帮助。
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);