2012-05-31 4 views
-1

Je suis un débutant alors s'il vous plaît ours avec moi. J'ai créé une application qui vous permet de faire défiler un choix de fonds d'écran et en sélectionner un. Tout fonctionne parfaitement, mais Android ne fixe que le coin inférieur gauche comme fond d'écran ... les dimensions du papier peint sont 640 x 960.Android Débutant: Réglage du papier peint

voici mon code:

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

Répondre

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

dans AndroidManifest.xml

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

Je reçois toujours la même erreur ... est-ce les dimensions de l'image? – drew

0

inclure dans le Xml de votre imageView:

android:scaleType="fitXY" ou "fitCenter" ou "cropCenter" ou d'autres options que vous pourriez aimer

Questions connexes