簡體   English   中英

在相對布局中播放gif圖像

[英]play gif image in relative layout

我很困惑如何在我的布局中播放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  
        }
     });
}

先感謝您!

據我所知,android不支持gif動畫,你必須通過其他方式創建動畫,如系列圖像

比如下面鏈接的例子

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

Android可以解碼gif格式。

看看這篇文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM