2015-10-18 73 views
2

我試圖創建一個動畫,就像我們在Gmail的listview小部件中看到的一樣。當我們選擇這一行時,我們按下左邊的圓圈並翻轉成一個選中的標記。Gmail列表視圖翻轉動畫

我打算做的是創建一個帶有兩個動畫的動畫集。由於一些奇怪的原因,它不起作用。

我暫時使用單個圖像並將其應用到該圖像上。這是我的anim.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set> 
    <scale 
     android:duration="2000" 
     android:fromXScale="1.0" 
     android:fromYScale="1.0" 
     android:toXScale="0.0" 
     android:toYScale="1.0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     > 
    </scale> 
    <scale 
     android:startOffset="2000" 
     android:duration="2000" 
     android:fromXScale="0.0" 
     android:fromYScale="1.0" 
     android:toXScale="1.0" 
     android:toYScale="1.0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     > 
    </scale> 
</set> 

該動畫很容易閱讀。我開始從100到0的縮放比例,然後將其縮小回100.當我在imageview上應用這個時,我根本沒有看到任何動畫。

這是我的加載動畫:

final AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.flip); 
mCompanyProfileImage.startAnimation(animationSet); 

我在做什麼錯?

回答

0

我無法確定該程序中的任何特定缺陷,它以某種方式爲我工作,如果您有興趣,可以查看我的其他定製,希望它能幫助您。 Javaclass

import android.animation.AnimatorSet; 
import android.animation.AnimatorInflater; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.Menu; 
import android.view.View; 
import android.view.animation.AlphaAnimation; 
import android.view.animation.Animation; 
import android.widget.Button; 
import android.widget.ImageView; 
public class MainActivity extends Activity { 
    AnimatorSet set; 
    Button horizontal,vertical; 
    ImageView imgView; 
    Animation in; 
    Boolean check=false; 
    protected void onCreate(Bundle savedInstanceState) { 
     //TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     horizontal=(Button)findViewById(R.id.button); 
     vertical=(Button)findViewById(R.id.button2); 
     in = new AlphaAnimation(0.0f, 1.0f); 
     in.setDuration(300); 
     horizontal.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       imgView = (ImageView) findViewById(R.id.imageview); 
       set = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.animfliphorizontal); 
       set.setTarget(imgView); 
       set.start(); 
       anim(); 
      } 


     }); 
     vertical.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       imgView=(ImageView)findViewById(R.id.imageview); 
       set = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.animflipvertical); 
       set.setTarget(imgView); 
       set.start(); 
       anim(); 
      } 
     }); 
    } 

    private void anim() { 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
if(check==false) 
{ 
    check=true; 
    imgView.setImageResource(R.drawable.background2); 
} 
       else 
{ 
    check=false; 
    imgView.setImageResource(R.drawable.background); 
} 
       imgView.setAnimation(in); 

      } 
     }, 500); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
} 

下面u能看到一個XML文件和用於水平和垂直2個不同勢對象animaters,檢查出來

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    > 
    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/background" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="HORIZONTAL" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_alignParentStart="true" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="VERTICAL" 
     android:id="@+id/button2" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@+id/imageview" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="FLIP MODE" 
     android:id="@+id/textView" 
     android:layout_above="@+id/button" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

,併爲它們

<?xml version="1.0" encoding="utf-8"?> 
<!--animfliphorizontal--> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:duration="1000" 
     android:propertyName="rotationX" 
     android:valueFrom="180" 
     android:valueTo="0" > 
    </objectAnimator> 
</set> 
<?xml version="1.0" encoding="utf-8"?> 
<!--animflipvertical--> 
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > 
    <objectAnimator 
     android:duration="1000" 
     android:propertyName="rotationY" 
     android:valueFrom="0" 
     android:valueTo="180" > 
    </objectAnimator> 
</set> 
2個動畫文件夾
2

你是幸運的!因爲我剛剛開發了一個新的庫FlipView,其中包含基本的翻轉動畫並延伸ViewFlipper。我的意思是一個完全可定製的庫,您可以用任何形式的動畫和形狀(以及更多)交換任何類型的視圖和佈局,包括您搜索的Gmail圖像翻轉。

請看看。

+0

感謝的人!這是很酷的圖書館! – maXp

1

這是確切的答案:

1)在動畫文件夾定義兩個動畫xml文件:

scale_in.xml:

<?xml version="1.0" encoding="utf-8"?> 
<scale 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/decelerate_interpolator" 
    android:fromXScale="0.0" 
    android:toXScale="1.0" 
    android:fromYScale="1.0" 
    android:toYScale="1.0" 
    android:pivotX="50%" 
    android:fillAfter="false" 
    android:duration="150" /> 

scale_out.xml:

<?xml version="1.0" encoding="utf-8"?> 
<scale 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/decelerate_interpolator" 
    android:fromXScale="1.0" 
    android:toXScale="0.0" 
    android:fromYScale="1.0" 
    android:toYScale="1.0" 
    android:pivotX="50%" 
    android:fillAfter="false" 
    android:duration="150" /> 

2)在您的活動中:

Animation animation = AnimationUtils.loadAnimation(YourActivity.this, R.anim.scale_out); 
layoutWithCircleDrawable.startAnimation(animation); 

然後:

animation.setAnimationListener(new Animation.AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       //your code for change the layout image or ... 

       Animation animation2 = AnimationUtils.loadAnimation(YourActivity.this, R.anim.scale_in); 
       layoutWithCircleDrawable.startAnimation(animation2); 
      } 
     });