1. 程式人生 > >一個常見的一個Dialog動畫旋轉效果

一個常見的一個Dialog動畫旋轉效果

在我們進行一個網路訪問的過程的時候,我們需要大概1到2秒鐘的等待,所以我們給它一個動畫效果,當網路訪問成功的時候動畫隨之結束,給使用者一個良好的體驗效果.寫了一個簡單的Demo具體程式碼如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBt = (Button) findViewById(bt1);
        mBt.setOnClickListener(this
); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt1: Animal_Custom animal_custom = new Animal_Custom(this,null); animal_custom.show(); startActivity(new Intent(MainActivity.this,TwoActivity.class)); } } }

在跳轉第二個頁面的時候之前先出現一個動畫效果,然後再跳轉,以為Intent跳轉非常快,所以顯示效果不太明顯,主要還是用於網路訪問的操作等待時間時使用的效果

public class Animal_Custom extends Dialog {
    private String data_text;
    private Context mContext;
    private ImageView mIv_show;
    private RelativeLayout mRl_ahow;

    public Animal_Custom(Context context, String text) {
        super
(context); this.data_text = text; this.mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.animal_layout); initView(); } private void initView() { mIv_show = (ImageView) findViewById(R.id.iv_show); mRl_ahow = (RelativeLayout) findViewById(R.id.rl_show); mIv_show.setBackgroundResource(R.drawable.Animal_Scale); AnimationDrawable background = (AnimationDrawable) mIv_show.getBackground(); background.start(); ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(200); mRl_ahow.startAnimation(scaleAnimation); Window window = this.getWindow(); WindowManager.LayoutParams params = this.getWindow().getAttributes(); window.setBackgroundDrawable(new BitmapDrawable()); //設定dialog的對話方塊透明程度背景 params.dimAmount = 0.5f; window.setAttributes(params); //點選以外的區域會不消失 setCancelable(false); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Animal_Custom.this.dismiss(); return false; }else { return super.onKeyDown(keyCode, event); } } }

這是 Animal_Scale:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:drawable="@mipmap/r1"
    android:duration="50" />
    <item
        android:drawable="@mipmap/r2"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r3"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r4"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r5"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r6"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r7"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r8"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r9"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r10"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r11"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r12"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r13"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r14"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r15"
        android:duration="50" />
    <item
        android:drawable="@mipmap/r16"
        android:duration="50" />
</animation-list>

這樣就會出現一個旋轉的圖案了.