android - 在Android中,当旋转图像时它会慢慢消失。

标签 android

我使用下面的代码来旋转图像。图像旋转没有问题,但是当我旋转图像时,图像像素减少了。如果我连续旋转意味着图像将会消失。我该如何解决这个问题。

ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
<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/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/test" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="66dp"
        android:text="Click here to Rotate" />

</RelativeLayout>

ImageResizeTestActivity.java

public class ImageResizeTestActivity extends Activity {
    /** Called when the activity is first created. */
    Button click;
    ImageView img;
    static File rotated_File;

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

        click = (Button) findViewById(R.id.button1);
        img = (ImageView) findViewById(R.id.imageView1);
        String fname = "Rotated_Image.jpg";
        rotated_File = new File("/sdcard/" + fname);
        click.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (!(rotated_File.exists())) {
                    Log.v("Inside if", "if");
                    rotateImage();
                } else {
                    Log.v("Inside else", "else");
                    rotateImage(rotated_File.getAbsolutePath());
                }

            }
        });
    }

    protected void rotateImage(String absolutePath) {
        // TODO Auto-generated method stub
        Bitmap myImg = BitmapFactory.decodeFile(absolutePath);

        Matrix matrix = new Matrix();
        matrix.postRotate(90);

        Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
                myImg.getHeight(), matrix, true);
        saveImage_Rotate(rotated);
        img.setImageBitmap(rotated);
    }

    protected void rotateImage() {
        // TODO Auto-generated method stub
        Bitmap myImg = BitmapFactory.decodeResource(getResources(),
                R.drawable.test);

        Matrix matrix = new Matrix();
        matrix.postRotate(90);

        Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
                myImg.getHeight(), matrix, true);
        saveImage_Rotate(rotated);
        img.setImageBitmap(rotated);
    }

    static void saveImage_Rotate(Bitmap dest2) {

        if (rotated_File.exists())
            rotated_File.delete();
        try {
            FileOutputStream out = new FileOutputStream(rotated_File);
            dest2.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Initially my Image is旋转后Rotated image

最佳答案

快速查看问题。您多次保存 jpg,在(有损)压缩过程中,它的质量会越来越低。

我建议你使用 png 代替

关于android - 在Android中,当旋转图像时它会慢慢消失。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10950080/

相关文章:

android - 使用 RxAndroidBle (rxJava) 将命令列表发送到设备

Android Activity 不响应触摸事件 由于没有窗口焦点而导致放置事件

android - 在 android 通话期间将铃声音量静音

Android JetCreator 深度复制错误

Android - 如果没有空间,请向下移动按钮

android - Webview 无法正确呈现 css - Kitkat 版本 4.4.2

java - 为什么只有使用反射才能使用此 Java 方法?

android - 有没有办法使用外部配置文件分发 Android 应用程序?

安卓+Phonegap+Admob

android - 在布局中频繁填充 listView