a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android中創建一個透明的進度對話框實例

2020-04-11 11:50:25
字體:
來源:轉載
供稿:網友

首先我們看一下什么叫做透明的進度對話框:



接下來我們講一下如何創建:
1、使用Eclipse創建一個新的Andr​​oid 項目,使用Android 2.2或以上。
2、在/res/layout文件夾,創建線性布局activity_main.xml文件,主要是為了添加一個文本標簽和一個按鈕

復制代碼 代碼如下:

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="8dp"
        android:textSize="20sp"
        android:text="Transparent Progress Indicator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check it out!"
        android:layout_marginTop="40dp"
        android:layout_gravity="center"
        android:id="@+id/the_button" />

</LinearLayout>

3、在/res/values中打開styles.xml,在這里將添加透明對話框的樣式。請務必指定父屬性,否則你在運行時會出現問題

復制代碼 代碼如下:

styles.xml
<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <!--  Transparent dialog -->
    <style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

</resources>

4、 在/res中間添加一個動態旋轉的動畫圖片:



5、現在可以實現您的MainActivity.java文件了

復制代碼 代碼如下:

MainActivity.java
package com.authorwjf.transparentprogressdialog;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

 private TransparentProgressDialog pd;
 private Handler h;
 private Runnable r;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  h = new Handler();
  pd = new TransparentProgressDialog(this, R.drawable.spinner);
  r =new Runnable() {
   @Override
   public void run() {
    if (pd.isShowing()) {
     pd.dismiss();
    }
   }
  };
  findViewById(R.id.the_button).setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  pd.show();
  h.postDelayed(r,5000);
 }

 @Override
 protected void onDestroy() {
  h.removeCallbacks(r);
  if (pd.isShowing() ) {
   pd.dismiss();
  }
  super.onDestroy();
 }

}

6、以下是實現透明動畫的代碼

復制代碼 代碼如下:

private class TransparentProgressDialog extends Dialog {

 private ImageView iv;

 public TransparentProgressDialog(Context context, int resourceIdOfImage) {
  super(context, R.style.TransparentProgressDialog);
         WindowManager.LayoutParams wlmp = getWindow().getAttributes();
         wlmp.gravity = Gravity.CENTER_HORIZONTAL;
         getWindow().setAttributes(wlmp);
  setTitle(null);
  setCancelable(false);
  setOnCancelListener(null);
  LinearLayout layout = new LinearLayout(context);
  layout.setOrientation(LinearLayout.VERTICAL);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  iv = new ImageView(context);
  iv.setImageResource(resourceIdOfImage);
  layout.addView(iv, params);
  addContentView(layout, params);
 }

 @Override
 public void show() {
  super.show();
  RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
  anim.setInterpolator(new LinearInterpolator());
  anim.setRepeatCount(Animation.INFINITE);
  anim.setDuration(3000);
  iv.setAnimation(anim);
  iv.startAnimation(anim);
 }
}

最后的結果是

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品毛片在线看 | 国产精品一区二区三区四区 | 成人18视频在线观看 | 黄色视屏在线免费观看 | 精品国产欧美一区二区 | 欧美一区高清 | 91日日| 综合色综合 | 国产一区二区三区免费 | 成人精品国产 | 麻豆三级| 成人免费视频在线看 | 91色在线观看 | 欧美一区二区三区在线观看 | 日日综合| 天天干一干 | av福利网站| av免费在线播放 | 在线观看日韩一区 | 黄色一级片在线观看 | 福利网址 | 国产成人精品综合 | 天天做天天爱天天操 | 日韩国产欧美精品 | 中文字幕在线观 | 一区二区三区免费在线 | 91视频污黄 | av片网站 | 亚洲午夜精品一区二区三区他趣 | 男女精品网站 | 希岛爱理在线 | 亚洲高清视频一区二区 | 久草免费在线 | 国产一区二区毛片 | 啪一啪| 一级视频在线观看 | 日韩精品| 久久久久久久久久久久福利 | 久久午夜精品影院一区 | 日韩精品免费 | 99re视频在线播放 |