将布局下载为图像时出现图像质量问题

时间:2018-06-20 06:33:24

标签: android image

我的要求是下载图像,效果很好,但是下载为图像后图像质量不好

我在图像中显示Textview,但图像中的质量无法保持

我对保持图像质量一无所知,请建议我

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".PreviewActivity">

<RelativeLayout
    android:id="@+id/lnimg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:foreground="@drawable/scrim"
    android:layout_centerInParent="true">

    <ImageView
        android:id="@+id/imgquote"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"/>

    <View
        android:id="@+id/viewpreview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/scrim"
        android:visibility="visible"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/quote"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:paddingTop="@dimen/album_title_padding"
            android:gravity="center_horizontal"
            android:textColor="@color/white"
            android:text="Quotes"
            android:typeface="serif"
            android:textSize="@dimen/_15sdp" />

        <TextView
            android:id="@+id/author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:padding="@dimen/album_title_padding"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginBottom="@dimen/_5sdp"
            android:text=" -Author"
            android:textColor="#80EFEFEF"
            android:textSize="@dimen/_15sdp" />

    </LinearLayout>


</RelativeLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/_50sdp"
    android:layout_alignParentBottom="true"
    android:background="#50585757"
    android:layout_alignParentRight="true">

<LinearLayout
    android:id="@+id/lnsetwallpaper"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <ImageView
        android:id="@+id/imgsetwallpaper"
        android:layout_width="@dimen/_40sdp"
        android:layout_height="@dimen/_40sdp"
        android:layout_margin="@dimen/_5sdp"
        android:padding="@dimen/_5sdp"
        android:clickable="true"
        android:src="@drawable/ic_done"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Wallpaper"
        android:layout_gravity="center"
        android:textColor="@color/white"
        android:textSize="@dimen/_15sdp"/>
</LinearLayout>


<LinearLayout
    android:id="@+id/lndownload"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <ImageView
        android:id="@+id/imgsave"
        android:layout_width="@dimen/_40sdp"
        android:layout_height="@dimen/_40sdp"
        android:layout_margin="@dimen/_5sdp"
        android:padding="@dimen/_5sdp"
        android:clickable="true"
        android:src="@drawable/downloadnew"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Download"
        android:layout_gravity="center"
        android:textColor="@color/white"
        android:textSize="@dimen/_15sdp"/>
</LinearLayout>


</LinearLayout>


<ProgressBar
    android:visibility="gone"
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />

</RelativeLayout>

这是我的代码 java文件

lndownload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            lnimg.setDrawingCacheEnabled(true);
            lnimg.buildDrawingCache();
            Bitmap bitmap = lnimg.getDrawingCache();

            saveImageToExternalStorage(bitmap);


        }
    });

 private void saveImageToExternalStorage(Bitmap finalBitmap) {
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myDir = new File(root + "/Inspiquo/Inspiquo Images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    MediaScannerConnection.scanFile(PreviewActivity.this, new String[]{file.toString()}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                }
            });

    Toast.makeText(PreviewActivity.this, "Saved to Gallery", Toast.LENGTH_SHORT).show();
}

请帮助我处理图像质量

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

我建议您不要使用getDrawingCache

尝试以下方法。

document.getElementById('YourID').style.display="none" 

答案 1 :(得分:1)

您正在压缩位图,请删除此行

finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)