-1

我是初学者,所以请多多包涵。我创建了一个应用程序,允许您滚动浏览一系列壁纸并选择一个。一切都很完美,但android只将左下角设置为壁纸......壁纸尺寸为640 x 960。

这是我的代码:

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Wallpapers extends Activity implements OnClickListener{

    Button preview;
    Button setBackground;
    Button returnBack;

    ImageView display;
    ImageView wallpaper1;
    ImageView wallpaper2;
    ImageView wallpaper3;
    ImageView wallpaper4;

    LinearLayout backgroundPreview;
    private int image = R.drawable.wallpaper1;
    HorizontalScrollView scrollView;

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

        preview= (Button)findViewById(R.id.wallpaperPreview);
        setBackground = (Button)findViewById(R.id.wallpaperSet);
        returnBack = (Button)findViewById(R.id.wallpaperReturn);
            preview.setOnClickListener(this);
            setBackground.setOnClickListener(this);
            returnBack.setOnClickListener(this);

        display=(ImageView)findViewById(R.id.wallpaperDisplay);
        wallpaper1=(ImageView)findViewById(R.id.wallpaper1);
        wallpaper2=(ImageView)findViewById(R.id.wallpaper2);
        wallpaper3=(ImageView)findViewById(R.id.wallpaper3);
        wallpaper4=(ImageView)findViewById(R.id.wallpaper4);
            wallpaper1.setOnClickListener(this);
            wallpaper2.setOnClickListener(this);
            wallpaper3.setOnClickListener(this);
            wallpaper4.setOnClickListener(this);

        backgroundPreview = (LinearLayout)findViewById(R.id.wallpaperActivityLayout);
        scrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);

    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.wallpaperPreview:
            previewBackground(image);
            break;
        case R.id.wallpaperSet:
            new AlertDialog.Builder(this)
                .setMessage("Would you like to set this image as your wallpaper?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {           
                    public void onClick(DialogInterface dialog, int which) {
                        InputStream is = getResources().openRawResource(image);
                        Bitmap wallpaper = BitmapFactory.decodeStream(is);

                        try{
                            getApplicationContext().setWallpaper(wallpaper);
                        }catch(IOException e){
                            e.printStackTrace();
                        }
                    }
                })
                .setNegativeButton("No", null)
                .show();

            break;
        case R.id.wallpaperReturn:
            reverse();
            break;
        case R.id.wallpaper1:
            display.setImageResource(R.drawable.wallpaper1);
            image = R.drawable.wallpaper1;
            break;
        case R.id.wallpaper2:
            display.setImageResource(R.drawable.wallpaper2);
            image = R.drawable.wallpaper2;
            break;
        case R.id.wallpaper3:
            display.setImageResource(R.drawable.wallpaper3);
            image = R.drawable.wallpaper3;
            break;
        case R.id.wallpaper4:
            display.setImageResource(R.drawable.wallpaper4);
            image = R.drawable.wallpaper4;
            break;
        }

    }

    private void previewBackground(int image) {
        backgroundPreview.setBackgroundResource(image);
        setBackground.setVisibility(View.GONE);
        preview.setVisibility(View.GONE);

        display.setVisibility(View.GONE);
        scrollView.setVisibility(View.GONE);

        returnBack.setVisibility(View.VISIBLE);             
    }

    private void reverse() {
        backgroundPreview.setBackgroundResource(android.R.color.black);
        setBackground.setVisibility(View.VISIBLE);
        preview.setVisibility(View.VISIBLE);

        display.setVisibility(View.VISIBLE);
        scrollView.setVisibility(View.VISIBLE);

        returnBack.setVisibility(View.GONE);                
    }
}
4

2 回答 2

1
 public void SetBackground(int Url) {

    try {
        File file = new File("/sdcard/sampleimage");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
        bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
        Context context = this.getBaseContext();
        context.setWallpaper(bitmap);            
        Toast.makeText(getApplicationContext(), "Wallpaper has been set",             Toast.LENGTH_SHORT).show();            
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }         
}

在 AndroidManifest.xml 中

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
于 2012-05-31T06:14:29.367 回答
0

将此包含在您的 imageView 的 Xml 中:

android:scaleType="fitXY""fitCenter""cropCenter"或您可能喜欢的任何其他选项

于 2017-11-18T14:02:38.870 回答