android Animations 動畫效果(二)

來源:互聯網
上載者:User

Animations的第二種使用方法
1在res檔案夾下建立一個anim檔案夾
2.建立xml檔案,並首先加入set標籤,改標籤如下:

android:interpolator="@android:anim/accelerate_interpolator">

3.在該標籤中加入rotate,alpha,scale或translate標籤

4.在代碼中使用AnimationUtils當中裝載xml檔案,並產生Animation對象


下面是代碼

MainActivity.java

package com.yx.animations01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private Button scaleButton=null;
private Button rotateButton=null;
private Button alphaButton=null;
private Button translateButton=null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new scaleButtonListener());

rotateButton = (Button) findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new rotateButtonListener());

alphaButton = (Button) findViewById(R.id.alphaButton);
alphaButton.setOnClickListener(new alphaButtonListener());

translateButton = (Button) findViewById(R.id.translateButton);
translateButton.setOnClickListener(new translateButtonListener());

imageView = (ImageView) findViewById(R.id.imageViewId);
}

class scaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
imageView.startAnimation(animation);
}
}

class rotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
imageView.startAnimation(animation);
}
}

//淡入淡出
class alphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//使用AnimationUtils裝載動畫設定檔案
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
}
}

class translateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imageView.startAnimation(animation);
}
}

}

下面分別是四個xml

alpha.xml


android:interpolator="@android:anim/accelerate_interpolator">
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500">


rotate.xml


android:interpolator="@android:anim/accelerate_interpolator">
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000">



scale.xml


android:interpolator="@android:anim/accelerate_interpolator">
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
>


translate.xml


android:interpolator="@android:anim/accelerate_interpolator">
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"
/>

注意xml檔案的置放位置


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.