<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Himanshu Tiwari</title>
    <description>The latest articles on Forem by Himanshu Tiwari (@himanshut0305).</description>
    <link>https://forem.com/himanshut0305</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F340838%2F5bf1e053-a963-4002-b374-ee59a468869f.png</url>
      <title>Forem: Himanshu Tiwari</title>
      <link>https://forem.com/himanshut0305</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/himanshut0305"/>
    <language>en</language>
    <item>
      <title>Animating Android views</title>
      <dc:creator>Himanshu Tiwari</dc:creator>
      <pubDate>Sun, 27 Mar 2022 07:35:54 +0000</pubDate>
      <link>https://forem.com/himanshut0305/animating-android-views-1nki</link>
      <guid>https://forem.com/himanshut0305/animating-android-views-1nki</guid>
      <description>&lt;p&gt;Hello World!,&lt;br&gt;
Android app without animation looks incomplete, and with rapid growth in architecture android we are missing out animations but lets remember some core roots&lt;/p&gt;

&lt;p&gt;This consist of 4 major categories of animation - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Scale Animation - change size of view&lt;/li&gt;
&lt;li&gt;Rotate Animation - rotate the view in clockwise and anticlockwise&lt;/li&gt;
&lt;li&gt;Translate Animation - change in x &amp;amp; y position in layout&lt;/li&gt;
&lt;li&gt;Alpha Animation - change in transparency&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would like to cover the Translate animation and moving any views in directions for this blog taking imageViews moving up.&lt;br&gt;
Animation Layer: It is a simple frame layout lying on top of any of the fragment where our views will be created and animated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;FrameLayout
        android:id="@+id/animation_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This layout class contains basic 3 functions&lt;br&gt;
&lt;strong&gt;create&lt;/strong&gt;: Here we create a imageView and attach to the animation_holder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun create(): FrameLayout? {

        val imageView = ImageView(activity)
        imageView.setImageBitmap(mBitmap)
        val minWidth = mBitmap!!.width
        val minHeight = mBitmap!!.height
        imageView.measure(
            View.MeasureSpec.makeMeasureSpec(minWidth, View.MeasureSpec.AT_MOST),
            View.MeasureSpec.makeMeasureSpec(minHeight, View.MeasureSpec.AT_MOST)
        )
        var params = imageView.layoutParams
        if (params == null) {
            params = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.TOP
            )
            imageView.layoutParams = params
        }
        val xPosition = mDrawLocation[0]
        val yPosition = mDrawLocation[1]
        params.width = minWidth
        params.height = minHeight
        params = params as FrameLayout.LayoutParams
        params.leftMargin = xPosition
        params.topMargin = yPosition
        imageView.layoutParams = params
        val animationLayer = FrameLayout(activity)
        val topLayerParam = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT,
            Gravity.TOP
        )
        animationLayer.layoutParams = topLayerParam
        animationLayer.addView(imageView)
        attachingView!!.addView(animationLayer)
        mCreatedAnimationLayer = animationLayer

        return mCreatedAnimationLayer
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;applyAnimation&lt;/strong&gt;: here we start the animation on the imageView Added&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun applyAnimation(animation: Animation?) {
        if (mCreatedAnimationLayer != null) {
            val drawnImageView = mCreatedAnimationLayer!!.getChildAt(0) as ImageView
            drawnImageView.startAnimation(animation)
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;setBitmap&lt;/strong&gt;: here will set the bitmap to global variable which is referenced above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun setBitmap(bitmap: Bitmap?, location: IntArray?): AnimationLayer {
        var location = location
        if (location == null) {
            location = intArrayOf(0, 0)
        } else if (location.size != 2) {
            throw AnimationLayerException("Requires location as an array of length 2 - [x,y]")
        }
        mBitmap = bitmap
        mDrawLocation = location
        return this
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can create a animation with properties needed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// here destination is part where we want to fly the view //upto in x &amp;amp; y for us it is height of the layout  
val animation = TranslateAnimation(
                    0F,
                    destinationX, 0f, DestinationY
                )
//amount of time to travel the distance
animation.duration = duration.toLong()
//animation will start after this much time
animation.startOffset = 0L
// create instance of above layer and apply animation
val layer = AnimationLayer()
 val animationLayer = layer.with(activity)
                    .attachTo(parentView)
                    .setBitmap(scaledBitmap, startingPoints)
                    .create()
// after all the part is ready we can call applyanimation
layer.applyAnimation(animation)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Process is completed you can get the animation like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CGsEKbST--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wau5hwr7v7hhr0mbtwc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CGsEKbST--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wau5hwr7v7hhr0mbtwc.gif" alt="Image description" width="352" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
    </item>
  </channel>
</rss>
