gpt4 book ai didi

android - 尝试绘制用户加载的位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:20 26 4
gpt4 key购买 nike

我正在尝试绘制用户加载的位图。这是在 onCreate 中。

imageView = (ImageView) this.findViewById(R.id.ImageView);

Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(bitmap);
imageView.setOnTouchListener(this);

下一段代码是当用户按下按钮获取位图并成功检索位图后。这是在结束“onActivityResult

canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.setOnTouchListener(this);

我可以在 onCreate 期间创建的空白位图上绘制,但是当用户加载新位图并尝试绘制时,它不显示。新位图已加载。

我也累了

canvas = new Canvas(BitmapFactory.decodeFile(picturePath));

然后它给了我错误。

有人知道我做错了什么吗?

编辑:这是日志文件:

10-25 03:19:31.409: W/System.err(1971): java.lang.Exception: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
10-25 03:19:31.409: W/System.err(1971): at com.example.milestone2.Draw.onActivityResult(Draw.java:148)
10-25 03:19:31.409: W/System.err(1971): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
10-25 03:19:31.419: W/System.err(1971): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
10-25 03:19:31.419: W/System.err(1971): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 03:19:31.419: W/System.err(1971): at android.os.Looper.loop(Looper.java:137)
10-25 03:19:31.428: W/System.err(1971): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 03:19:31.428: W/System.err(1971): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 03:19:31.428: W/System.err(1971): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 03:19:31.428: W/System.err(1971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 03:19:31.428: W/System.err(1971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 03:19:31.428: W/System.err(1971): at dalvik.system.NativeStart.main(Native Method)
10-25 03:19:31.438: W/System.err(1971): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
10-25 03:19:31.448: W/System.err(1971): at android.graphics.Canvas.<init>(Canvas.java:127)
10-25 03:19:31.469: W/System.err(1971): at com.example.milestone2.Draw.onActivityResult(Draw.java:141)
10-25 03:19:31.469: W/System.err(1971): ... 13 more
10-25 03:19:31.588: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

最佳答案

您需要设置返回的位图作为 Canvas 的位图。但是 BitmapFactory.decodeFile(picturePath) 返回一个不可变的位图。将其转换为可变位图,然后将其设置为 Canvas 。

试试这个:

Bitmap loadedBitmap = BitmapFactory.decodeFile(picturePath);
Bitmap drawableBitmap = loadedBitmap.copy(Bitmap.Config.ARGB_8888, true);

canvas = new Canvas(drawableBitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(drawableBitmap);
imageView.setOnTouchListener(this);

关于android - 尝试绘制用户加载的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19580293/

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