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

时间:2012-06-08 13:46:49

标签: android

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

main.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

1 个答案:

答案 0 :(得分:2)

快速查看问题。你可以多次保存jpg,并且在(有损)压缩期间它会降级越来越多。

我建议您改用png

相关问题