下記のように作成する。
① resのdrawableの直下にpng図を格納する。
② drawableの直下に、anim_pgbar.xmlを作成
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/loading_01" android:duration="200"/> <item android:drawable="@drawable/loading_02" android:duration="200"/> <item android:drawable="@drawable/loading_03" android:duration="200"/> <item android:drawable="@drawable/loading_04" android:duration="200"/> <item android:drawable="@drawable/loading_05" android:duration="200"/> <item android:drawable="@drawable/loading_06" android:duration="200"/> <item android:drawable="@drawable/loading_07" android:duration="200"/> <item android:drawable="@drawable/loading_08" android:duration="200"/> <item android:drawable="@drawable/loading_09" android:duration="200"/> <item android:drawable="@drawable/loading_10" android:duration="200"/> <item android:drawable="@drawable/loading_11" android:duration="200"/> <item android:drawable="@drawable/loading_12" android:duration="200"/> </animation-list>
③ レイアウトを作成
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img_pgbar"/> </LinearLayout>
④ アクティビティを作成
public class MainActivity extends AppCompatActivity { private ImageView img_pgbar1; private AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img_pgbar1 = (ImageView) findViewById(R.id.img_pgbar); img_pgbar1.setBackgroundResource(R.drawable.anim_pgbar); ad = (AnimationDrawable) img_pgbar1.getBackground(); img_pgbar1.postDelayed(new Runnable() { @Override public void run() { ad.start(); } }, 100); } }
参考サイト:
http://seesaawiki.jp/w/moonlight_aska/d/%A5%D5%A5%EC%A1%BC%A5%E0%A5%A2%A5%CB%A5%E1%A1%BC%A5%B7%A5%E7%A5%F3%A4%F2%B9%D4%A4%A6
http://www.runoob.com/w3cnote/android-tutorial-progressbar.html