<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vignette &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/vignette/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Sat, 22 Oct 2022 09:44:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>vignette &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Android &#8211; Create a Vignette effect</title>
		<link>https://blog.sephiroth.it/2014/07/06/android-create-a-vignette-effect/</link>
					<comments>https://blog.sephiroth.it/2014/07/06/android-create-a-vignette-effect/#respond</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sun, 06 Jul 2014 03:32:17 +0000</pubDate>
				<category><![CDATA[android]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[radialgradient]]></category>
		<category><![CDATA[shader]]></category>
		<category><![CDATA[vignette]]></category>
		<category><![CDATA[xfermode]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=1685</guid>

					<description><![CDATA[Let&#8217;s say you want to want to dynamically&#160;apply a &#8220;vignette&#8221; on an ImageView. Something like this: These are basically 4&#160;steps we need in order to accomplish the task: Create a Paint using the&#160;PorterDuff.Mode.DST_OUT as&#160;Xfermode Create a RadialGradient and assign to our Paint object as a Shader Alter the gradient matrix using the&#160;setLocalMatrix method. Use&#160;canvas.saveLayer when &#8230; <a href="https://blog.sephiroth.it/2014/07/06/android-create-a-vignette-effect/" class="more-link">Continue reading <span class="screen-reader-text">Android &#8211; Create a Vignette effect</span></a>]]></description>
										<content:encoded><![CDATA[<p>Let&#8217;s say you want to want to dynamically apply a &#8220;vignette&#8221; on an <a href="http://developer.android.com/reference/android/widget/ImageView.html">ImageView</a>.<br />
Something like this:</p>
<table style="height: 300px;" width="100%">
<tbody>
<tr>
<td><img fetchpriority="high" decoding="async" src="https://blog.sephiroth.it/wp-content/uploads/2014/07/Screenshot_2014-07-05-22-30-30-168x300.png" alt="Screenshot_2014-07-05-22-30-30" width="168" height="300" hspace="20" /></td>
</tr>
</tbody>
</table>
<p>These are basically 4 steps we need in order to accomplish the task:<br />
<span id="more-1685"></span></p>
<ol>
<li>Create a Paint using the <a href="http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html">PorterDuff.Mode</a>.DST_OUT as Xfermode</li>
<li>Create a <a href="http://developer.android.com/reference/android/graphics/RadialGradient.html">RadialGradient</a> and assign to our Paint object as a Shader</li>
<li>Alter the gradient matrix using the setLocalMatrix method.</li>
<li>Use canvas.saveLayer when drawing</li>
</ol>
<h3>1. Paint using PorterDuff.Mode.DST_OUT</h3>
<p>This is needed in order to draw an &#8220;hole&#8221; in our back background.</p>
<pre>
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
</pre>
<h3>2. Create the RadialGradient shader:</h3>
<p>This is required in order to draw a radial gradient &#8220;hole&#8221; in our black background. Thus revealing the background image using a &#8220;vignette&#8221; filter.</p>
<pre>
final int[] colors = new int[]{0xff000000, 0xff000000, 0};
final float[] anchors = new float[]{0, 0.5f, 1};
Shader shader = new android.graphics.RadialGradient(0, 0, 1, colors, anchors, Shader.TileMode.CLAMP);
paint.setShader(shader);
</pre>
<h3>3. Alter the gradient matrix</h3>
<p>The again, in order to create a real vignette effect we need to draw a custom &#8220;oval&#8221;, otherwise the result of draw something using the radialgradient shader will result in a simple circle. Instead, if we alter its matrix we can modify its aspect ratio, giving it the oval shape:</p>
<pre>
Matrix matrix = new Matrix();
matrix.postTranslate(rect.centerX(), rect.centerY());
matrix.postScale(rect.width() / 2, rect.height() / 2, rect.centerX(), rect.centerY());
shader.setLocalMatrix(matrix);
</pre>
<h3>4. Draw on a canvas</h3>
<p>Now we need to draw our objects on a real canvas. We need to save the current layer using <a href="http://developer.android.com/reference/android/graphics/Canvas.html#saveLayer(android.graphics.RectF,%20android.graphics.Paint, int)">Canvas.saveLayer</a> because otherwise everything we previously drawn will be erased by our paint. But because we&#8217;re using saveLayer, we actually are drawing on an offscreen bitmap.</p>
<pre>
// save current status
// 'pBitmapRect' is the current Bitmap rectangle
canvas.saveLayer(pBitmapRect, mPaint, Canvas.ALL_SAVE_FLAG);
// draw the black background
canvas.drawRect(pBitmapRect, mBlackPaint);
// draw the vignette on the black background using our radial gradient
canvas.drawOval(tempRect2, mPaintShader);
canvas.restore();
</pre>
<h2>Source Code</h2>
<p>You can download the complete source code here:<br />
<a href="https://github.com/sephiroth74/vignette_demo">https://github.com/sephiroth74/vignette_demo</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2014/07/06/android-create-a-vignette-effect/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
