Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom View is Causing Strange behavior with RelativeLayout
    primarykey
    data
    text
    <p>I am trying to use Romain Guy's <a href="http://www.curious-creature.org/2012/12/13/android-recipe-2-fun-with-shaders/" rel="nofollow noreferrer">SpotlightView</a> to highlight a particular part of my Action Bar, but I'm running into some problems. To start with, I've created a custom Action Bar which centers text via this layout, which works in a vacuum.</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rel_test" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" &gt; &lt;TextView android:id="@+id/actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/app_name" android:textSize="24sp" android:textColor="@android:color/white" android:contentDescription="@string/app_name" android:shadowColor="@android:color/black" android:shadowDx="0" android:shadowDy="-1" android:shadowRadius="1" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Now, that's all well and good, and the title is centered as you can see here.</p> <p><img src="https://i.stack.imgur.com/AvhdT.png" alt="enter image description here"></p> <p>However, when I do the following everything gets messed up.</p> <pre><code>public boolean onPrepareOptionsMenu(Menu menu) { SpotlightView spotlightView = new SpotlightView(this, null); // some slight changes I made to allow me to set information dynamically spotlightView.setTargetId(mActionbarTitle.getId()); spotlightView.setMaskId(R.drawable.spot_mask); spotlightView.setAlpha(0.33f); RelativeLayout layout = (RelativeLayout) getSupportActionBar().getCustomView().findViewById(R.id.rel_test); layout.add(spotlightView); } </code></pre> <p>When the following happens. Note that nothing is shown because I have stripped SpotlightView down basically to a call to super in the constructor, and a call to <code>super.onDraw()</code>. I have also included the screenshot with the layout bounds shown here.</p> <p><img src="https://i.stack.imgur.com/GeY3s.png" alt="Incorrect ActionBar"></p> <p>As you can see, it looks like what happens is the wrapping <code>RelativeLayout</code> only spans the space between the back button and the action buttons, resulting in the <code>TextView</code> being only centered in that space. However, why does that happen? Is there any way around it? Do I need to override <code>onMeasure</code> or another one of the <code>View</code> callbacks?</p> <p>Also, interestingly, when I add another <code>TextView</code> to <code>rel_test</code> the title is still properly centered.</p> <p>EDIT: Alright, so I narrowed it down to a problem caused by calling <code>setMaskId</code>, which in turn calls this block of code:</p> <pre><code>Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8); Canvas c = new Canvas(a); c.drawBitmap(b, 0.0f, 0.0f, null); </code></pre> <p>When I comment out the line that is <code>c.drawBitmap(b, 0.0f, 0.0f, null);</code> everything works smoothly. However, given that the code relies on having that canvas, I'm not quite sure what to do about removing it.</p> <p>I'm not familiar with graphics on Android, so any advice on how to adapt the original code to create the same spotlight effect while not removing the centered Action Bar would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload