如何使用广播接收器使壁纸适合屏幕?

时间:2014-09-16 10:50:44

标签: java android eclipse broadcastreceiver

我正在制作一个壁纸应用程序,你有一堆照片,你选择成为你的手机壁纸,一切都很好,除非我重新启动手机壁纸放大,我无法解决它,因为我可以'让它选择用户选择的照片我能做的就是让它只适用于第一张照片,但我不能为别人做,有人可以告诉我该怎么办。

这是我尝试过的: 我的主要活动:

 static int tophone;
 ImageView display;
 public static Integer[] tophone2 = {
R.drawable.iv1,R.drawable.iv2,R.drawable.iv3,R.drawable.iv4 };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

tophone = R.drawable.iv1;
display = (ImageView) findViewById(R.id.IView);
ImageView image1 = (ImageView) findViewById(R.id.iv1);
ImageView image2 = (ImageView) findViewById(R.id.iv2);
ImageView image3 = (ImageView) findViewById(R.id.iv3);
ImageView image4 = (ImageView) findViewById(R.id.iv4);
Button setWp = (Button) findViewById(R.id.setWp);

Picasso.with(MainActivity.this).load(R.drawable.iv1_s).into(image1);
Picasso.with(MainActivity.this).load(R.drawable.iv2_s).into(image2);
Picasso.with(MainActivity.this).load(R.drawable.iv3_s).into(image3);
Picasso.with(MainActivity.this).load(R.drawable.iv4_s).into(image4);


image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
setWp.setOnClickListener(this);
}

 @Override
 public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.iv1:
    display.setImageResource(R.drawable.iv1);
    tophone = tophone2[0];
    break;
case R.id.iv2:
    display.setImageResource(R.drawable.iv2);
    tophone = tophone2[1];
    break;
case R.id.iv3:
    display.setImageResource(R.drawable.iv3);
    tophone = tophone2[2];
    break;
case R.id.iv4:
    display.setImageResource(R.drawable.iv4);
    tophone = tophone2[3];
    break;
case R.id.setWp:
    Toast WpSet = Toast.makeText(MainActivity.this,"Wallpaper Set", Toast.LENGTH_SHORT);

    SharedPreferences       sharedPreferences = getSharedPreferences("wallpaperapp",0);
    sharedPreferences.edit().putInt("position",0).commit();

    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = metrics.heightPixels; 
    int width = metrics.widthPixels;
    Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), tophone);
    Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
    wallpaperManager.setWallpaperOffsetSteps(1, 1);
    wallpaperManager.suggestDesiredDimensions(width, height);
    try {
      wallpaperManager.setBitmap(bitmap);
      } catch (IOException e) {
      e.printStackTrace();
    }
    WpSet.show();
    break;
 }



 }

 }

和我的广播接收者:

 public class BootReceiver extends BroadcastReceiver {
 private static final String TAG="BootReceiver";

@Override public void onReceive(Context context,Intent intent){
try{ 
SharedPreferences       sharedPreferences =   context.getSharedPreferences("wallpaperapp",0);
int position= sharedPreferences.getInt("position", 0);
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;
            Bitmap tempbitMap =  BitmapFactory.decodeResource(context.getResources(),MainActivity.tophone2[position]);
                Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
              wallpaperManager.setBitmap(bitmap);
              } catch (IOException e) {
              e.printStackTrace();
            }
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}

0 个答案:

没有答案