【问题标题】:Rotate an image by 90,180,270 and 360 degrees every time a button is clicked and retain the rotation degree in portrait and landscape mode?每次单击按钮时将图像旋转 90,180,270 和 360 度并在纵向和横向模式下保留旋转度数?
【发布时间】:2013-11-11 09:28:34
【问题描述】:

我正在 android 中制作一个应用程序,只要单击旋转按钮 90,180,270 和 360 度,就必须旋转图像。在纵向和横向模式下,旋转必须相同。 我是 Android 编程的初学者,我有 Java 的基本知识。 请尽快帮助我。

我使用的代码如下。我只能将它旋转90度一次。它如何继续下去?

btn_rotate = (ImageButton) findViewById(R.id.btn_rotate); btn_rotate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.about_us_ipad_p);
            Display d = getWindowManager().getDefaultDisplay();
            @SuppressWarnings("deprecation")
            int x = d.getWidth();
            @SuppressWarnings("deprecation")
            int y = d.getHeight();
            ImageView imgView = (ImageView) findViewById(R.id.imgViewEdit_Pic);
            Bitmap scaledBmap = Bitmap.createScaledBitmap(bmap, y, x, true);
            Matrix matrix = new Matrix();
            matrix.postRotate(90, 180, 270);
            Bitmap rotatedBmap = Bitmap.createBitmap(scaledBmap,0,0,scaledBmap.getWidth(),scaledBmap.getHeight(),matrix,true);
            imgView.setImageBitmap(rotatedBmap);

        }
    });

【问题讨论】:

  • 在android中使用anim属性。

标签: android image rotation orientation


【解决方案1】:

这段代码可能会对你有所帮助,而且它是不言自明的......

public class TestActivity extends Activity {
    private int rotation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState != null) {
            rotation = savedInstanceState.getInt("ANGLE");
        }
        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        final Button button = (Button) findViewById(R.id.iv_icon);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        final int width = metrics.widthPixels;
        final int height = metrics.heightPixels;
        final Bitmap imageBitmap = BitmapFactory.decodeResource(
                getResources(), R.drawable.image1);
        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, true);
        imageView.setImageBitmap(getRotatedBitmap(scaledBitmap));
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                rotation += 90;
                rotation %= 360;
                Bitmap bitmap = getRotatedBitmap(scaledBitmap);
                imageView.setImageBitmap(bitmap);
            }
        });
    }

    private Bitmap getRotatedBitmap(Bitmap bitmap) {
        if (rotation % 360 == 0) {
            return bitmap;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation, bitmap.getWidth() / 2,
                bitmap.getHeight() / 2);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight() / 2, matrix, true);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("ANGLE", rotation);
        super.onSaveInstanceState(outState);
    }
}

【讨论】:

  • 非常感谢!!它以我需要的方式旋转。但是,如果随着活动再次开始而改变方向,它就不会保留。你能帮我做定位部分吗?
  • 旋转工作正常,但发生了一件奇怪的事情。假设我在纵向模式下将图像旋转 90 度,现在更改为横向模式,图像未显示为旋转 90 度,但是当我单击旋转按钮时,它会记得我之前已旋转并将图像旋转 180 度。
  • 我已经检查了我的代码中的所有内容。它工作正常。我认为您没有考虑我编辑的答案...]
  • 我重新检查了代码,它和你的一样……然后清理了项目,它工作了!!!非常感谢您的帮助!!!
  • 你能解释一下哪部分代码使图像在纵向和横向模式下看起来相同吗?
【解决方案2】:
yourimage.setRotation(yourimage.getRotation() + 90);

【讨论】:

【解决方案3】:

在清单文件中,像这样锁定屏幕方向:

android:screenOrientation="portrait"

android:screenOrientation="landscape"

【讨论】:

  • 我的应用程序需要两个方向。当我在纵向模式下将图像旋转 90 度时,它也必须在横向模式下旋转相同的角度。我可能必须保存有关图像的旋转角度信息,并在方向改变时保存并传递它,但我不确定我是否正确,也不知道该怎么做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
相关资源
最近更新 更多