【发布时间】:2014-07-15 02:17:18
【问题描述】:
我有一个宽 200 x 高 50 像素的 png 图像。我需要将它围绕中心旋转一个角度。
我的 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、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);
}
【问题讨论】:
-
我“认为”(不确定),您不应该为此使用“createBitmap”,因为它在内存使用方面效率不高。有一次,因为这个,我得到了 OutOfMemory 错误
-
如此处所示,没有创建新的位图???
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) Added in API level 1 Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.