【问题标题】:rotating custom image around its center围绕其中心旋转自定义图像
【发布时间】: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.

标签: android android-canvas


【解决方案1】:

你最好使用动画。 示例:

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

【讨论】:

  • 动画效果很好,有一些有趣的时间:),但这不是解决方案,因为旋转图像回到起始角度后。我使用 Seek Bar 来调整ROTATE_FROMROTATE_TO。将那段代码添加到问题中
  • 只需要添加r.setFillAfter(true);。现在动画在转弯后仍然存在 :) 感谢您的建议!
  • 我将该行添加到示例中;大多数人都会想要那个。很高兴它对你有用!
【解决方案2】:

我也做过类似的事情。我旋转了一个瓶子,慢慢地降低速度,直到它停止。分享一段代码,希望对你有帮助。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2011-07-08
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多