WallpaperManager无法调整大小,并且会拉伸

时间:2019-01-07 17:56:24

标签: resize wallpaper wallpapermanager

我有以下代码很完美,通过单击按钮,可以放置完美的墙纸,细节是将其拉伸。

如果我在android studio的模拟器上尝试完美运行,但如果在2部我拥有的手机(Sony Xpiria C2105和Samsung galaxy S3)上尝试,则壁纸会拉伸,尽管它可以很好地检测到Width和高度(以像素为单位)。

我真的不知道为什么会发生这种情况,我需要专家来查看我的失败之处。首先,谢谢。

Java:

    public class infoanimales extends AppCompatActivity {

    private RequestOptions options;
    TextView txtclose;
    LinearLayout img;
    public ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_infoanimales);

        Button btnSetWallpaper = (Button) findViewById(R.id.btn);

        btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SetWallpaperTask().execute();
            }
        });

        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        this.options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        String image_url = getIntent().getExtras().getString("img2");

        img = findViewById(R.id.thumbnail2);
        Glide.with(this).load(image_url).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable fondoreceta, Transition<? super Drawable> transition) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    img.setBackground(fondoreceta);
                }
            }
        });

        TextView txtclose = findViewById(R.id.txtclose);
        txtclose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }

    public class SetWallpaperTask extends AsyncTask<String, Void, Bitmap> {

        String image_url = getIntent().getExtras().getString("img2");

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap result= null;

            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
            Log.e("Width", "" + width);
            Log.e("height", "" + height);

            try {
                result = Picasso.get()
                        .load(image_url)
                        .resize(width, height)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return result;
        }

        @Override
        protected void onPostExecute (Bitmap result) {
            super.onPostExecute(result);

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
            try {
                wallpaperManager.setBitmap(result);

                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.wallyes), Toast.LENGTH_SHORT).show();
                finish();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void onPreExecute () {
            super.onPreExecute();

            progressDialog = new ProgressDialog(infoanimales.this);
            progressDialog.setMessage(getString(R.string.proceso));
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    }
}

Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/thumbnail2"
    android:padding="5dp">

    <TextView
        android:id="@+id/txtclose"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="end"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/circulo"
        android:gravity="center"
        android:text="@string/equis"
        android:textColor="@android:color/background_light"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end">

    <ImageView
        android:id="@+id/wall"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@string/app_name"/>

        <Button
                android:id="@+id/btn"
                android:layout_width="159dp"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center"
                android:layout_marginBottom="25dp"
                android:background="@drawable/borde_redondo"
                android:text="Establecer como Fondo de Pantalla"
                android:textColor="#ffffff" />

</FrameLayout>
    </LinearLayout>
</LinearLayout>

0 个答案:

没有答案