【问题标题】:Updated: Rotate a bitmap that exceeds screen size更新:旋转超过屏幕尺寸的位图
【发布时间】:2012-07-02 09:15:47
【问题描述】:

我的图片比较大,但只需要在屏幕上显示一部分。我已经做到了。我的问题是,图像必须用手指旋转。下面的代码可以在小图像上正常工作。但是在我的图像上它有一个错误的行为,上下重新定位图像并奇怪地旋转它。如何使我的图像正常旋转?,作为一个小图像。然后我必须能够触摸到颜色。我得到了答案here,但我仍然需要努力。但首先,请告诉我如何解决旋转问题,如果你对第二个问题有想法,我想听听他们的意见。

这是图片:

这是它在屏幕上的显示方式:

XML 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView android:id="@+id/imag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/roata"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="-200dp"
    android:scaleType="center"/>
 </RelativeLayout>

Java 代码

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    picture = (ImageView)findViewById(R.id.imag);
    picture.setOnTouchListener(onTableTouched);
}

public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent evt)
    {
        double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
        int rotation = (int) Math.toDegrees(r);


        if (evt.getAction() == MotionEvent.ACTION_DOWN)
        {
            //
        }

        if (evt.getAction() == MotionEvent.ACTION_MOVE)
        {
           updateRotation(rotation);
        }

        if (evt.getAction() == MotionEvent.ACTION_UP)
        {
            //
        }

        return true;
    }
};

private void updateRotation(double rot)
{
    float newRot = new Float(rot);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

    Matrix matrix = new Matrix();
    matrix.postRotate(newRot - 50);

    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    picture.setImageBitmap(redrawnBitmap);
}

更新: 因为BitmapMatrix 无处可去,我试图调整RotateAnimtaion() 以获得尽可能相似的东西。我认为修改第二个参数和动画动态的持续时间,我们可以在不改变 Y 位置的情况下得到类似的结果。现在,问题是图像被裁剪,我认为是因为scaleType="center"。我将发布代码和屏幕截图。可以改进吗?

Animation a = new RotateAnimation(0.0f, param,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    a.setFillEnabled(true);
    a.setFillAfter(true);
    a.setDuration(param);

  picture.startAnimation(a);

【问题讨论】:

  • 尝试使用 android:scaleType="fitXY" 而不是 android:scaleType="center"
  • @Aerrow..scaleType=fitXY 正在拉伸图像,我不想要这个,但我已经尝试过了,我得到了相同的行为

标签: android matrix touch image-rotation


【解决方案1】:

只需将 Bitmap 声明为全局:

private WeakReference<Bitmap> bitmap;

并使用

bitmap =new WeakReference(BitmapFactory.decodeResource(getResources(), R.drawable.roata));

而不是

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

【讨论】:

  • 如果我这样做,我不能再使用bitmap.getWidth()bitmap.getHeight() in updateRotation()
  • @AlexAndro : WeakReference 如果另一种方法可以避免内存泄漏。如果您的应用程序是由@Andro 运行的答案则很好,但您必须尝试将 Bitmap 声明为 WeakReference
【解决方案2】:

我认为问题是,您试图在每个 updateRotation() 调用中重绘相同的位图。

删除这条线怎么样,

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

来自updateRotation(double rot) 并将其声明为全局,这样您就不需要一次又一次地创建相同的位图,这就是您获得 OutOfMemory 的原因。

试试这个,

Bitmap bitmap=null;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    picture = (ImageView)findViewById(R.id.imag);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
    picture.setOnTouchListener(onTableTouched);
}


public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent evt) {
        double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
        int rotation = (int) Math.toDegrees(r);

        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            //
        }

        if (evt.getAction() == MotionEvent.ACTION_MOVE) {
           updateRotation(rotation);
        }

        if (evt.getAction() == MotionEvent.ACTION_UP) {
            //
        }

        return true;
    }
};

private void updateRotation(double rot) {
    float newRot = new Float(rot);

    Matrix matrix = new Matrix();
    matrix.postRotate(newRot - 50);

    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    picture.setImageBitmap(redrawnBitmap);
}

【讨论】:

  • 它有同样奇怪的效果。尝试旋转图像时,图像会上下移动(平移效果),改变它的 Y 位置,这是最让我烦恼的。然后图像往往会出现在初始位置。
  • 仔细看,旋转本​​身似乎得到了Y平移,即使是小图像但至少小图像在旋转,大图像....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 2011-06-15
  • 2016-05-21
  • 1970-01-01
  • 2012-04-10
相关资源
最近更新 更多