Android începător: Setarea Wallpaper

Sunt un incepator asa ca te rog purtati cu mine. Am creat o aplicație care vă permite să parcurgeți o selecție de imagini de fundal și să selectați una. Totul funcționează perfect, dar Android doar stabilește colțul din stânga jos ca tapet ... dimensiunile tapetului sunt de 640 x 960.

aici este codul meu:

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);                
    }
}
1

2 răspunsuri

 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();
    }         
}

în AndroidManifest.xml


1
adăugat
Încă mai primesc aceeași eroare ... sunt dimensiunile imaginii?
adăugat autor drew, sursa

includeți acest lucru în Xml-ul dvs. de imagine:

android:scaleType="fitXY" or "fitCenter" or "cropCenter" or any other options that you may like

0
adăugat
Android dezvoltatori, România — Moldova
Android dezvoltatori, România — Moldova
27 participanți

Parteneri: ciupacabra.com, @php_ro, @js_ro, @node_ro iOS: @ro_ios Reguli: https://github.com/js-ro/it-telegram/blob/master/RULES.md