Android壁纸应用程序 - 调整图像的可滚动背景

时间:2017-07-08 22:20:57

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

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

includes = tagsI.map(tag => `'${tag}'`).join(',')

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

1 个答案:

答案 0 :(得分:0)

我的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>

希望这会有所帮助。