Android-loading-spinner
Android-スピナーの読み込み
進行状況バーを読み込むことで、Androidでタスクの進行状況を表示できます。 進行状況バーには2つの形があります。 読み込みバーと読み込みスピナー。 この章では、スピナーについて説明します。
スピナーは、完了の合計時間が不明なタスクの進行状況を表示するために使用されます。 それを使用するには、このようにxmlで定義する必要があります。
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
xmlで定義した後、ProgressBarクラスを介してjavaファイルでその参照を取得する必要があります。 その構文は以下のとおりです-
private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);
その後、setVisibilityメソッドを使用して、表示を非表示にし、必要に応じて元に戻すことができます。 その構文は以下のとおりです-
spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
これらのメソッドとは別に、ProgressBarクラスで定義された他のメソッドがあり、これを使用してスピナーをより効果的に処理できます。
Sr.No | Method & description |
---|---|
1 |
isIndeterminate() この進行状況バーが不確定モードになっているかどうかを示します |
2 |
postInvalidate() イベントループの後続のサイクルで無効化が発生するようにします |
3 |
setIndeterminate(boolean indeterminate) この進行状況バーの不確定モードを変更します |
4 |
invalidateDrawable(Drawable dr) 指定されたDrawableを無効化します |
5 |
incrementSecondaryProgressBy(int diff) 進行状況バーの2番目の進行状況を指定した量だけ増やす |
6 |
getProgressDrawable() プログレスモードでプログレスバーを描画するために使用されるドロアブルを取得します |
例
ProgressBarを使用してスピナーを処理する例を示す例を次に示します。 ボタンをクリックするとスピナーを有効にする基本的なアプリケーションが作成されます。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Need to create a xml file in drawable folder.it contains shape and rotate information about the progress bar |
5 | Run the application and choose a running android device and install the application on it and verify the results |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
Button b1;
private ProgressBar spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
spinner=(ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinner.setVisibility(View.VISIBLE);
}
});
}
}
以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。
'_次のコードで abc はfinddevguides.comのロゴを示します_
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Progress Dialog" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="download"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:progressDrawable="@drawable/circular_progress_bar"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
以下は res/drawable/circular_progress_bar.xml の内容です。
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<gradient
android:centerColor="#007DD6"
android:endColor="#007DD6"
android:startColor="#007DD6"
android:angle="0"
android:type="sweep"
android:useLevel="false"/>
</shape>
</rotate>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
変更したばかりのアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
Android Loading Spinnerチュートリアル
ロードスピナーボタンをクリックして、ロードスピナーをオンにします。 それは以下の画像に示されています-