這次學學怎么使用動畫資源,AnimationDrawable,同樣要定義一個相關的xml動畫文件,要放在路徑/res/anmi下,當創建一個Android應用時,默認不會創建該文件夾,需要自己手動創建.
動畫分為逐幀動畫(像電影一樣,一張一張的播放)和補間動畫(平移,旋轉,縮放,位移),
關于動畫的內容還是有點多,這次我只學一點,就是怎么定義和使用動畫資源
補間動畫可以有4個動作
alpha: 設置透明度的改變
scale: 設置圖片進行縮放改變
translate: 設置圖片進行位移變換
rotate:設置圖片進行旋轉
下面以補間動畫為例,根元素為<set .../>,在路徑/res/anmi下定義一個動畫文件my_anim.xml
?
<?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.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="2000" />
<!-- 定義位移變換 -->
<translate android:fromXDelta="10"
android:toXDelta="130"
android:fromYDelta="30"
android:toYDelta = "-80"
android:duration="2000" />
</set>
下面是主界面main.xml
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fengjing" />
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
在代碼中調用動畫資源
?
package WangLi.Resource.AnimationDrawableTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationDrawableTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView)findViewById(R.id.image);
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_anim);
//設置動畫結束后保留結束狀態
anim.setFillAfter(true);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{
image.startAnimation(anim);
}
});
}
}
動畫開始前
點擊按鈕,動畫結束后,圖片得到縮放,并且位移
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

