2017-05-25 28 views
0

我想製作一個自定義ProgressBar類,我想在其中顯示帶圖像旋轉的ProgressBar。在任何類中動態顯示Android自定義ProgressBar或ProgressDialog

我試過了...

my_progress_indeterminate.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/spool" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fromDegrees="0" 
    android:toDegrees="1080"> 
</rotate> 

MyProgress.java

public class MyProgress extends ProgressDialog { 
    //private AnimationDrawable animation; 
    ProgressBar la; 
    public ProgressDialog ctor(Context context) { 
    MyProgress dialog = new MyProgress(context); 
    dialog.setIndeterminate(true); 
    dialog.setIndeterminateDrawable(this.getContext().getResources().getDrawable(R.drawable.my_progress_indeterminate)); 
    dialog.setCancelable(false); 
    return dialog; 
    } 

    public MyProgress(Context context) { 
    super(context); 
    } 

    public MyProgress(Context context, int theme) { 
    super(context, theme); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_loader); 

    la = (ProgressBar) findViewById(R.id.animation); 


    } 

    @Override 
    public void show() { 
    super.show(); 
//show the progressbar which is rotating image 

    } 

    @Override 
    public void dismiss() { 
    super.dismiss(); 
    //hide the progress which is rotating image 
    } 
} 

在我想告訴我的進步其它類,我這樣做:

MyProgress rotateImage; 
protateImage = new MyProgress(getActivity()); 

protateImage.show(); //for showing 

protateImage.hide(); //for hiding 

ViewLoader.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:background="@null" 
    > 


    <ProgressBar 
     android:id="@+id/animation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="5dip" 
     android:indeterminateDrawable="@drawable/my_progress_indeterminate" /> 

</LinearLayout> 

它運作良好,但我看不到我的圖像旋轉, 只有機器人定製進度紡紗風格所示,用對話的背景。

我要的是通過創建我的類的實例,並顯示進度條(旋轉圖像)或隱藏它,只顯示我的形象在我的項目的任何位置旋轉......

我怎樣才能使它正確嗎?任何幫助...

+0

你還可以發佈view_loader.xml和animation.xml的內容嗎? –

+0

檢查我編輯問題提供的視圖loader.xml文件 – user6768912

回答

0

跟着this answer

,你需要在除了做的唯一一件事情就是添加自定義負載繪製,使用setIndeterminateDrawable()

ProgressDialog pd = new ProgressDialog(getActivity(), R.style.MyTheme); 
pd.setCancelable(false); 
pd.setIndeterminateDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.my_progress_indeterminate)); 
pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small); 
pd.show();` 

無需定製ProgressDialog子類。