位图到壁纸调整大小比例 setWallpaper

发布于 2024-12-09 13:42:01 字数 1068 浏览 0 评论 0原文

我想缩放壁纸集的位图但没有效果:|我有这个代码以及原始文件夹和数组中的所有 .jpg 文件,代码:

baton3.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        InputStream tapeta = getResources().openRawResource(textureArrayWin[n]);

        Bitmap bitmap = BitmapFactory.decodeStream(tapeta);
        Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();
        int height = display.getHeight();

        int oldwidth= bitmap.getWidth();
        int oldheight= bitmap.getHeight();
        float skalaszerokosci = ((float) oldwidth) / width;
        float skalawysokosci = ((float)oldheight) / height;

        Matrix macierz = new Matrix();
        macierz.postScale(skalaszerokosci, skalawysokosci);

        Bitmap zmieniona = Bitmap.createBitmap(bitmap, 0, 0, 
                width, height, macierz, true); 


        try
        {

            getApplicationContext().setWallpaper(zmieniona);


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


    }

});

I want to scale Bitmap for Wallpaper set but no effect:| I have this code and all .jpg files in raw folder and array, code:

baton3.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        InputStream tapeta = getResources().openRawResource(textureArrayWin[n]);

        Bitmap bitmap = BitmapFactory.decodeStream(tapeta);
        Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();
        int height = display.getHeight();

        int oldwidth= bitmap.getWidth();
        int oldheight= bitmap.getHeight();
        float skalaszerokosci = ((float) oldwidth) / width;
        float skalawysokosci = ((float)oldheight) / height;

        Matrix macierz = new Matrix();
        macierz.postScale(skalaszerokosci, skalawysokosci);

        Bitmap zmieniona = Bitmap.createBitmap(bitmap, 0, 0, 
                width, height, macierz, true); 


        try
        {

            getApplicationContext().setWallpaper(zmieniona);


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


    }

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-12-16 13:42:01

添加到清单
;

add to manifest
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />

今天小雨转甜 2024-12-16 13:42:01

代码如下:

    // Get display dimensions
    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    final int displayWidth = metrics.widthPixels;
    final int displayHeight = metrics.heightPixels;

    // Here I'm decoding a resource, just for the example sake
    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);

    // obtain the original Bitmap's dimensions
    final int originalWidth = bitmap.getWidth();
    final int originalHeight = bitmap.getHeight();

    // Obtain the horizontal and vertical scale factors
    final float horizontalScaleFactor = (float) originalWidth / (float) displayWidth;
    final float verticalScaleFactor = (float) originalHeight / (float) displayHeight;

    // Get the biggest scale factor to use in order to maintain original image's aspect ratio 
    final float scaleFactor = Math.max(verticalScaleFactor, horizontalScaleFactor);
    final int finalWidth = (int) (originalWidth / scaleFactor);
    final int finalHeight = (int) (originalHeight / scaleFactor);

     // Create the final bitmap
    final Bitmap wallpaperBmp = Bitmap.createScaledBitmap(
            bitmap, finalWidth, finalHeight, true);

    // Recycle the original bitmap
    bitmap.recycle();

    // Finally set it as wallpaper
    try {
        final WallpaperManager wallMan = WallpaperManager.getInstance(this);
        wallMan.setBitmap(wallpaperBmp);
    } catch (IOException e) {
        Log.e("Wallpaper", "Something went wrong: " + e.toString());
        wallpaperBmp.recycle();
    }

另外,不要忘记将 SET_WALLPAPER 添加到您的 manifest.xml 中:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

Here's the code:

    // Get display dimensions
    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    final int displayWidth = metrics.widthPixels;
    final int displayHeight = metrics.heightPixels;

    // Here I'm decoding a resource, just for the example sake
    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);

    // obtain the original Bitmap's dimensions
    final int originalWidth = bitmap.getWidth();
    final int originalHeight = bitmap.getHeight();

    // Obtain the horizontal and vertical scale factors
    final float horizontalScaleFactor = (float) originalWidth / (float) displayWidth;
    final float verticalScaleFactor = (float) originalHeight / (float) displayHeight;

    // Get the biggest scale factor to use in order to maintain original image's aspect ratio 
    final float scaleFactor = Math.max(verticalScaleFactor, horizontalScaleFactor);
    final int finalWidth = (int) (originalWidth / scaleFactor);
    final int finalHeight = (int) (originalHeight / scaleFactor);

     // Create the final bitmap
    final Bitmap wallpaperBmp = Bitmap.createScaledBitmap(
            bitmap, finalWidth, finalHeight, true);

    // Recycle the original bitmap
    bitmap.recycle();

    // Finally set it as wallpaper
    try {
        final WallpaperManager wallMan = WallpaperManager.getInstance(this);
        wallMan.setBitmap(wallpaperBmp);
    } catch (IOException e) {
        Log.e("Wallpaper", "Something went wrong: " + e.toString());
        wallpaperBmp.recycle();
    }

Also, don't forget to add the SET_WALLPAPER to your manifest.xml:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文