android tween動畫無法停止的問題

來源:互聯網
上載者:User

       今天在寫一個關於心跳的動畫時用的時tween動畫,發現無法停止。

1:動畫anim.xml檔案如下: </p><p><?xml version="1.0" encoding="utf-8"?><br /><set android:shareinterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <scale android:duration="335" android:fillafter="true" android:fromxscale="1.0" android:fromyscale="1.0" android:interpolator="@android:anim/linear_interpolator" android:pivotx="50%" android:pivoty="50%" android:repeatcount="infinite" android:startoffset="700" android:toxscale="1.4" android:toyscale="1.4"><br /> <!-- </p><p> fromXScale[float]<br /> fromYScale[float] 為動畫起始時,X、Y座標上的伸縮尺寸;0.0表示收縮到沒有 ;1.0表示正常無伸縮. 值小於1.0表示收縮. 值大於1.0表示放大toXScale [float]<br /> toYScale[float] 為動畫結束時,X、Y座標上的伸縮尺寸pivotX[float]<br /> pivotY[float] 為動畫相對於物件的X、Y座標的開始位置屬性值說明:從0%-100%中取值,50%為物件的X或Y方向座標上的中點位置<br /> --></p><p></scale></set><br />2:activity_main.xml的布局代碼如下:<br /><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"></p><p> <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:background="@anim/frame_anim" android:visibility="invisible"></p><p> <button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start"></p><p> </button><button android:id="@+id/end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_torightof="@+id/start" android:text="end"><br /></button></imageview></relativelayout></p><p>3:java 代碼如下: </p><p>package com.youpeng.animationdemo;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.view.animation.Animation;<br />import android.widget.Button;<br />import android.widget.ImageView;</p><p>public class MainActivity extends Activity implements OnClickListener {<br />private ImageView mImageView;<br />private Button start, end;<br />private Animation mAnimation</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.activity_main);</p><p>mImageView = (ImageView) findViewById(R.id.imageview);</p><p>start = (Button) findViewById(R.id.start);<br />start.setOnClickListener(this);<br />end = (Button) findViewById(R.id.end);<br />end.setOnClickListener(this);<br /> mAnimation = AnimationUtils.loadAnimation(this, R.anim.anim);</p><p>// mImageView.setBackgroundResource(R.anim.frame_anim);<br />// mAnimationDrawable = (AnimationDrawable)mImageView.getBackground();</p><p>}<br />//<br />@Override<br />public void onClick(View v) {<br />// TODO Auto-generated method stub<br />switch (v.getId()) {<br />case R.id.start:<br /> mImageView.startAnimation(mAnimation);</p><p>mImageView.setVisibility(View.VISIBLE);<br />Log.e("start", "start");<br />break;<br />case R.id.end:<br />// mImageView.setVisibility(View.GONE);<br />mImageView.clearAnimation();<br />Log.e("end", "end");</p><p>break;<br />default:<br />break;<br />}<br />}<br />}<br />4:在Animation類中有兩個方法分別是start()和 startNow()方法,這兩方法都可以啟動動畫,但要停止動畫有一個canel(),但好像一直都停止 不了,所以View大類中有兩個方法是startAnimation()和clearAnimation(),這兩個方法可以開始動畫和停止動畫,但想讓該動畫訊息在介面
上還得加一個讓該組件 setVisibility(View.GONE)。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.