android-围绕其中心旋转自定义图像

我有一个以像素为单位的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度-200×50、90度50×200等.在这种情况下,它不会绕其中心旋转.有人可以给我一些提示或解释如何获得结果吗?

编辑工作的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);
    }

解决方法:

您最好使用动画.
例:

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);
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...