2015-12-05 167 views
-2

我每隔5秒就會改變Imageview中的圖像並顯示一些動畫以顯示過渡。但動畫非常簡單,我想要一個好的動畫。可以給我一些像樣的動畫代碼,我可以在我的應用程序中使用。 下面是代碼在Imageview中更改圖像

public class Sampledslr extends Activity { 
    Handler handler = new Handler(); 
    int count; 
    int images[] = { R.drawable.ds8, 
      R.drawable.mr1, 
      R.drawable.mr3, 
      R.drawable.mr4, 
      R.drawable.mr6, 
      R.drawable.mr3, 
      R.drawable.mr7 }; 
    ImageView imageView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sampledslr); 
     imageView = (ImageView) findViewById(R.id.imageView1); 

     //imageView.setImageDrawable(null); 
     //imageView.setScaleType(ScaleType.FIT_XY); 
     handler.postDelayed(changeImage, 7000); 
    } 
    Runnable changeImage = new Runnable() { 

     @Override 
     public void run() { 

      if (count >6) { 
       handler.removeCallbacks(changeImage); 
      } else { 

       if (count >= 6) 
        count = 0; 
       AlphaAnimation animation1 = new AlphaAnimation(0.5f, 1.0f); //here is a bit of animation for ya ;) 
       animation1.setDuration(5000); 
       animation1.setStartOffset(1000); //time for that color effect 
       animation1.setFillAfter(true); 
       imageView.startAnimation(animation1); 
       imageView.setImageResource(images[count++]); 
       handler.postDelayed(changeImage, 7000); 
       } 
     } 
    }; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
+1

'誰能提出一個好的動畫。' - 這不是StackOverflow的工作原理。 –

+0

你可以給我一些像樣的動畫代碼,我可以在我的代碼中實現嗎? – Cr7

回答

0

最好的辦法是創建一個XML文件到RES /阿尼姆和,然後將其分配給ImageView的。

實例動畫以xml:

anim_clockwise

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator" > 

    <alpha 
     android:fromAlpha="0" 
     android:toAlpha="1" 
     android:duration="350" > 
    </alpha> 

</set> 

anim_rotate

<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="1000"/> 

slide_out_left

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="-50%p" 
     android:duration="@android:integer/config_mediumAnimTime"/> 
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
     android:duration="@android:integer/config_mediumAnimTime" /> 
</set> 

slide_out_right

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="50%p" android:toXDelta="0" 
     android:duration="@android:integer/config_mediumAnimTime"/> 
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:duration="@android:integer/config_mediumAnimTime" /> 
</set> 

實施例放動畫成imageview的

private Animation mAnimationSplash; 

。 。 。

private void animImage() { 

     mAnimationSplash = AnimationUtils.loadAnimation(this, R.anim.anim_clockwise); 
     mImageView.setAnimation(mAnimationSplash); 

    } 
+0

我在loadAnimation中發生錯誤。 – Cr7

+0

你能把這個錯誤嗎?在Android Monitor – dpulgarin

+0

中顯示的錯誤我在runnable的run函數中使用它。當我在oncreate中使用它時,它工作正常,但我想在runnable中使用它。 – Cr7