2014-10-03 69 views
-1

我正在做一个简单的android程序,我想在点击我的主XML文件的按钮后从图像数组中依次播放图像。 但程序关闭不幸。从图像数组中播放图像

代码:

package com.example.play_image; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
//import android.view.View; 
//import android.widget.Button; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

int imageList[]={R.drawable.exam_a,R.drawable.exam_b,R.drawable.exam_c, 
    R.drawable.exam_d,R.drawable.exam_e}; 

ImageView letter; 
@Override 

protected void onCreate(Bundle savedInstanceState){  
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
}  

public void play_image(){ 
    for(int i=0;i<5;i++){ 
     letter.setBackgroundResource(imageList[0]); 
    } 
} 

XML文件:

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"  
> 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="158dp" 
    android:background="@drawable/exam_a" 
    android:onClick="play_image" /> 
</RelativeLayout> 

我还有一个问题:

  1. 我要玩图像就像每个图像无线会出现在屏幕上并停留一段时间(如10秒),然后出现另一个。我应该怎么做?
  2. 我该怎么做才能在该图像上添加动画?

回答

0

您需要设置您的ImageView。像这样:

ImageView letter = (ImageView)findViewById(R.id.myImageView); 

,并在您play_image方法我想补充一点:

public void play_image(ImageView letter){ 

//your code 

} 

,然后当你启动它只是传递的ImageView对象像这样

play_images(letter); //or any other imageview 

下一页在你的play_images方法中,我确定你知道你并没有改变所呈现的图像。您将0作为数组项。你需要用我替换0或你在for循环中使用的Int。如果

for(int res : imageList){ 
     letter.setBackgroundResource(res); 
    } 

它仍然没有工作,你可以尝试使用可抽拉通过更换letter.setBackgroundResource行:

也是你的循环逻辑可以使用以下简化。像这样:

letter.setImageDrawable(getResources().getDrawable(int res id)); 
+0

我应该创造我的XML文件名为 “myImageView” 任何ImageView的? – STM 2014-10-03 19:25:04

0

首先声明图像视图

letter = (ImageView)findViewById(R.id.xxxxx); 

动画,你有两种方式:

1)使用AnimationDrawable

AnimationDrawable animationDrawable = new AnimationDrawable(); 
    animationDrawable.addFrame(frame, duration); 

帧绘制也应如此从你身上获取id

Drawable frame = getResources().getDrawable(imageList[x]); 

2)使用可运行

int i=0; 
runnable = new Runnable() { 

     @Override 
     public void run() { 
      letter.setBackgroundResource(imageList[i]); 
      if(i<5){ 
       handler.postDelayed(this, 10000); 
       i++; 
      } 
     } 
    }; 
    handler = new Handler(); 
    handler.post(runnable);