gpt4 book ai didi

android - 更改android中的像素颜色

转载 作者:行者123 更新时间:2023-11-29 14:27:41 25 4
gpt4 key购买 nike

testButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v)
{
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);for (int i = 0; i < mutableBitmap.getWidth(); i++)
{
for (int j = 0; j < mutableBitmap.getHeight(); j++)
{
int pixel = mutableBitmap.getPixel(i, j);
// get red color value
int red = Color.red(pixel);
int color = Color.argb(0xFF, red, 0, 0);
mutableBitmap.setPixel(i, j, color);
imageView.setImageBitmap(mutableBitmap);
}
}
}


});

我正在尝试仅使用红色值更改像素颜色。因此,我从给定的像素中获取红色值,然后尝试仅用该红色值替换相同的像素。该程序运行成功,但是当我单击按钮时它崩溃了。谁能告诉我我做错了什么

LOGCAT 错误

12-03 15:16:57.228: E/AndroidRuntime(5103): FATAL EXCEPTION: main
12-03 15:16:57.228: E/AndroidRuntime(5103): java.lang.IllegalStateException
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.graphics.Bitmap.setPixel(Bitmap.java:1002)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.example.imaging.AndroidCamera$7.onClick(AndroidCamera.java:216)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.view.View.performClick(View.java:3511)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.view.View$PerformClick.run(View.java:14109)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Handler.handleCallback(Handler.java:605)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Looper.loop(Looper.java:137)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-03 15:16:57.228: E/AndroidRuntime(5103): at java.lang.reflect.Method.invokeNative(Native Method
12-03 15:16:57.228: E/AndroidRuntime(5103): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-03 15:16:57.228: E/AndroidRuntime(5103): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您正在尝试修改不可变位图的像素。您无法修改不可变位图的像素。如果您尝试,它将抛出 IllegalStateException。

使用下面的方法从资源中获取可变位图

public static Bitmap getMutableBitmap(Resources resources,int resId) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
return BitmapFactory.decodeResource(resources, resId, options);
}

或使用

    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

从不可变位图中获取可变位图

关于android - 更改android中的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20347591/

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