微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐