我正在创建一个允许用户在表面上绘图的Android应用程序。不幸的是,我的应用程序中画布的最大图像尺寸是我手机上的1000 x 1500像素,并且根据设备尺寸进行缩放,但不会更大。
我的目标是允许用户在应用中绘图,但是绘图包含在各种较大的画布中,例如4032 x 3264像素的画布。如何为我想要绘制的位图覆盖设备的最大画布尺寸?绘图过程是否应该在服务器上进行,并且渲染的图像是否应该实时显示在客户端上?或者我可以以某种方式使画布大于屏幕,初始化图像的完全缩小视图,并允许用户滚动它,放大/缩小等,同时还能够绘制?
我已经向我在这里创建的课程发布了公开要点:
https://gist.github.com/santafebound/57f45c636e69c53b1df79302b8946099
只需更改生成图像的大小即会产生以下错误:
09-17 11:29:03.718 4490-4490/com.elgami.customizer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elgami.customizer, PID: 4490
java.lang.OutOfMemoryError: Failed to allocate a 52641804 byte allocation with 16777216 free bytes and 44MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:831)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
at com.elgami.customizer.MainActivity.SetupTemplate(MainActivity.java:451)
at com.elgami.customizer.MainActivity.SetupTemplate(MainActivity.java:434)
at com.elgami.customizer.MainActivity$1.onPageScrollStateChanged(MainActivity.java:289)
at android.support.v4.view.ViewPager.dispatchOnScrollStateChanged(ViewPager.java:1944)
at android.support.v4.view.ViewPager.setScrollState(ViewPager.java:497)
at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2119)
at com.elgami.views.ExtendedViewPager.onInterceptTouchEvent(ExtendedViewPager.java:44)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2771)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9520)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at andr
答案 0 :(得分:-1)
尝试对Canvas进行子类化并使用固定的MeasureSpec来锁定所需的大小:
private static final int fixedWidthMeasureSpec
= View.MeasureSpec.makeMeasureSpec(4032, View.MeasureSpec.EXACTLY);
private static final int fixedHeightMeasureSpec
= View.MeasureSpec.makeMeasureSpec(3264, View.MeasureSpec.EXACTLY);
// ...
@Override
protected void onMeasure(int widthMS, int heightMS) {
super.onMeasure(fixedWidthMeasureSpec, fixedHeightMeasureSpec);
}