Mirror a fragment to a presentation without resizing it - android

This topic is a following of my last one: How to mirror a fragment to a presentation in Android?
I managed to duplicate my fragment on the external display by using the library "Presentation" from CommonsWare Android Components (CWAC).
However, it resizes the initial fragment to optimize the user experience of the presentation (it is better displayed on the external screen than the device) but in my case, I would like the opposite.
Therefor, I searched in the library where it resizes the original fragment, and it appears it's in the class AspectLockedFrameLayout.java and more precisely, in the method void onMeasure(int widthSpec, int heightSpec).
I tried to not modify lockedWidth and lockedHeight (by putting them in commentary) and it seems to work: the initial fragment is not resized and the displaying suits me.
However, the app crashes when I connect the external display before having launched the app and it's a bit annoying.
After a lot of tests, I found that in the class MirroringFrameLayout.java and in the method public void draw(Canvas canvas), the variable bmp is null and then, crash.
Do you know where is the problem and how to solve it?
Thanks a lot,
Best regards.

I cannot readily debug code that I cannot see.
A quick glance at the current MirroringFrameLayout indicates that your problem is coming because draw() is being triggered without a Bitmap having been created in initBitmap(). That could be because there is no mirror, or because draw() is getting called before any of the following methods are called:
onSizeChanged()
onPreDraw()
onScrollChanged()
If you are able to reproduce this problem in umodified versions of the library, file an issue with a project that demonstrates the issue (or other instructions for reproducing it), and I can try to address it.

Related

How can I tell, in onDraw, whether or not I'm inside Android Studio?

I have a custom view that renders part of itself (pink area) in onDraw. It does this, via a class instance set via adapter/binding. I have no problem with the 117 and details controls at design time. However, this pink area is resulting in a call to onDraw within Android Studio.
Everything's ok at runtime. However, I am getting (and rightfully so) an Android Studio error message - because the class that I use to do the drawing hasn't been instantiated.
So, my question is:
Can I detect, in code, whether or not the app is running (rendering really) inside AS (as opposed to having been deployed to an emulator or device)?**
It might be something like if (BuildConfig.DEBUG), or perhaps something [entirely] different. (There's certainly nothing [else] in the BuildDebug instance that'll help.)
Ultimately, I see my current
override fun onDraw(canvas: Canvas) {
:
:
puzzle.doSomething
replaced with something like:
if (!INSIDE_ANDROID_STUDIO)
puzzle.doSomething
I spent over an hour searching online. With no luck. It's hard to ask "How do I know if I'm running in the Android Studio IDE?" and not get a bazillion links concerning:
Why isn't Android Studio running [correctly]
How to tell if another app is running
How to tell if your app is in the fore or background
Why this or that isn't rendering correctly
But nothing on point. I did numerous searches of "UninitializedPropertyAccessException" and "Render problem" as well.
You can do it by checking what returns the isInEditMode() method. If true your view is displayed in AS or other tool, otherwise - it`s on a device.
Indicates whether this View is currently in edit mode. A View is usually in edit mode when displayed within a developer tool. For instance, if this View is being drawn by a visual user interface builder, this method should return true.
https://developer.android.com/reference/android/view/View#isInEditMode()

Rendering issues: The following classes cannot be instantiated

I am converting a Eclipse project to work in Android Studio and have got all issues fixed except some layout xmls are showing the following issue in the drag and drop view
Rendering issues: The following classes cannot be instantiated
X.X.X.myclass.
I checked my class and it seems ok, i changed the latest API i have from the little dropdown; 22 (i guess this is the compile API). I havnt changed my Gradle setup, could it be something in there?
I havnt posted any code as im not sure what would be helpful - any ideas?
First of all: that's just a layout preview. You can edit the XML and carry on with your project and it won't affect anything on it.
The layout rendering thing literally runs your Java classes to create the preview with some mocked implementation or an actual android device, similar to testing mocked implementations.
So this message is just telling you that this mocked system failed to render the preview.
But if you really want to see the preview, you must check where in your class are you relying on variables, or objects that are inherent from your app that the mock system will not have access to.
An example, if your custom view does some special stuff during onLayout:
#Override onLayout(...){
int value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
that is a code that is calling to a static method on a separate class that is using the context (probably getting values from system resources or from the display manager) and this will not execute well in a mocked environment and the preview wouldn't be able to render it.
luckly it's an easy fix:
#Override onLayout(...){
int value;
if(isInEditMode()){ // that returns true if this code is being executed by AndroidStudio mocked system.
value = 0; // any value that makes OK for your custom class to properly show a preview
} else {
value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
}

Android Wear: Cannot Resolve Image Reference

Probably a stupid question here but I just started programming in Android so bear with me.
https://gist.github.com/gabrielemariotti/ca2d0a9f79b902b19a65
I'm following this project to implement a GridViewPager on my Android Wear application. It works fine, only the background images I'm trying to use are not displaying. It only displays a black screen.
The issue is this getBackground() method, the imageReference in particular.
public ImageReference getBackground(int row, int col) {
SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
return ImageReference.forDrawable(page.mBackgroundId);
}
The tutorial shows that an Image Reference should be returned however I am getting build errors saying that ImageReference cannot be resolved. So would I have to create a class or interface for ImageReference? Or do you set an attribute on the images themselves?
Can anyone shed any light on this?
Cheers in advance.
As of version 1.1 of the Wearable Support Library (which FragmentGridPagerAdapter is part of), the methods have changed quite a bit: they no longer use any sort of ImageReference class - the new method, getBackgroundForPage, simply returns a Drawable.

How to properly reset a scene in AndEngine GLES-2?

I am developing a game in Android using AndEngine GLES-2. I am facing a problem while resetting a scene after player has completed a level. When I reset the scene, all the sprites lose their positions and tend to appear in each other's positions i.e. they swap their positions.
I have tried all the things like setting all the sprites etc. to null and calling methods like clearUpdateHandlers() and clearEventModifiers() etc. but no success yet.
I found out after a lot of googling that engineOptions.getRenderOptions().disableExtensionVertexBufferObjects(); method can fix this problem. So I am trying to invoke it but compiler gives error saying that this method in not defined for RenderOptions class.
I checked the RenderOptions class in org.andengine.engine.options package and the method really does not exist in that class. Am I missing any plug-in or is there any other problem? Please help, I am stuck.
you need to manually restart the scene, for example:
To restart a Scene you can Finish the activity and Start again but different level with SharedPreferences, or Tag's in intents, or you can set position of each Sprite and cler the Scene with:
//detachChild this Sprites that you do not use
Scene.detachChild(SpriteX);
//clear the space of memory of each sprite that you do not use
SpriteX.dispose();
//unload the bitmaps that you do not use
BitMapsX.unload();
this method have a seconds to run, but you can use a elegant "hud" in you game, and while charging set in the hud a logo or animation with "loading", best regards

Memory Error whilst setting image following screen rotation on Mono for Android

Things were going well until I switched off the screen lock on my device, then things started going wrong intermittently.
I've managed to track the issue down and have some workarounds in mind BUT I would like to know if there is 'best practice' for avoiding or removing the issue.
The issue:
I have an application which changes images based on the application status.
The images are not huge but quite large (231k~) and are stored as resources.
After several screen rotations (I counted 27 with a project using a single ImageView), loading the images fails with Exception of type 'Java.Lang.OutOfMemoryError'
Stripped down to the barest project, the following demonstrates the problem:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
//get a reference to the ImageView
var imageView = FindViewById<ImageView>(Resource.Id.imageView1);
imageView.SetImageBitmap( Android.Graphics.BitmapFactory.DecodeResource( this.Resources, Resource.Drawable.Ready) );
}
The above code is the only method I used to reproduce the issue.
Whilst attempting to resolve, I extended the example so that imageView was released in OnDestry:
protected override void OnDestroy ()
{
base.OnDestroy ();
imageView.SetImageBitmap( null );
imageView.DestroyDrawingCache();
imageView.Dispose();
}
This made no difference unless I added GC.Collect() which I don't want to do.
The best workaround I've currently thought of so far would be to modify the code as follows:
static Bitmap _ready = null;
private Bitmap GetReadyImage {
get {
if (_ready == null) {
_ready = Android.Graphics.BitmapFactory.DecodeResource (this.Resources, Resource.Drawable.Ready);
}
return _ready;
}
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
//get a reference to the ImageView
imageView = FindViewById<ImageView>(Resource.Id.imageView1);
imageView.SetImageBitmap( GetReadyImage );
}
This relies upon a static reference to each Bitmap and a property accessor for each.
I could even write a method which stores the images in a static List to save writing property accessors for each different property/variable.
I could perhaps add the flags ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize |ConfigChanges.KeyboardHidden) but this would break the normal Activity lifecycle which I've read isn't best practice?
I find it strange that having scoured the web, I've not yest encountered similar issues or examples. I'm left wondering how most others deal with this?
Any thoughts or comments are much appreciated.
I can only approach this problem from a truly native perspective, as I have not worked directly with the Mono framework.
The described symptoms are 100% indicative of a memory leak in the Activity, but the code shows no real evidence. If you can truly produce the issue with a project containing only one Activity and those four lines of code, it sounds to me like it is perhaps a framework bug that ought to be filed with Xamarin. Have you attempted to create the same simple project in pure Java to see how the results fare on the same device/emulator you are using? It would also be interesting to know if the issue is localized to a specific version of Android. I have never seen this particular behavior before in a native application project.
The awkward part is your statement that forcing a garbage collection makes the problem go away. You're right, you shouldn't have to do that, but a true memory leak (i.e. an unreleased reference) would still persist even if you hit the GC several times. The Android paradigm of destroying and re-creating the Activity on each rotation is such that even if the old Activity lived for awhile, when memory was tight it (and all its references) would quickly be collected to make room for a new instance. If this is not happening, and each Activity is living on past even the system triggered GC passes, perhaps there is a stuck reference in the native code generated by Mono.
Interestingly enough, technically your workaround actually does introduce a true leak, by attaching the Bitmap to a static field that is never cleared. However, I agree that in comparison it seems like a more efficient move. A simpler workaround might also be to code your Activity to manually handle configuration changes (I don't know if Mono is different, but this is accomplished by adding android:configChanges="orientation" to the manifest file). This will keep your Activity from being recreated on each rotation, but it may also require you to reload your view hierarchy if you have different layouts for landscape and portrait. However, even if you have to do this the Acitivity instance will be the same you can safely save the Bitmap without resorting to a static field.
However, if you cannot reproduce the problem with the same project in a native Java project, I would report a Mono bug.
Hard to see without the entire code but it obviously sounds like you have a memory leak. Screen rotation (due to the destroying/creation of the activity) is known to cause these. You might want to have a read at this article by Romain Guy as well as this talk from last year's IO.

Categories

Resources