簡體   English   中英

如何以編程方式為ImageView設置動畫

[英]How to programmatically animate an ImageView

我正在嘗試在單擊所述圖像視圖時為ImageView設置動畫。

具體來說,我希望ImageView的大小變大(比如.20更大)並立即縮小回原始大小)。

到目前為止,我一直在試驗這段代碼而沒有運氣。

// thumbLike is the imageView I would like to animate.
button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.5f, 1.0f, 2.5f,
                                    Animation.RELATIVE_TO_SELF, 0.5f,
                                    Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnim.setInterpolator(new LinearInterpolator());
        scaleAnim.setDuration(1500);
        thumbLike.startAnimation(scaleAnim);
        thumbLike.setAnimation(null);
    }
});

任何人都可以建議我使用可能的解決方案

編輯#1

正如Hardik4560所回答的那樣,它正在通過XML工作:

// finally i use this code to execute the animation
Animation animationScaleUp = AnimationUtils.loadAnimation(this, R.anim.scale_up);
Animation animationScaleDown = AnimationUtils.loadAnimation(this, R.anim.scale_down);

AnimationSet growShrink = new AnimationSet(true);
growShrink.addAnimation(animationScaleUp);
growShrink.addAnimation(animationScaleDown);
thumbLike.startAnimation(growShrink);

和XML

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

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

ps:這很尷尬,因為我已經接受了答案。 我試圖將@tharkbad和@ Hardik4560的答案結合起來,但現在動畫的方式看起來並不順暢。

在scale_up動畫期間,它看起來像是“跳過”到動畫結束然后立即開始scale_down動畫。 我想我必須稍微玩一下。

如果你想在沒有XML的情況下實現它,你可以這樣做

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);

當然,您也可以使用NineOldAndroids並使用新的動畫方法。

我認為您的原始錯誤是此行,它會再次從視圖中刪除您剛剛開始的動畫。

thumbLike.setAnimation(null);

我用它來實現popin彈出效果,

看看它對你有用

彈出。

<?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>

流行

<?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>

我使用kotlin創建了相同的動畫:

回復: https //github.com/David-Hackro/Bounce-Animation

你需要創建你的元素並擴展任何東西(ImageView,Button等),在我的情況下你創建了一個名為BounceWidget的類

在此輸入圖像描述

在xml中添加元素

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

以編程方式添加元素

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM