Android 围绕其中心旋转自定义图像
我有一个png图像宽200 x高50像素。我需要绕着它的中心旋转一个角度 我的onDraw方法Android 围绕其中心旋转自定义图像,android,android-canvas,Android,Android Canvas,我有一个png图像宽200 x高50像素。我需要绕着它的中心旋转一个角度 我的onDraw方法 @Override public void onDraw(Canvas canvas){ initDrawingTools(); drawRect(canvas); drawBack(canvas); Matrix mat = new Matrix(); Bitmap bMap = BitmapFactory.d
@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度,90度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);
}
seekBar=(seekBar)findViewById(R.id.seekBar1);
seekBar.setonseekbarchaneglistener(新的onseekbarchaneglistener(){
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
旋转至=速度;
自旋();}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
旋转_FROM=速度;}
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser){
速度=进步;
//textView=(textView)findViewById(R.id.textView1);
//textView.setText(进度);
//rodykle.onSpeedChanged((浮动)进度);
}
});
}
公共空间{
TextView textView1=(TextView)findViewById(R.id.textView1);
textView1.setText(“From:“+ROTATE\u From+”\nTo:“+ROTATE\u TO+”\n进度:“+speed”);
ImageView指针=(ImageView)findViewById(R.id.1);
RotateAnimation r=新的RotateAnimation(RotateAnimation,RotateAnimation,Animation.RELATIVE,0.5f,Animation.RELATIVE,0.5f);
r、 设置持续时间((长)2*1000);
r、 setRepeatCount(0);
r、 setFillAfter(true);//我做了类似的事情。我旋转了一个瓶子,慢慢降低速度,直到它停止。分享一段代码,希望它能帮助你
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);
你最好使用动画。
例如:
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);
}
我“认为”(不确定),您不应该为此使用“createBitmap”,因为它在内存使用方面没有效率。有一次,由于这个原因,我从内存错误中解脱出来,因为它显示没有创建新的位图???公共静态位图createBitmap(位图源,int x,int y,int width,int height,矩阵m,布尔过滤器)“添加到API级别1”会从源位图的子集返回一个不可变位图,并通过可选矩阵进行转换。新位图可能与源位图是同一对象,也可能是已创建的副本。该位图将以与原始位图相同的密度进行初始化。如果源位图不可变且请求的子集与源位图相同位图本身,然后返回源位图,并且没有创建新位图。
动画工作正常,有一些有趣的时间:),但这不是解决方案,因为在旋转图像回到起始角度后。我使用搜索栏将旋转从
,旋转到
。将这段代码添加到问题中只是需要o添加r.setFillAfter(true);
。现在动画在回合后保留:)谢谢你的建议!我将把这一行添加到示例中;大多数人都会希望这样做。很高兴它对你有用!