android如何设置颜色资源为壁纸?
How to set color resource as a wallpaper in android?
我想在 android 中设置纯色作为墙纸,但是当我点击按钮时,不幸的是应用程序停止了。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
try {
wm.setResource(R.color.red);
Toast.makeText(getApplicationContext(), "Wallpaper changed", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
这里是colors.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff4366</color>
</resources>
Logcat:
10-24 15:59:26.216 12529-12529/com.danish.testapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
10-24 15:59:26.216 12529-12529/com.danish.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.danish.testapp, PID: 12529
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
at android.content.res.Resources.openRawResource(Resources.java:1271)
at android.content.res.Resources.openRawResource(Resources.java:1241)
at android.app.WallpaperManager.setResource(WallpaperManager.java:739)
at com.danish.testapp.MainActivity.onClick(MainActivity.java:30)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我还在清单中设置了所需的权限。
对于 WallpaperManager,您必须设置 drawable。
wm.setResource(R.drawable.your_drawable);
已编辑
还必须设置权限。
android.permission.SET_WALLPAPER
对于可绘制对象的设置颜色,您可以使用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/your_coloe" />
</shape>
</item>
</selector>
已编辑
最终解决:
我正在写一个示例。有效。享受吧。
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WallpaperManager wm = WallpaperManager.getInstance(this);
Drawable drawable = getResources().getDrawable(R.drawable.mydrawable);
Bitmap bitmap = drawableToBitmap(drawable);
Log.i(getClass().getName(), "bitmap = " +bitmap);
try {
wm.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Wallpaper changed",
Toast.LENGTH_LONG).show();
}
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
我想在 android 中设置纯色作为墙纸,但是当我点击按钮时,不幸的是应用程序停止了。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
try {
wm.setResource(R.color.red);
Toast.makeText(getApplicationContext(), "Wallpaper changed", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
这里是colors.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff4366</color>
</resources>
Logcat:
10-24 15:59:26.216 12529-12529/com.danish.testapp D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
10-24 15:59:26.216 12529-12529/com.danish.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.danish.testapp, PID: 12529
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
at android.content.res.Resources.openRawResource(Resources.java:1271)
at android.content.res.Resources.openRawResource(Resources.java:1241)
at android.app.WallpaperManager.setResource(WallpaperManager.java:739)
at com.danish.testapp.MainActivity.onClick(MainActivity.java:30)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我还在清单中设置了所需的权限。
对于 WallpaperManager,您必须设置 drawable。
wm.setResource(R.drawable.your_drawable);
已编辑
还必须设置权限。
android.permission.SET_WALLPAPER
对于可绘制对象的设置颜色,您可以使用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/your_coloe" />
</shape>
</item>
</selector>
已编辑
最终解决:
我正在写一个示例。有效。享受吧。
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WallpaperManager wm = WallpaperManager.getInstance(this);
Drawable drawable = getResources().getDrawable(R.drawable.mydrawable);
Bitmap bitmap = drawableToBitmap(drawable);
Log.i(getClass().getName(), "bitmap = " +bitmap);
try {
wm.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Wallpaper changed",
Toast.LENGTH_LONG).show();
}
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}