在相对布局中播放gif图像

时间:2011-10-26 13:45:53

标签: android relativelayout gif

我很困惑如何在我的布局中播放gif图像。我的布局使用RelativeLayout,图像位于中心。我正在寻找播放GIF的方法但是示例并没有告诉我如何将它实现到我的布局中,其中包含另一个小部件,不仅仅是单独播放gif。请帮我这样做。

这是代码:

choosepet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
android:paddingTop="50px"
android:paddingBottom="10px"
>

<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/choose"
android:layout_gravity="center"
android:layout_marginBottom="10px"
/>

<Spinner
android:id="@+id/pet"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/pettype"
/>
<ImageView 
android:id="@+id/petpic"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cat"
/>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="10px"
android:paddingLeft="10px">

<RadioGroup
android:orientation="horizontal"
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>   
<TextView
android:text="Gender"
android:id="@+id/textGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="13px"
android:textColor="#000000"
/>

<RadioButton
android:id="@+id/maleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_toRightOf="@+id/textGender"
/>

<RadioButton 
android:id="@+id/femaleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_toRightOf="@+id/maleRadio"
/>
</RadioGroup>>

<TextView 
android:text="Name"
android:id="@+id/textName"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="11px"
android:layout_above="@+id/genderGroup"
android:textColor="#000000"
android:layout_marginRight="12px"
/>

<EditText
android:id="@+id/name"
android:layout_width="150sp"
android:layout_height="40sp"
android:layout_toRightOf="@+id/textName"
android:layout_above="@+id/genderGroup"
/>

<ImageButton
android:id="@+id/process"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/letsplay"
android:background="@null"
android:layout_toRightOf="@+id/genderGroup"
android:layout_alignParentBottom="true"
android:layout_marginBottom="38px"
/>

</RelativeLayout>
</LinearLayout>

这是choosepet.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.choosepet_layout);

    Spinner spinnerPet = (Spinner) findViewById(R.id.pet);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.pet_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerPet.setAdapter(adapter);
    EditText nameField = (EditText) findViewById(R.id.name);
    nameField.requestFocus();
    ImageButton processButton = (ImageButton) findViewById(R.id.process);
     processButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
                 Intent myIntent = new Intent(view.getContext(), petMain.class);
                 startActivityForResult(myIntent, 0);
             }
     });
     final ImageView imgPet = (ImageView) findViewById(R.id.petpic);
     spinnerPet.setOnItemSelectedListener(new OnItemSelectedListener() {
         public void onItemSelected(AdapterView<?>parent, 
               View view, int pos, long id){
             String petType = parent.getItemAtPosition(pos).toString();
             if(petType.equals("Anjing")){
                 imgPet.setImageResource(R.drawable.dog);
             }else{
                 imgPet.setImageResource(R.drawable.cat);
             }
         }
        @Override
        public void onNothingSelected(AdapterView parent) {
            // TODO Auto-generated method stub  
        }
     });
}

提前谢谢你!

2 个答案:

答案 0 :(得分:3)

据我所知,android不支持gif动画,你必须通过其他方式创建动画,如系列图像

与下面链接的示例相似

http://yekmer.posterous.com/how-to-make-a-loading-animator-in-android

答案 1 :(得分:1)

Android可以解码gif格式。

看看这个post