java實(shí)現(xiàn)兩張圖片2D翻轉(zhuǎn)動(dòng)畫(huà)效果
本文實(shí)例為大家分享了java實(shí)現(xiàn)兩張圖片2D翻轉(zhuǎn)動(dòng)畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下
這可能是簡(jiǎn)單的動(dòng)畫(huà)效果吧,但是感覺(jué)還挺有意思的。
效果如下

XML代碼如下,很簡(jiǎn)單只有兩個(gè)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代碼,也挺簡(jiǎn)單的
/*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;
? ? //第一個(gè)動(dòng)畫(huà),效果是像翻轉(zhuǎn)似得消失
? ? //第一個(gè)參數(shù)和第二個(gè)參數(shù)表示在x軸上的變化:從1變?yōu)?。
? ? //第三個(gè)參數(shù)和第四個(gè)參數(shù)表示在y軸上的變化:從1變?yōu)?,沒(méi)有變化。
? ? //第五個(gè)參數(shù)和第六個(gè)參數(shù)表示在x軸上的變化所參考的位置:RELATIVE_TO_PARENT沿著父級(jí)空間,0.5f的中心點(diǎn)。
? ? //第七個(gè)參數(shù)和第八個(gè)參數(shù)表示在y軸上的變化所參考的位置:含義與x軸類(lèi)似
? ? private ScaleAnimation sato1=new ScaleAnimation(1, 0, 1, 1,?
? ? ? ? ? ? Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
? ? //第二個(gè)動(dòng)畫(huà),效果是像翻轉(zhuǎn)似得出現(xiàn)
? ? 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);
? ? ? ? //自定義的一個(gè)初始化方法
? ? ? ? initImage();
? ? ? ? //主布局使用的是框架布局framelayout,其中只有一個(gè)圖片,所以點(diǎn)擊framelayout時(shí)候就翻轉(zhuǎn)圖片
? ? ? ? findViewById(R.id.framelayout1).setOnClickListener(new OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? if (imageview1.isShown()) {//當(dāng)前的圖片是圖片1
? ? ? ? ? ? ? ? ? ? imageview1.startAnimation(sato1);//圖片1翻轉(zhuǎn)式消失
? ? ? ? ? ? ? ? }else {//當(dāng)前圖片是圖片2的話,圖片2就翻轉(zhuǎn)式消失
? ? ? ? ? ? ? ? ? ? imageview2.startAnimation(sato1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void showImage1(){
? ? ? ? imageview1.setVisibility(View.VISIBLE);//圖片1可見(jiàn)
? ? ? ? imageview2.setVisibility(View.INVISIBLE);//圖片2不可見(jiàn)
? ? }
? ? private void showImage2(){
? ? ? ? imageview1.setVisibility(View.INVISIBLE);//圖片1不可見(jiàn)
? ? ? ? imageview2.setVisibility(View.VISIBLE);//圖片2可見(jiàn)
? ? }
? ? private void initImage(){
? ? ? ? imageview1 = (ImageView) findViewById(R.id.imageView1);
? ? ? ? imageview2 = (ImageView) findViewById(R.id.imageView2);
? ? ? ? showImage1();//默認(rèn)顯示圖片1
? ? ? ? sato1.setDuration(1000);//給動(dòng)畫(huà)設(shè)置執(zhí)行時(shí)間
? ? ? ? sato2.setDuration(1000);
? ? ? ? //給動(dòng)畫(huà)1設(shè)置時(shí)間監(jiān)聽(tīng)器,因?yàn)橐男Ч牵涸趧?dòng)畫(huà)一結(jié)束時(shí)立即開(kāi)始動(dòng)畫(huà)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
? ? ? ? ? ? }
? ? ? ? ? ? //動(dòng)畫(huà)結(jié)束的時(shí)候執(zhí)行的方法 ? ? ? ? ?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? //如果當(dāng)前圖片是圖片1
? ? ? ? ? ? ? ? if (imageview1.getVisibility()==View.VISIBLE) {
? ? ? ? ? ? ? ? ? ? //把圖片1的動(dòng)畫(huà)設(shè)置為空,現(xiàn)在圖片1的不需要了。也方便下一次設(shè)置動(dòng)畫(huà)
? ? ? ? ? ? ? ? ? ? imageview1.setAnimation(null);
? ? ? ? ? ? ? ? ? ? imageview2.startAnimation(sato2);//圖片2按照動(dòng)畫(huà)2出場(chǎng) ??
? ? ? ? ? ? ? ? ? ? showImage2();//動(dòng)畫(huà)播完了后,把圖片2顯示出來(lái)
? ? ? ? ? ? ? ? }else {//如果當(dāng)前的圖片是圖片2,圖片1就翻轉(zhuǎn)式的出現(xiàn)
? ? ? ? ? ? ? ? ? ? imageview2.setAnimation(null);
? ? ? ? ? ? ? ? ? ? imageview1.startAnimation(sato2);
? ? ? ? ? ? ? ? ? ? showImage1();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java調(diào)用python代碼的五種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java調(diào)用python代碼的五種方式,在Java中調(diào)用Python函數(shù)的方法有很多種,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Java實(shí)現(xiàn)Swing組件定制Button示例
這篇文章主要介紹了Java實(shí)現(xiàn)Swing組件定制Button,涉及java Swing組件Button相關(guān)屬性設(shè)置與使用操作技巧,需要的朋友可以參考下2018-01-01
MyBatis3用log4j在控制臺(tái)輸出SQL的方法示例
本篇文章主要介紹了MyBatis3用log4j在控制臺(tái)輸出SQL的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
關(guān)于spring boot中幾種注入方法的一些個(gè)人看法
這篇文章主要給大家介紹了關(guān)于spring boot中幾種注入方法的一些個(gè)人看法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
java網(wǎng)絡(luò)編程之識(shí)別示例 獲取主機(jī)網(wǎng)絡(luò)接口列表
一個(gè)客戶(hù)端想要發(fā)起一次通信,先決條件就是需要知道運(yùn)行著服務(wù)器端程序的主機(jī)的IP地址是多少。然后我們才能夠通過(guò)這個(gè)地址向服務(wù)器發(fā)送信息。2014-01-01
Springboot2.0自適應(yīng)效果錯(cuò)誤響應(yīng)過(guò)程解析
這篇文章主要介紹了Springboot2.0自適應(yīng)效果錯(cuò)誤響應(yīng)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

