旋转图像,而在Android上缩水 [英] Rotate image without shrinking on Android

查看:118
本文介绍了旋转图像,而在Android上缩水的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个应用程序,我工作的指南针。所以我试图创建一个名为CompassView新的观点基本上延伸ImageView的,显示了一个位图,有东,西北南尖就可以了,使用传感器来发现手机正指向度,旋转图像相应因此,这将创造一个实际的指南针。但问题是,如果我试图将图像旋转一定的角度想45度,它收缩了下来。下面是一些图片来解释它更好的。

正如你所看到的,第二图像收缩下来的时候我尽量绕45.我希望它做的是这样的:

下面是code我目前使用的:

 位图BMAP = BitmapFactory.de codeResource(getResources()
                R.drawable.compass);
        矩阵xMatrix版权=新的Matrix();
        xMatrix.reset();
        xMatrix.postRotate(360 mValue,75,75); //这是75,因为150是图像宽度
        位图bMapRotate = Bitmap.createBitmap(BMAP,0,0,
                bMap.getWidth(),bMap.getHeight(),xMatrix版权,真正的);
        setImageBitmap(bMapRotate);
 

任何帮助将是AP preciated。感谢

编辑:(液) 我终于得到了它的工作多亏了公认的答案。这里是$ C $词现在用的人谁想要知道它是如何工作:

  RotateAnimation rAnimAntiClockWise =新RotateAnimation(
                    360  -  mValue,360  -  event.values​​ [0]
                    Animation.RELATIVE_TO_SELF,0.5F,
                    Animation.RELATIVE_TO_SELF,0.5F);
// mValue是度的角度和我减去从360做逆时针旋转,并event.values​​ [0]是一回事mValue
rAnimAntiClockWise.setFillAfter(真正的);
rAnimAntiClockWise.setInterpolator(新LinearInterpolator());
rAnimAntiClockWise.setDuration(0);
startAnimation(rAnimAntiClockWise);
 

解决方案

您可以使用一个替代的把戏,将工作像一样旋转,并且不调整图像大小。其实我转动45度角的图像和动画后,仍然改变。

  rAnimAntiClockWise =新RotateAnimation(0.0,45.0f,
                Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,
                0.5F);
        rAnimAntiClockWise.setFillAfter(真正的);
        rAnimAntiClockWise.setInterpolator(新LinearInterpolator());
            位= BitmapFactory.de codeResource(getResources()
                    R.drawable.rotate);
        rAnimAntiClockWise.setDuration(100);
        img_rotate.startAnimation(rAnimAntiClockWise);
 

I need to create a compass on an app i am working on. So i tried to create a new view called CompassView which basically extends imageview, shows a bitmap that has east west north south pointed on it, uses sensors to find the degrees the phone is pointed at, and rotate the image accordingly so that it would create an actual compass. But the problem is if i try to rotate the image to some angles like 45 degrees, it shrinks down. Here are some images to explain it better.

As you can see, the second image is shrinked down when i try to rotate around 45. What i want it to do is this:

Here is the code i am currently using:

        Bitmap bMap = BitmapFactory.decodeResource(getResources(),
                R.drawable.compass);
        Matrix xMatrix = new Matrix();
        xMatrix.reset();
        xMatrix.postRotate(360-mValue, 75, 75); //This is 75 because 150 is the image width
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
                bMap.getWidth(), bMap.getHeight(), xMatrix, true);
        setImageBitmap(bMapRotate);

Any help would be appreciated. THanks

EDIT: (SOLUTION) I finally got it working thanks to the accepted answer. Here is the code i am using for anyone who wants to know how it worked:

RotateAnimation rAnimAntiClockWise = new RotateAnimation(
                    360 - mValue, 360 - event.values[0],
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
//mValue is the angle in degrees and i subtracted it from 360 to make it anticlockwise, and event.values[0] is the same thing as mValue
rAnimAntiClockWise.setFillAfter(true);
rAnimAntiClockWise.setInterpolator(new LinearInterpolator());
rAnimAntiClockWise.setDuration(0);
startAnimation(rAnimAntiClockWise);

解决方案

You can use a alternative trick which will work like same as rotate and doesn't resize the image. I actually rotate the image with 45 degree angle and remain changes after animation.

rAnimAntiClockWise = new RotateAnimation(0.0f, 45.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rAnimAntiClockWise.setFillAfter(true);
        rAnimAntiClockWise.setInterpolator(new LinearInterpolator());       
            bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.rotate);     
        rAnimAntiClockWise.setDuration(100);
        img_rotate.startAnimation(rAnimAntiClockWise);

这篇关于旋转图像,而在Android上缩水的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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