在定点旋转的位图就像SurfaceView时钟手 [英] Rotate bitmap in fixed point like a clock hand in SurfaceView

查看:196
本文介绍了在定点旋转的位图就像SurfaceView时钟手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制文件夹时钟指针的图像。我想它在固定点像时钟指针转动。我曾尝试以下code这在旋转圆形路径手绕一个固定点,但并不像时钟的手。

 矩阵矩阵=新的Matrix();
 matrix.reset();
 矩阵preTranslate(0,0)。
 matrix.postRotate(angleRotation,-0,-0);
 matrix.postTranslate(240,480); canvas.drawColor(Color.WHITE);
 canvas.drawBitmap(位图,矩阵,零);

我挣扎了几个小时把它整理出来。任何帮助将大大AP preciated。

我曾尝试下面的帮助链接以及没有成功。


  1. <一个href=\"http://stackoverflow.com/questions/21234002/surfaceview-draw-image-and-reduce-size-rotate-image\">SurfaceView,画的形象和减小尺寸,旋转图片


  2. <一个href=\"http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a-center-point\">Android:如何在旋转的中心点位图


  3. <一个href=\"http://stackoverflow.com/questions/13399173/how-to-rotate-a-bitmap-in-android-about-images-center-smoothly-without-oscillato\">How在Android的顺利位图旋转约影像中心,而不振荡运动



解决方案

我找到了答案。让像素 PY 放在画布的任何一点。 bitmap.getWidth()/ 2 沿位图宽度中间点。它可以为 X cordinate 的任何一点。对于Ÿcordinate 我已为 10 angleRotation 是根据需要的角度。

所以 matrix.postTranslate(-bitmap.getWidth()/ 2,-10); 是旋转的

下面是code。

 矩阵矩阵=新的Matrix();
        matrix.reset();
        涂料粉刷=新的油漆();
        浮动PX = 240;
        浮PY = 480;
        matrix.postTranslate(-bitmap.getWidth()/ 2,-10);
        matrix.postRotate(angleRotation);
        matrix.postTranslate(PX,PY);
        canvas.drawBitmap(位图,矩阵,油漆);

以上code满足我的要求。请修改根据自己的需要。

I have an image in drawable folder for clock hand. I want to rotate it in fixed point like a clock hand. I have tried below code which rotates the hand in circular path around a fixed point but not as like clock hand.

 Matrix matrix = new Matrix();
 matrix.reset();
 matrix.preTranslate(0, 0);
 matrix.postRotate(angleRotation, -0, -0);
 matrix.postTranslate(240, 480);

 canvas.drawColor(Color.WHITE);
 canvas.drawBitmap(bitmap, matrix, null);

I am struggling for hours to get it sorted out. Any help will be greatly appreciated.

I have tried below links for help as well with no success.

  1. SurfaceView, draw image and reduce size, rotate image

  2. Android: How to rotate a bitmap on a center point

  3. How to rotate a bitmap in Android about images center smoothly without oscillatory movement

解决方案

I found the answer. Let px and py be any point on canvas. bitmap.getWidth()/2 is middle point along bitmap width. It can be any point for x cordinate. For y cordinate I have taken it as 10. angleRotation is the angle that is as per required.

So matrix.postTranslate(-bitmap.getWidth()/2, -10); is the point of rotation.

Below is the code.

        Matrix matrix = new Matrix();
        matrix.reset();
        Paint paint = new Paint();
        float px = 240;
        float py = 480;
        matrix.postTranslate(-bitmap.getWidth()/2, -10);
        matrix.postRotate(angleRotation);
        matrix.postTranslate(px, py);
        canvas.drawBitmap(bitmap, matrix, paint);

Above code satisfy my requirements. Please modify it as per your need.

这篇关于在定点旋转的位图就像SurfaceView时钟手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆