<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vladimir Ryabchikov on Medium]]></title>
        <description><![CDATA[Stories by Vladimir Ryabchikov on Medium]]></description>
        <link>https://medium.com/@vladimirryabchikov?source=rss-f9d9957aa354------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Et6IRK3BEg8iY4RBmLN25A.jpeg</url>
            <title>Stories by Vladimir Ryabchikov on Medium</title>
            <link>https://medium.com/@vladimirryabchikov?source=rss-f9d9957aa354------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:56:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vladimirryabchikov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Working with 9-patch]]></title>
            <link>https://medium.com/@vladimirryabchikov/working-with-9-patch-fff5f7841c3a?source=rss-f9d9957aa354------2</link>
            <guid isPermaLink="false">https://medium.com/p/fff5f7841c3a</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Vladimir Ryabchikov]]></dc:creator>
            <pubDate>Mon, 02 Mar 2015 10:49:54 GMT</pubDate>
            <atom:updated>2015-03-02T10:49:54.920Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to resize 9-patch drawable with respect to aspect ratio.</h3><p>Recently I was working on a task of creating guide “How to add widget to your home screen” for our android app-widget <a href="https://play.google.com/store/apps/details?id=com.snaprix.commuter">Commuter</a>. If not mention the vast majority of different home screen launchers, the task was pretty simple. We decided to start with two launchers: default Nexus launcher and brand new <a href="https://play.google.com/store/apps/details?id=com.google.android.launcher&amp;hl=en">Google Now Launcher</a> and display several “how to add widget” steps. The result should be similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fNMDK5YDMR8alyJCWa5JXg.png" /></figure><h4>Creating 9-patch drawable</h4><p>I started with creating images for each screen. I have chosen <a href="http://radleymarx.com/blog/simple-guide-to-9-patch/">9-patch drawables</a> to maintain screenshot original resolution for various screen sizes and densities. Creating 9-patch drawables took some time, but I will not describe details here. To sum up, I took screenshot, put it into device frame, and added patches. For example, I got such image for the first page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*NnTZoe9q3GrOJjiBlJn13A.png" /><figcaption>Colored areas patches</figcaption></figure><p>Then, I created fragment layout with ImageView for the image:</p><pre>&lt;FrameLayout<br>  xmlns:android=”http://schemas.android.com/apk/res/android&quot;<br>  xmlns:tools=”http://schemas.android.com/tools&quot;<br>  android:layout_width=”match_parent”<br>  android:layout_height=”match_parent”<br>  tools:context=”com.snaprix.MainFragment”&gt;</pre><pre>&lt;ImageView <br>    android:layout_width=”match_parent”<br>    android:layout_height=”match_parent”<br>    android:scaleType=”fitXY”<br>    android:src=”@drawable/guide_now_page_0&quot;/&gt;<br>&lt;/FrameLayout&gt;</pre><p>The size of this image is 512 x 932 px for xhdpi bucket (256 x 466 dp). We have set <a href="http://developer.android.com/reference/android/widget/ImageView.ScaleType.html">scaleType</a> property to fitXY to force image be stretched (otherwise 9-patch drawable will not work). This params work fine for portrait layout, because image fits most screen sizes, but the height of the image is too big for landscape layout on phones and small tablets, so its aspect ratio breaks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*AMAvv_S5zyJTIWBV76e-2w.png" /></figure><p>Unfortunately, there is no value for <a href="http://developer.android.com/reference/android/widget/ImageView.ScaleType.html">scaleType</a> property to resolve this issue, so my workaround for this issue will be described in the next chapter.</p><h4>Cropping 9-patch drawable with respect to aspect ratio</h4><p>In this situation I decided to resize initial image maintaining aspect ratio, and then crop it, as a result we will have such image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*f7fx-XRbP6I8a524O8N9lQ.png" /></figure><p>To get this behavior I created ScaleImageView class (<a href="http://developer.android.com/reference/android/widget/ImageView.html">ImageView</a> subclass) and overrode it’s <a href="http://developer.android.com/reference/android/view/View.html#onMeasure(int,%20int)">onMeasure</a> and <a href="http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)">onDraw</a> methods. The final implementation could seem simple, but it took me couple hours to make it work as expected, so if my post save you this hours my goal will be accomplished.</p><p>Firstly, I added instance variables to store drawable right, bottom bounds and matrix for transformation:</p><pre>private int mWidthSize;<br>private int mHeightSize;<br>private Matrix mMatrix;</pre><p>mMatrix should be initialized in View’s constructor:</p><pre>mMatrix = new Matrix();</pre><p>Then override <a href="http://developer.android.com/reference/android/view/View.html#onMeasure(int,%20int)">onMeasure</a> method.</p><blockquote><em>This method measure the view and its content to determine the measured width and the measured height (see </em><a href="http://developer.android.com/reference/android/view/View.html#onMeasure(int,%20int)"><em>documentation</em></a><em> for more details).</em></blockquote><p>We use it to calculate drawable bounds and matrix transformation (when necessary), which will be applied in onDraw method.</p><pre><a href="http://twitter.com/Override">@Override</a><br>protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {<br>  // get dimensions provided by parent for this view:<br>  int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);<br>  int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);</pre><pre>// subtract padding to get real space available for the view<br>  int vwidth = measuredWidth—getPaddingLeft()—getPaddingRight();<br>  int vheight = measuredHeight—getPaddingTop()—getPaddingBottom();</pre><pre>// get drawable size<br>  Drawable drawable = getDrawable();<br>  final int dwidth = drawable.getIntrinsicWidth();<br>  final int dheight = drawable.getIntrinsicHeight();</pre><pre>/*<br>   * When view height is smaller than image height and <br>   * view width is larger than image width <br>   * calc target width, height and <br>   * apply matrix transformation, <br>   * otherwise just take view width and height.<br>   */<br>  if (vheight &lt; dheight &amp;&amp; dwidth &lt; vwidth) {<br>    // fill entire view width<br>    mWidthSize = vwidth;<br>    // calculate target height to maintain aspect ratio<br>    mHeightSize = (dheight * vwidth) / dwidth;</pre><pre>// move image to have “fit bottom” effect<br>    int dy = vheight — mHeightSize;<br>    mMatrix.setTranslate(0, dy);</pre><pre>// apply matrix transformation<br>    setImageMatrix(mMatrix);<br>  } else {<br>    // take view width and height to get fitXY scaleType<br>    mWidthSize = vwidth;<br>    mHeightSize = vheight;<br>  }<br>  super.onMeasure(widthMeasureSpec, heightMeasureSpec);<br>}</pre><p>Finally, we should override <a href="http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)">onDraw</a> method to apply calculated drawable right and bottom bounds:</p><pre><a href="http://twitter.com/Override">@Override</a><br>protected void onDraw(Canvas canvas) {<br>  Drawable drawable = getDrawable();<br>  // set bounds to scale drawable using 9-patch params<br>  drawable.setBounds(0, 0, mWidthSize, mHeightSize);</pre><pre>super.onDraw(canvas);<br>}</pre><p>Replace ImageView with ScaleImageView in Fragment layout, and DO NOT FORGET to set <a href="http://developer.android.com/reference/android/widget/ImageView.ScaleType.html">scaleType</a> property to matrix:</p><pre>&lt;FrameLayout<br>  xmlns:android=”<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>&quot;<br>  xmlns:tools=”<a href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>&quot;<br>  android:layout_width=”match_parent”<br>  android:layout_height=”match_parent”<br>  tools:context=”com.snaprix.MainFragment”&gt;</pre><pre>&lt;com.snaprix.views.ScaleImageView<br>    android:layout_width=”match_parent”<br>    android:layout_height=”match_parent”<br>    android:scaleType=”matrix”<br>    android:src=”<a href="http://twitter.com/drawable/guide_now_page_0">@drawable/guide_now_page_</a>0&quot;/&gt;<br>&lt;/FrameLayout&gt;</pre><p>Now you could run the app and check that 9-patch cropped as expected.</p><p>Source code for this example is available on <a href="https://github.com/vova1vova/Cropped9patch">GitHub</a></p><p>ps</p><p>Thank you for reading this article, if it helped you any way I would be happy. You could watch this code working live in our <a href="https://play.google.com/store/apps/details?id=com.snaprix.commuter">Commuter app available in Google Play</a>. The app is currently working in Russia (but could be downloaded everywhere). If you would like the app working in your area, please contact us at <a href="/service/mailto:support@snaprix.com">support@snaprix.com</a>, we will try our best to make it happen. If you have any questions please contact me at <a href="/service/mailto:vladimir@snaprix.com">vladimir@snaprix.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fff5f7841c3a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>