I create a simple Project to load all images in drawable
これが私のコードです
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ImageView)findViewById(R.id.tung);
ct = getApplicationContext();
try
{
IDs = getAllResourceIDs(R.drawable.class);
n= IDs.length;
dem=0;
list.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
++dem;
list.setImageResource(IDs[dem]);
if(dem==n-1)
dem=0;
}
});
Thread x = new Thread(
new Runnable ()
{
public void run()
{
while(true)
{
try
{
++dem;
//Toast.makeText(ct, ""+dem, 20).show();
list.setImageResource(IDs[dem]);
if(dem==n-1)
dem=0;
}
catch(Exception e)
{
//Toast.makeText(ct,e.toString(), 200).show();
}
}
}
}
);
x.start();
}
catch(Exception e)
{
Toast.makeText(this,e.toString(), 200).show();
}
}
anh ここに getAllResourceIDs 関数があり、すべての ID を取得します
private int[] getAllResourceIDs(Class<?> aClass) throws IllegalArgumentException{
/* Get all Fields from the class passed. */
Field[] IDFields = aClass.getFields();
/* int-Array capable of storing all ids. */
int[] IDs = new int[IDFields.length];
try {
/* Loop through all Fields and store id to array. */
for(int i = 0; i < IDFields.length; i++){
/* All fields within the subclasses of R
* are Integers, so we need no type-check here. */
// pass 'null' because class is static
IDs[i] = IDFields[i].getInt(null);
}
} catch (Exception e) {
/* Exception will only occur on bad class submitted. */
throw new IllegalArgumentException();
}
return IDs;
ファイル main.xml には id = "tung" の ImageView しかありません コード list.setImageResource(IDs[dem]); を使用して、x という名前の Thead を使用してすべての画像をロードしようとしました。私のコードでわかるように、何も起こりません。説明してもらえますか! どうもありがとうございます !