How to programmatically animate an ImageView

前端 未结 3 1001
天命终不由人
天命终不由人 2021-02-14 09:19

I am trying to animate an ImageView when the said image view is clicked.

Specifically I want the size of the ImageView gets bigger (say .20 bigger) and the immediately s

相关标签:
3条回答
  • 2021-02-14 10:00

    I use this to achieve popin popout effect,

    See if its of any use to you

    Pop Out.

    <?xml version="1.0" encoding="utf-8"?>
    <set
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/bounce_interpolator"    >
        <scale
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromXScale="0.5"
            android:fromYScale="0.5"
            android:toXScale="1.0"
            android:toYScale="1.0"
            android:duration="500" />
    
    </set>
    

    Pop In

    <?xml version="1.0" encoding="utf-8"?>
    <set
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/bounce_interpolator"    >
        <scale
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:toXScale="0.0"
            android:toYScale="0.0"
            android:duration="500" />
    
    </set>
    
    0 讨论(0)
  • 2021-02-14 10:03

    If you want to implement this without XML you could do so as follows

    final float growTo = 1.2f;
    final long duration = 1200;
    
    ScaleAnimation grow = new ScaleAnimation(1, growTo, 1, growTo, 
                                             Animation.RELATIVE_TO_SELF, 0.5f,
                                             Animation.RELATIVE_TO_SELF, 0.5f);
    grow.setDuration(duration / 2);
    ScaleAnimation shrink = new ScaleAnimation(growTo, 1, growTo, 1,
                                               Animation.RELATIVE_TO_SELF, 0.5f,
                                               Animation.RELATIVE_TO_SELF, 0.5f);
    shrink.setDuration(duration / 2);
    shrink.setStartOffset(duration / 2);
    AnimationSet growAndShrink = new AnimationSet(true);
    growAndShrink.setInterpolator(new LinearInterpolator());
    growAndShrink.addAnimation(grow);
    growAndShrink.addAnimation(shrink);
    thumbLike.startAnimation(growAndShrink);
    

    Of course, you could also use NineOldAndroids and use the new animation methods.

    I think your original error is this line, it removes the animation you just started from the view again.

    thumbLike.setAnimation(null);
    
    0 讨论(0)
  • 2021-02-14 10:24

    I created the same animation using kotlin:

    Repo: https://github.com/David-Hackro/Bounce-Animation

    You need create your element and extend of anything (ImageView,Button etc) in my case y created a class named BounceWidget

    Add element in xml

    <hackro.tutorials.com.bounceWidget.widget.BounceWidget
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    Add element programmatically

    val xpp = resources.getXml(R.xml.bouncewidget)
    val attr = Xml.asAttributeSet(xpp)
    val layout : LinearLayout = findViewById(R.id.layout)
    val octocat1 : BounceWidget = BounceWidget(this,attr)
    
    //you can change this values and have default values
    
    //octocat1.id_resource(R.mipmap.bounceimage) // change image --> default : R.mipmap.ic_launcher
    //octocat1.positionX = 1 //position X starting --> default : 0
    //octocat1.positionY = 1 //position Y starting--> default : 0
    //octocat1.velocityX = 1 //Velocity X --> default : 20
    //octocat1.velocityY = 1 //Velocity Y --> default : 15
    
    octocat1.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    
    layout.addView(octocat1)
    
    0 讨论(0)
提交回复
热议问题