gpt4 book ai didi

android - 重启智能手机后放大我创建的墙纸

转载 作者:行者123 更新时间:2023-11-30 02:51:52 25 4
gpt4 key购买 nike

我的墙纸有问题。当我创建它并重置我的手机时,墙纸会放大。我如何更改 BootReceiver 类以正确加载墙纸和屏幕的位置?

主要 Activity :

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

public static int toPhone;

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

ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);

Context context;
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void onClick(View view) {
Intent intent = new Intent(this, Podglad.class);
switch (view.getId()) {
case R.id.IVimage1:
toPhone = R.drawable.kwiaty;
startActivity(intent);
break;
case R.id.IVimage2:
toPhone = R.drawable.listek;
startActivity(intent);
break;
case R.id.IVimage3:
toPhone = R.drawable.kwiaty;
startActivity(intent);
break;
}
}
}

波德高兴:

public class Podglad extends ActionBarActivity {
final Context context = getApplicationContext();

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

final Intent intent = new Intent();
View myView = this.findViewById(R.id.a);
myView.setBackgroundResource(MainActivity.toPhone);
Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
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(), MainActivity.toPhone);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(Podglad.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.podglad, menu);
return true;
}
}

启动接收器:

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

最佳答案

您可以注册一个BroadcastReceiver对于“android.intent.action.BOOT_COMPLETED”并重做您为让它在第一时间工作所做的一切。

或者您可以调整图像大小并裁剪以适合所需的尺寸,这样当设备重新启动时它会加载一个已经完美尺寸的图像。

可能还有其他解决方案,但首先想到的是这两个。


编辑,这是我在关于使用一种方法的评论中的意思的示例:

public class WallpaperUtil {
public static setWallpaper(Context context) {
try {
SharedPreferences sharedPreferences = context
.getSharedPreferences("wallpaperapp",0);
...
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
...
} catch(Exception e) {
Log.e(TAG,e.toString());
}
}
}

public class BootReceiver extends BroadcastReceiver {

@Override public void onReceive(Context context,Intent intent) {
WallpaperUtil.setWallpaper(getApplicationContext);
}
}

public class SetWallpaperActivity extends Activity
implements View.OnClickListener {
...
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonSetWallpaper:
// save choice to SharedPreferences then
WallpaperUtil.setWallpaper(this);
break;
default:
break;
}
}
}

关于android - 重启智能手机后放大我创建的墙纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com