【问题标题】:Android Wallpaper App - Resizing Images for scrollable backgroundAndroid 壁纸应用程序 - 为可滚动背景调整图像大小
【发布时间】:2017-07-08 22:20:56
【问题描述】:

我有一个壁纸应用程序,它将选定的图像设置为手机菜单的背景。它是可滚动的(默认情况下),但问题是背景只是原始图像的剪切。这是我将图像设置为背景的代码:

private Bitmap image = [...];

[...]

private void setBackgroundImage(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
        wallpaperManager.setBitmap(pic);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我现在的问题是:如何确定图片的正确尺寸,以便每部手机都将整张图片作为背景图片,而不仅仅是剪切?

【问题讨论】:

  • 你是如何得到你设置到壁纸管理器的位图的?
  • plz,你可以试试我的帖子。请

标签: android image background-image image-resizing android-wallpaper


【解决方案1】:

我的 java 文件:像这样

public class MainActivity extends AppCompatActivity {

    Button button;
    ImageView imageView;
    WallpaperManager wallpaperManager ;
    Bitmap bitmap1, bitmap2 ;
    DisplayMetrics displayMetrics ;
    int width, height;
    BitmapDrawable bitmapDrawable ;

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

        button = (Button)findViewById(R.id.button);

        imageView = (ImageView)findViewById(R.id.imageView);

        wallpaperManager  = WallpaperManager.getInstance(getApplicationContext());

        bitmapDrawable = (BitmapDrawable) imageView.getDrawable();

        bitmap1 = bitmapDrawable.getBitmap();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                GetScreenWidthHeight();

                SetBitmapSize();

                wallpaperManager = WallpaperManager.getInstance(MainActivity.this);

                try {

                    wallpaperManager.setBitmap(bitmap2);

                    wallpaperManager.suggestDesiredDimensions(width, height);


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

    public void GetScreenWidthHeight(){

        displayMetrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        width = displayMetrics.widthPixels;

        height = displayMetrics.heightPixels;

        Toast.makeText(MainActivity.this,"width:"+width+", height:"+height,Toast.LENGTH_SHORT).show();

    }

    public void SetBitmapSize(){

        bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);

    }
}

还有我的布局文件。像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/sample_image_wallpaper"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click here to Set this image as Wallpaper in android programmatically"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="94dp" />

</RelativeLayout>

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多