java實現兩張圖片2D翻轉動畫效果
更新時間:2022年08月22日 08:35:16 作者:碼靈薯
這篇文章主要為大家詳細介紹了java實現兩張圖片2D翻轉動畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現兩張圖片2D翻轉動畫的具體代碼,供大家參考,具體內容如下
這可能是簡單的動畫效果吧,但是感覺還挺有意思的。
效果如下

XML代碼如下,很簡單只有兩個imageview
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/framelayout1" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context="com.example.dreverse.MainActivity" > ? ? <ImageView ? ? ? ? android:id="@+id/imageView1" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:src="@drawable/image1" /> ? ? <ImageView ? ? ? ? android:id="@+id/imageView2" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:src="@drawable/image2" /> </FrameLayout>
java代碼,也挺簡單的
/*the reversing animation of two pictures
?* @author stephenson feng
?* @date 2016-9-4
?* */
package com.example.dreverse;
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.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
? ? private ImageView imageview1;
? ? private ImageView imageview2;
? ? //第一個動畫,效果是像翻轉似得消失
? ? //第一個參數和第二個參數表示在x軸上的變化:從1變?yōu)?。
? ? //第三個參數和第四個參數表示在y軸上的變化:從1變?yōu)?,沒有變化。
? ? //第五個參數和第六個參數表示在x軸上的變化所參考的位置:RELATIVE_TO_PARENT沿著父級空間,0.5f的中心點。
? ? //第七個參數和第八個參數表示在y軸上的變化所參考的位置:含義與x軸類似
? ? private ScaleAnimation sato1=new ScaleAnimation(1, 0, 1, 1,?
? ? ? ? ? ? Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
? ? //第二個動畫,效果是像翻轉似得出現
? ? private ScaleAnimation sato2=new ScaleAnimation(0, 1, 1, 1,?
? ? ? ? ? ? Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //自定義的一個初始化方法
? ? ? ? initImage();
? ? ? ? //主布局使用的是框架布局framelayout,其中只有一個圖片,所以點擊framelayout時候就翻轉圖片
? ? ? ? findViewById(R.id.framelayout1).setOnClickListener(new OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? if (imageview1.isShown()) {//當前的圖片是圖片1
? ? ? ? ? ? ? ? ? ? imageview1.startAnimation(sato1);//圖片1翻轉式消失
? ? ? ? ? ? ? ? }else {//當前圖片是圖片2的話,圖片2就翻轉式消失
? ? ? ? ? ? ? ? ? ? imageview2.startAnimation(sato1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void showImage1(){
? ? ? ? imageview1.setVisibility(View.VISIBLE);//圖片1可見
? ? ? ? imageview2.setVisibility(View.INVISIBLE);//圖片2不可見
? ? }
? ? private void showImage2(){
? ? ? ? imageview1.setVisibility(View.INVISIBLE);//圖片1不可見
? ? ? ? imageview2.setVisibility(View.VISIBLE);//圖片2可見
? ? }
? ? private void initImage(){
? ? ? ? imageview1 = (ImageView) findViewById(R.id.imageView1);
? ? ? ? imageview2 = (ImageView) findViewById(R.id.imageView2);
? ? ? ? showImage1();//默認顯示圖片1
? ? ? ? sato1.setDuration(1000);//給動畫設置執(zhí)行時間
? ? ? ? sato2.setDuration(1000);
? ? ? ? //給動畫1設置時間監(jiān)聽器,因為要的效果是:在動畫一結束時立即開始動畫2
? ? ? ? sato1.setAnimationListener(new AnimationListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? }
? ? ? ? ? ? //動畫結束的時候執(zhí)行的方法 ? ? ? ? ?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? //如果當前圖片是圖片1
? ? ? ? ? ? ? ? if (imageview1.getVisibility()==View.VISIBLE) {
? ? ? ? ? ? ? ? ? ? //把圖片1的動畫設置為空,現在圖片1的不需要了。也方便下一次設置動畫
? ? ? ? ? ? ? ? ? ? imageview1.setAnimation(null);
? ? ? ? ? ? ? ? ? ? imageview2.startAnimation(sato2);//圖片2按照動畫2出場 ??
? ? ? ? ? ? ? ? ? ? showImage2();//動畫播完了后,把圖片2顯示出來
? ? ? ? ? ? ? ? }else {//如果當前的圖片是圖片2,圖片1就翻轉式的出現
? ? ? ? ? ? ? ? ? ? imageview2.setAnimation(null);
? ? ? ? ? ? ? ? ? ? imageview1.startAnimation(sato2);
? ? ? ? ? ? ? ? ? ? showImage1();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

